Skip to content
Closed
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
31 changes: 23 additions & 8 deletions src/Converter/ResourceConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Dogado\JsonApi\Model\Resource\ResourceInterface;
use Dogado\JsonApi\Support\Model\CustomAttributeSetterInterface;
use Dogado\JsonApi\Support\Model\DataModelAnalyser;
use Dogado\JsonApi\Support\Model\ValueObjectFactoryInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
Expand Down Expand Up @@ -78,34 +79,48 @@ protected function setValue(ReflectionClass $reflection, object $model, array $p
}

$property = $reflection->getProperty($propertyName);
if (null !== $property->getType() && !$property->getType() instanceof ReflectionNamedType) {
$propertyType = $property->getType();
if (null !== $propertyType && !$propertyType instanceof ReflectionNamedType) {
// other reflection types, like union types, are not supported
return;
}

if (
null === $property->getType() ||
'mixed' === $property->getType()->getName() ||
$property->getType()->getName() === gettype($value)
null === $propertyType ||
'mixed' === $propertyType->getName() ||
$propertyType->getName() === gettype($value)
) {
$property->setAccessible(true);
$property->setValue($model, $value);
return;
}

if (class_exists($property->getType()->getName()) && 0 < count($propertyMap)) {
$propertyClassName = $propertyType->getName();
if (class_exists($propertyClassName) && 0 < count($propertyMap)) {
if (null === $value) {
return;
}

$property->setAccessible(true);
$valueObject = $property->getValue($model);
if (null === $valueObject) {
return;
if (
!(new ReflectionClass($propertyClassName))
->implementsInterface(ValueObjectFactoryInterface::class)
) {
return;
}
/** @var ValueObjectFactoryInterface $propertyClassName */
$valueObject = $propertyClassName::create();
$property->setValue($model, $valueObject);
}

$this->setValue(new ReflectionClass($valueObject), $valueObject, $propertyMap, $value);
return;
}

if (null === $value) {
if (!$property->getType()->allowsNull()) {
if (!$propertyType->allowsNull()) {
throw DataModelSerializerException::propertyIsNotNullable(
$this->resource ? $this->resource->type() : 'unknown',
get_class($model),
Expand All @@ -122,7 +137,7 @@ protected function setValue(ReflectionClass $reflection, object $model, array $p
return;
}

switch ($property->getType()->getName()) {
switch ($propertyType->getName()) {
case 'boolean':
case 'bool':
$property->setAccessible(true);
Expand Down
10 changes: 10 additions & 0 deletions src/Support/Model/ValueObjectFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Dogado\JsonApi\Support\Model;

interface ValueObjectFactoryInterface
{
public static function create(): self;
}
13 changes: 13 additions & 0 deletions tests/Converter/ResourceConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function testResourceToModel(): void
'number' => (string) $this->faker()->numberBetween(),
'ignoreOnNull' => $this->faker()->text(),
],
'nullableValueObject' => [
'number' => (string) $this->faker()->numberBetween(),
'ignoreOnNull' => $this->faker()->text(),
],
'arrayItems' => [
$this->faker->slug() => $this->faker->text(),
$this->faker->slug() => $this->faker->text(),
Expand Down Expand Up @@ -82,6 +86,15 @@ public function testResourceToModel(): void
$attributes->getSubCollection('values')->getRequired('ignoreOnNull'),
$model->getValueObject()->getIgnoreOnNull()
);
$this->assertNotNull($model->getNullableValueObject());
$this->assertEquals(
(int) $attributes->getSubCollection('nullableValueObject')->getRequired('number'),
$model->getNullableValueObject()->getSubItem()
);
$this->assertEquals(
$attributes->getSubCollection('nullableValueObject')->getRequired('ignoreOnNull'),
$model->getNullableValueObject()->getIgnoreOnNull()
);
$this->assertEquals(
$attributes->get('arrayItems'),
$model->getArrayItems()
Expand Down
Loading