Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/MetaCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,14 @@ public function cast(string $class, $data)
throw new InvalidArgumentException("Can not cast '$type' to '$class': expected object or array");
}

if (is_array($data)) {
$data = (object)$data;
}

if (is_a($data, $class)) {
return clone $data;
}

$meta = $this->metaFactory->forClass($class);
$data = $this->castProperties($meta, $data);

return $this->typeCast->to($class)->cast($data);
return $data;
}

/**
Expand All @@ -75,18 +71,22 @@ public function cast(string $class, $data)
*/
protected function castProperties(MetaClass $meta, $data)
{
$isArray = is_array($data);
if ($isArray) {
$data = (object)$data;
}

$data = clone $data;
$properties = $meta->getProperties();

foreach ($properties as $name => $item) {
$toType = $item->get('type');
if (!$toType || !isset($data->$name)) {
continue;
}

$data->$name = $this->typeCast->to($toType)->cast($data->$name);
if ($toType && isset($data->$name)) {
$data->$name = $this->typeCast->to($toType)->cast($data->$name);
}
}

return $data;
return $isArray ? (array)$data : $data;
}
}
36 changes: 16 additions & 20 deletions tests/MetaCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ public function castProvider()
'baz' => 'value3'
];

$expected = [
'foo' => 'casted_value1',
'bar' => 'value2',
'baz' => 'casted_value3'
];

return [
[$data],
[(object)$data],
[$data, $expected],
[(object)$data, (object)$expected],
];
}

Expand All @@ -94,7 +100,7 @@ public function castProvider()
*
* @dataProvider castProvider
*/
public function testCast($data)
public function testCast($data, $expected)
{
$class = 'Foo';
$meta = $this->createMock(MetaClass::class);
Expand All @@ -120,24 +126,16 @@ public function testCast($data)
'pir' => $property5
];

$castedProperties = (object)[
'foo' => 'casted_value1',
'bar' => 'value2',
'baz' => 'casted_value3'
];

$expected = (object)$castedProperties;

$this->metaFactory->expects($this->once())->method('forClass')->with($class)->willReturn($meta);
$meta->expects($this->once())->method('getProperties')->willReturn($properties);

$this->typeCast->expects($this->exactly(3))->method('to')
->withConsecutive(['type1'], ['type3'], [$class])
->willReturnOnConsecutiveCalls($castHandler, $castHandler, $castHandler);
$this->typeCast->expects($this->exactly(2))->method('to')
->withConsecutive(['type1'], ['type3'])
->willReturnOnConsecutiveCalls($castHandler, $castHandler);

$castHandler->expects($this->exactly(3))->method('cast')
->withConsecutive(['value1'], ['value3'], [$castedProperties])
->willReturnOnConsecutiveCalls('casted_value1', 'casted_value3', $expected);
$castHandler->expects($this->exactly(2))->method('cast')
->withConsecutive(['value1'], ['value3'])
->willReturnOnConsecutiveCalls('casted_value1', 'casted_value3');

$metaCast = new MetaCast($this->metaFactory, $this->typeCast);
$result = $metaCast->cast('Foo', $data);
Expand All @@ -155,12 +153,10 @@ public function testCastNoProperties()
$meta = $this->createMock(MetaClass::class);
$castHandler = $this->createMock(HandlerInterface::class);

$expected = (object)$data;
$expected = $data;

$this->metaFactory->expects($this->once())->method('forClass')->with($class)->willReturn($meta);
$meta->expects($this->once())->method('getProperties')->willReturn([]);
$this->typeCast->expects($this->once())->method('to')->with($class)->willReturn($castHandler);
$castHandler->expects($this->once())->method('cast')->with((object)$data)->willReturn($expected);

$metaCast = new MetaCast($this->metaFactory, $this->typeCast);
$result = $metaCast->cast('Foo', $data);
Expand Down