Skip to content

Commit

Permalink
Merge pull request #3 from healerz/feat/enhanceAllowedAdditionalField…
Browse files Browse the repository at this point in the history
…sFeature

feat: enhance allowed additional fields feature
  • Loading branch information
dominikzogg authored Dec 11, 2017
2 parents 9a7678d + 15b3268 commit 2a1a5ce
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 40 deletions.
6 changes: 3 additions & 3 deletions doc/Denormalizer/DenormalizerContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use Psr\Http\Message\ServerRequestInterface;

$request = ...;

$context = new DenormalizerContext(true, ['group1'], $request);
$context = new DenormalizerContext(['allowed_additional_field'], ['group1'], $request);

echo $context->isAllowedAdditionalFields();
// true
echo $context->getAllowedAdditionalFields();
// ['allowed_additional_field']

print_r($context->getGroups());
// ['group1']
Expand Down
6 changes: 3 additions & 3 deletions doc/Denormalizer/DenormalizerContextBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use Psr\Http\Message\ServerRequestInterface;
$request = ...;

$context = DenormalizerContextBuilder::create()
->setAllowedAdditionalFields(true)
->setAllowedAdditionalFields(['allowed_additional_field'])
->setGroups(['group1'])
->setRequest($request)
->getContext();

echo $context->isAllowedAdditionalFields();
// true
echo $context->getAllowedAdditionalFields();
// ['allowed_additional_field']

print_r($context->getGroups());
// ['group1']
Expand Down
6 changes: 4 additions & 2 deletions src/Denormalizer/Denormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public function denormalize($object, array $data, DenormalizerContextInterface $
unset($data[$denormalizationFieldMapping->getName()]);
}

if ([] !== $data && !$context->isAllowedAdditionalFields()) {
$this->handleNotAllowedAdditionalFields($path, array_keys($data));
if (null !== $context->getAllowedAdditionalFields()
&& [] !== $fields = array_diff(array_keys($data), $context->getAllowedAdditionalFields())
) {
$this->handleNotAllowedAdditionalFields($path, $fields);
}

return $object;
Expand Down
12 changes: 6 additions & 6 deletions src/Denormalizer/DenormalizerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
final class DenormalizerContext implements DenormalizerContextInterface
{
/**
* @var bool
* @var array|null
*/
private $allowedAdditionalFields = false;
private $allowedAdditionalFields;

/**
* @var string[]
Expand All @@ -24,12 +24,12 @@ final class DenormalizerContext implements DenormalizerContextInterface
private $request;

/**
* @param bool $allowedAdditionalFields
* @param array|null $allowedAdditionalFields
* @param string[] $groups
* @param ServerRequestInterface|null $request
*/
public function __construct(
bool $allowedAdditionalFields = false,
array $allowedAdditionalFields = null,
array $groups = [],
ServerRequestInterface $request = null
) {
Expand All @@ -39,9 +39,9 @@ public function __construct(
}

/**
* @return bool
* @return array|null
*/
public function isAllowedAdditionalFields(): bool
public function getAllowedAdditionalFields()
{
return $this->allowedAdditionalFields;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Denormalizer/DenormalizerContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class DenormalizerContextBuilder implements DenormalizerContextBuilderInterface
{
/**
* @var bool
* @var array|null
*/
private $allowedAdditionalFields;

Expand All @@ -33,19 +33,20 @@ private function __construct()
public static function create(): DenormalizerContextBuilderInterface
{
$self = new self();
$self->allowedAdditionalFields = false;
$self->allowedAdditionalFields = null;
$self->groups = [];

return $self;
}

/**
* @param bool $allowedAdditionalFields
* @param array|null $allowedAdditionalFields
*
* @return DenormalizerContextBuilderInterface
*/
public function setAllowedAdditionalFields(bool $allowedAdditionalFields): DenormalizerContextBuilderInterface
{
public function setAllowedAdditionalFields(
array $allowedAdditionalFields = null
): DenormalizerContextBuilderInterface {
$this->allowedAdditionalFields = $allowedAdditionalFields;

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Denormalizer/DenormalizerContextBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ interface DenormalizerContextBuilderInterface
public static function create(): DenormalizerContextBuilderInterface;

/**
* @param bool $allowedAdditionalFields
* @param array|null $allowedAdditionalFields
*
* @return self
*/
public function setAllowedAdditionalFields(bool $allowedAdditionalFields): self;
public function setAllowedAdditionalFields(array $allowedAdditionalFields = null): self;

/**
* @param string[] $groups
Expand Down
4 changes: 2 additions & 2 deletions src/Denormalizer/DenormalizerContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
interface DenormalizerContextInterface
{
/**
* @return bool
* @return array|null
*/
public function isAllowedAdditionalFields(): bool;
public function getAllowedAdditionalFields();

/**
* @return string[]
Expand Down
6 changes: 3 additions & 3 deletions tests/Denormalizer/DenormalizerContextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testCreate()

self::assertInstanceOf(DenormalizerContextInterface::class, $context);

self::assertSame(false, $context->isAllowedAdditionalFields());
self::assertSame(null, $context->getAllowedAdditionalFields());
self::assertSame([], $context->getGroups());
self::assertNull($context->getRequest());
}
Expand All @@ -30,14 +30,14 @@ public function testCreateWithOverridenSettings()
$request = $this->getRequest();

$context = DenormalizerContextBuilder::create()
->setAllowedAdditionalFields(true)
->setAllowedAdditionalFields(['allowed_field'])
->setGroups(['group1'])
->setRequest($request)
->getContext();

self::assertInstanceOf(DenormalizerContextInterface::class, $context);

self::assertSame(true, $context->isAllowedAdditionalFields());
self::assertSame(['allowed_field'], $context->getAllowedAdditionalFields());
self::assertSame(['group1'], $context->getGroups());
self::assertSame($request, $context->getRequest());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Denormalizer/DenormalizerContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testCreate()
{
$context = new DenormalizerContext();

self::assertSame(false, $context->isAllowedAdditionalFields());
self::assertSame(null, $context->getAllowedAdditionalFields());
self::assertSame([], $context->getGroups());
self::assertNull($context->getRequest());
}
Expand All @@ -26,9 +26,9 @@ public function testCreateWithOverridenSettings()
{
$request = $this->getRequest();

$context = new DenormalizerContext(true, ['group1'], $request);
$context = new DenormalizerContext(['allowed_field'], ['group1'], $request);

self::assertSame(true, $context->isAllowedAdditionalFields());
self::assertSame(['allowed_field'], $context->getAllowedAdditionalFields());
self::assertSame(['group1'], $context->getGroups());
self::assertSame($request, $context->getRequest());
}
Expand Down
19 changes: 11 additions & 8 deletions tests/Denormalizer/DenormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testDenormalizeWithDataContainsNumericKeys()
$this->getDenormalizationObjectMapping(),
]));

$denormalizer->denormalize(get_class($this->getObject()), ['test']);
$denormalizer->denormalize(get_class($this->getObject()), ['test'], $this->getDenormalizerContext([]));
}

public function testDenormalizeWithNotWorkingFactory()
Expand Down Expand Up @@ -91,7 +91,11 @@ public function testDenormalizeWithAdditionalData()
$this->getDenormalizationObjectMapping(),
]));

$denormalizer->denormalize(get_class($this->getObject()), ['name' => 'name', 'value' => 'value']);
$denormalizer->denormalize(
get_class($this->getObject()),
['name' => 'name', 'value' => 'value'],
$this->getDenormalizerContext([])
);
}

public function testDenormalizeWithAdditionalDataAndAllowIt()
Expand All @@ -102,8 +106,7 @@ public function testDenormalizeWithAdditionalDataAndAllowIt()

$object = $denormalizer->denormalize(
get_class($this->getObject()),
['name' => 'name', 'value' => 'value'],
$this->getDenormalizerContext(true)
['name' => 'name', 'value' => 'value']
);

self::assertSame('name', $object->getName());
Expand Down Expand Up @@ -138,7 +141,7 @@ public function testDenormalizeWithGroups()
$object = $denormalizer->denormalize(
get_class($this->getObject()),
['name' => 'name'],
$this->getDenormalizerContext(false, ['read'])
$this->getDenormalizerContext(null, ['read'])
);

self::assertSame('name', $object->getName());
Expand All @@ -153,7 +156,7 @@ public function testDenormalizeWithGroupsButNoGroupOnField()
$object = $denormalizer->denormalize(
get_class($this->getObject()),
['name' => 'name'],
$this->getDenormalizerContext(false, ['read'])
$this->getDenormalizerContext(null, ['read'])
);

self::assertNull($object->getName());
Expand Down Expand Up @@ -279,15 +282,15 @@ private function getFieldDenormalizer(): FieldDenormalizerInterface
* @return DenormalizerContextInterface
*/
private function getDenormalizerContext(
bool $allowedAdditionalFields = false,
array $allowedAdditionalFields = null,
array $groups = []
): DenormalizerContextInterface {
/** @var DenormalizerContextInterface|\PHPUnit_Framework_MockObject_MockObject $context */
$context = $this->getMockBuilder(DenormalizerContextInterface::class)
->setMethods([])
->getMockForAbstractClass();

$context->expects(self::any())->method('isAllowedAdditionalFields')->willReturn($allowedAdditionalFields);
$context->expects(self::any())->method('getAllowedAdditionalFields')->willReturn($allowedAdditionalFields);
$context->expects(self::any())->method('getGroups')->willReturn($groups);

return $context;
Expand Down
10 changes: 7 additions & 3 deletions tests/DeserializerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,12 @@ public function testDenormalizeWithAdditionalFieldsExpectsException()

$data = json_encode(['name' => 'Name', 'unknownField' => 'value']);

$deserializer->deserialize(Model::class, $data, 'application/json');
$deserializer->deserialize(
Model::class,
$data,
'application/json',
DenormalizerContextBuilder::create()->setAllowedAdditionalFields([])->getContext()
);
}

public function testDenormalizeWithAllowedAdditionalFields()
Expand All @@ -347,8 +352,7 @@ public function testDenormalizeWithAllowedAdditionalFields()
$object = $deserializer->deserialize(
Model::class,
$data,
'application/json',
DenormalizerContextBuilder::create()->setAllowedAdditionalFields(true)->getContext()
'application/json'
);

self::assertSame('Name', $object->getName());
Expand Down

0 comments on commit 2a1a5ce

Please sign in to comment.