Skip to content
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.1.0 - 2022-04-XX

- Resource converter: Add `Dogado\JsonApi\Support\Model\PlainAttributesInterface` to optionally be able to fetch the original attribute input in model instances

## v3.0.0 - 2021-10-01

- Request: Add ability to add custom query parameters to requests
Expand Down
1 change: 0 additions & 1 deletion src/Attribute/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Id
{

}
5 changes: 5 additions & 0 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\PlainAttributesInterface;
use Dogado\JsonApi\Support\Model\ValueObjectFactoryInterface;
use ReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -48,6 +49,10 @@ public function toModel(?ResourceInterface $resource, object $model): object
}

$attributeValues = $resource->attributes()->all();
if ($model instanceof PlainAttributesInterface) {
$model->setPlainAttributes($resource->attributes());
}

foreach ($analyser->getAttributesPropertyMap() as $attributeMap => $propertyMap) {
$this->setValue(
$reflection,
Expand Down
14 changes: 14 additions & 0 deletions src/Support/Model/PlainAttributesInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Dogado\JsonApi\Support\Model;

use Dogado\JsonApi\Support\Collection\KeyValueCollectionInterface;

interface PlainAttributesInterface
{
public function getPlainAttributes(): KeyValueCollectionInterface;

public function setPlainAttributes(KeyValueCollectionInterface $attributes): self;
}
24 changes: 24 additions & 0 deletions src/Support/Model/PlainAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Dogado\JsonApi\Support\Model;

use Dogado\JsonApi\Support\Collection\KeyValueCollection;
use Dogado\JsonApi\Support\Collection\KeyValueCollectionInterface;

trait PlainAttributesTrait
{
private ?KeyValueCollectionInterface $plainAttributes = null;

public function setPlainAttributes(KeyValueCollectionInterface $attributes): self
{
$this->plainAttributes = $attributes;
return $this;
}

public function getPlainAttributes(): KeyValueCollectionInterface
{
return $this->plainAttributes ?? new KeyValueCollection();
}
}
28 changes: 28 additions & 0 deletions tests/Converter/ResourceConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
use Dogado\JsonApi\Converter\ResourceConverter;
use Dogado\JsonApi\Exception\DataModelSerializerException;
use Dogado\JsonApi\Model\Resource\Resource;
use Dogado\JsonApi\Support\Collection\KeyValueCollectionInterface;
use Dogado\JsonApi\Support\Model\PlainAttributesInterface;
use Dogado\JsonApi\Tests\Converter\ResourceConverterTest\DataModel;
use Dogado\JsonApi\Tests\Converter\ResourceConverterTest\DataModelWithoutTypeAnnotation;
use Dogado\JsonApi\Tests\Converter\ResourceConverterTest\DataModelWithPlainAttributes;
use Dogado\JsonApi\Tests\Converter\ResourceConverterTest\ValueObjectWithFactory;
use Dogado\JsonApi\Tests\Converter\ResourceConverterTest\ValueObjectWithFactoryWrapper;
use Dogado\JsonApi\Tests\TestCase;
Expand Down Expand Up @@ -236,4 +239,29 @@ public function testNestedMissingValueDoesNotInitializeValueObjects(): void
$this->assertInstanceOf(ValueObjectWithFactoryWrapper::class, $model->getNested());
$this->assertNull($model->getNested()->getNullableValueObject());
}

public function testPlainAttributes(): void
{
$resource = new Resource(
'dummy-deserializer-model-with-plain-attributes',
(string) $this->faker()->numberBetween(),
[
'stringValue' => $this->faker()->userName(),
'values' => [
'number' => (string) $this->faker()->numberBetween(),
'ignoreOnNull' => $this->faker()->text(),
],
],
);

$model = new DataModelWithPlainAttributes();
$this->assertInstanceOf(PlainAttributesInterface::class, $model);
$attributes = $resource->attributes();
$this->assertInstanceOf(get_class($model), (new ResourceConverter())->toModel($resource, $model));
$plainAttributes = $model->getPlainAttributes();
$this->assertInstanceOf(KeyValueCollectionInterface::class, $plainAttributes);

$this->assertEquals($attributes->getRequired('stringValue'), $plainAttributes->getRequired('stringValue'));
$this->assertEquals($attributes->getRequired('values'), $plainAttributes->getRequired('values'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Dogado\JsonApi\Tests\Converter\ResourceConverterTest;

use Dogado\JsonApi\Attribute\Type;
use Dogado\JsonApi\Support\Model\PlainAttributesInterface;
use Dogado\JsonApi\Support\Model\PlainAttributesTrait;

#[Type('dummy-deserializer-model-with-plain-attributes')]
class DataModelWithPlainAttributes implements PlainAttributesInterface
{
use PlainAttributesTrait;
}