+
Skip to content
Merged
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
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
processIsolation= "false"
stopOnFailure="false"
bootstrap = "tests/bootstrap.php"

displayDetailsOnTestsThatTriggerWarnings="true"
>
<testsuites>
<testsuite name="Test Suite">
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/InvalidEnumValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Andrey\PancakeObject\Exceptions;

use InvalidArgumentException;

class InvalidEnumValueException extends InvalidArgumentException
{ }
10 changes: 8 additions & 2 deletions src/SimpleHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Andrey\PancakeObject\Attributes\Item;
use Andrey\PancakeObject\Attributes\SkipItem;
use Andrey\PancakeObject\Attributes\ValueObject;
use Andrey\PancakeObject\Exceptions\InvalidEnumValueException;
use Andrey\PancakeObject\KeyMapping\KeyMappingStrategy;
use Andrey\PancakeObject\KeyMapping\KeyMappingUnderscore;
use InvalidArgumentException;
Expand Down Expand Up @@ -141,11 +142,16 @@ private function handleBuiltin(array $data, string $key, ReflectionProperty $pro
/**
* @throws ReflectionException
*/
private function handleCustomType(mixed $value, string $type): mixed
private function handleCustomType(mixed $value, string $type): Payload
{
$typeReflection = new ReflectionClass($type);
if ($typeReflection->isEnum()) {
return new Payload(data: call_user_func($type.'::tryFrom', $value));
$data = call_user_func($type.'::tryFrom', $value);
$error = null;
if ($data === null) {
$error = new InvalidEnumValueException("Invalid enum value '{$value}' for type '{$type}'");
}
return new Payload(data: $data, error: $error);
}

// Recursively hydrate child/internal value objects
Expand Down
18 changes: 17 additions & 1 deletion tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Andrey\PancakeObject\Attributes\SkipItem;
use Andrey\PancakeObject\Attributes\ValueObject;
use Andrey\PancakeObject\Attributes\Item;
use Andrey\PancakeObject\Exceptions\InvalidEnumValueException;
use Andrey\PancakeObject\Payload;
use Andrey\PancakeObject\SimpleHydrator;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -63,7 +64,6 @@ public function testSimpleHydrate(): void
$this->assertFalse($obj->bool);
$this->assertNull($obj->nullableInt);

$this->assertInstanceOf(ChildObject::class, $obj->singleChild);
$this->assertIsArray($obj->singleChild->andImAnArrayOfInt);
$this->assertSame($data['single_child']['and_im_an_array_of_int'], $obj->singleChild->andImAnArrayOfInt);

Expand Down Expand Up @@ -177,4 +177,20 @@ public function testDefaultValueFromItem(): void
$this->assertCount(0, $object->singleChild->andImAnArrayOfInt);
$this->assertEquals('default child name', $object->singleChild->iHaveAName);
}

/**
* @throws ReflectionException
*/
public function testInvalidEnumValue(): void
{
$data = [
'enum' => 'C',
];


$this->expectException(InvalidEnumValueException::class);

$hydrator = new SimpleHydrator();
$hydrator->hydrate($data, TestObject::class);
}
}
2 changes: 0 additions & 2 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function testSimpleSerialize(): void
$this->assertIsArray($data["single_child"]["and_im_an_array_of_int"]);
$this->assertSame([4, 5, 6], $data["single_child"]["and_im_an_array_of_int"]);

$this->assertNull($data["nullable_int"]);

$this->assertIsArray($data["array_of_children"]);
$this->assertCount(2, $data["array_of_children"]);

Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载