Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/JMP-592/enhancePolicies #29

Merged
merged 5 commits into from
Aug 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Denormalizer/Denormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Chubbyphp\Deserialization\DeserializerRuntimeException;
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingInterface;
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
use Chubbyphp\Deserialization\Policy\GroupPolicy;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand Down Expand Up @@ -157,6 +158,10 @@ private function denormalizeField(
array $data,
$object
) {
if (!$this->isCompliant($context, $denormalizationFieldMapping, $object)) {
return;
}

if (!$this->isWithinGroup($context, $denormalizationFieldMapping)) {
return;
}
Expand Down Expand Up @@ -184,6 +189,25 @@ private function handleNotAllowedAdditionalFields(string $path, array $names)
throw $exception;
}

/**
* @param DenormalizerContextInterface $context
* @param DenormalizationFieldMappingInterface $mapping
* @param object $object
*
* @return bool
*/
private function isCompliant(
DenormalizerContextInterface $context,
DenormalizationFieldMappingInterface $mapping,
$object
): bool {
if (!is_callable([$mapping, 'getPolicy'])) {
return true;
}

return $mapping->getPolicy()->isCompliant($context, $object);
}

/**
* @param DenormalizerContextInterface $context
* @param DenormalizationFieldMappingInterface $fieldMapping
Expand All @@ -198,6 +222,15 @@ private function isWithinGroup(
return true;
}

@trigger_error(
sprintf(
'Use "%s" instead of "%s::setGroups"',
GroupPolicy::class,
DenormalizerContextInterface::class
),
E_USER_DEPRECATED
);

foreach ($fieldMapping->getGroups() as $group) {
if (in_array($group, $groups, true)) {
return true;
Expand Down
51 changes: 50 additions & 1 deletion src/Denormalizer/DenormalizerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class DenormalizerContext implements DenormalizerContextInterface
private $allowedAdditionalFields;

/**
* @deprecated
*
* @var string[]
*/
private $groups = [];
Expand All @@ -28,17 +30,24 @@ final class DenormalizerContext implements DenormalizerContextInterface
*/
private $resetMissingFields;

/**
* @var array
*/
private $attributes;

/**
* @param array|null $allowedAdditionalFields
* @param string[] $groups
* @param ServerRequestInterface|null $request
* @param bool $resetMissingFields
* @param array $attributes
*/
public function __construct(
array $allowedAdditionalFields = null,
array $groups = [],
ServerRequestInterface $request = null,
bool $resetMissingFields = false
bool $resetMissingFields = false,
array $attributes = []
) {
$this->allowedAdditionalFields = $allowedAdditionalFields;
$this->groups = $groups;
Expand All @@ -52,6 +61,7 @@ public function __construct(
}

$this->resetMissingFields = $resetMissingFields;
$this->attributes = $attributes;
}

/**
Expand All @@ -63,6 +73,8 @@ public function getAllowedAdditionalFields()
}

/**
* @deprecated
*
* @return string[]
*/
public function getGroups(): array
Expand All @@ -87,4 +99,41 @@ public function isResetMissingFields(): bool
{
return $this->resetMissingFields;
}

/**
* @return array
*/
public function getAttributes(): array
{
return $this->attributes;
}

/**
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getAttribute(string $name, $default = null)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}

return $default;
}

/**
* @param string $name
* @param mixed $value
*
* @return DenormalizerContextInterface
*/
public function withAttribute(string $name, $value): DenormalizerContextInterface
{
$context = clone $this;
$context->attributes[$name] = $value;

return $context;
}
}
24 changes: 23 additions & 1 deletion src/Denormalizer/DenormalizerContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class DenormalizerContextBuilder implements DenormalizerContextBuilderInte
private $allowedAdditionalFields;

/**
* @deprecated
*
* @var string[]
*/
private $groups = [];
Expand All @@ -28,6 +30,11 @@ final class DenormalizerContextBuilder implements DenormalizerContextBuilderInte
*/
private $resetMissingFields = false;

/**
* @var array
*/
private $attributes = [];

private function __construct()
{
}
Expand All @@ -54,6 +61,8 @@ public function setAllowedAdditionalFields(
}

/**
* @deprecated
*
* @param string[] $groups
*
* @return DenormalizerContextBuilderInterface
Expand Down Expand Up @@ -96,6 +105,18 @@ public function setResetMissingFields(bool $resetMissingFields): DenormalizerCon
return $this;
}

/**
* @param array $attributes
*
* @return DenormalizerContextBuilderInterface
*/
public function setAttributes(array $attributes): DenormalizerContextBuilderInterface
{
$this->attributes = $attributes;

return $this;
}

/**
* @return DenormalizerContextInterface
*/
Expand All @@ -105,7 +126,8 @@ public function getContext(): DenormalizerContextInterface
$this->allowedAdditionalFields,
$this->groups,
$this->request,
$this->resetMissingFields
$this->resetMissingFields,
$this->attributes
);
}
}
17 changes: 11 additions & 6 deletions src/Denormalizer/DenormalizerContextBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Psr\Http\Message\ServerRequestInterface;

/**
* @method setAttributes(array $attributes): self
*/
interface DenormalizerContextBuilderInterface
{
/**
Expand All @@ -21,6 +24,8 @@ public static function create(): self;
public function setAllowedAdditionalFields(array $allowedAdditionalFields = null): self;

/**
* @deprecated
*
* @param string[] $groups
*
* @return self
Expand All @@ -34,12 +39,12 @@ public function setGroups(array $groups): self;
*/
public function setRequest(ServerRequestInterface $request = null): self;

// /**
// * @param bool $resetMissingFields
// *
// * @return self
// */
// public function setResetMissingFields(bool $resetMissingFields): self;
/**
* @param array $attributes
*
* @return self
*/
//public function setAttributes(array $attributes): self;

/**
* @return DenormalizerContextInterface
Expand Down
30 changes: 26 additions & 4 deletions src/Denormalizer/DenormalizerContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

use Psr\Http\Message\ServerRequestInterface;

/**
* @method getAttributes(): array
* @method getAttribute(string $name, $default = null)
* @method withAttribute(string $name, $value): self
*/
interface DenormalizerContextInterface
{
/**
Expand All @@ -14,6 +19,8 @@ interface DenormalizerContextInterface
public function getAllowedAdditionalFields();

/**
* @deprecated
*
* @return string[]
*/
public function getGroups(): array;
Expand All @@ -23,8 +30,23 @@ public function getGroups(): array;
*/
public function getRequest();

// /**
// * @return bool
// */
// public function isResetMissingFields(): bool;
/*
* @return array
*/
//public function getAttributes(): array;

/*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
//public function getAttribute(string $name, $default = null);

/*
* @param string $name
* @param mixed $value
* @return self
*/
//public function withAttribute(string $name, $value): self;
}
29 changes: 27 additions & 2 deletions src/Mapping/DenormalizationFieldMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Chubbyphp\Deserialization\Mapping;

use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
use Chubbyphp\Deserialization\Policy\NullPolicy;
use Chubbyphp\Deserialization\Policy\PolicyInterface;

final class DenormalizationFieldMapping implements DenormalizationFieldMappingInterface
{
Expand All @@ -14,6 +16,8 @@ final class DenormalizationFieldMapping implements DenormalizationFieldMappingIn
private $name;

/**
* @deprecated
*
* @var array
*/
private $groups;
Expand All @@ -23,16 +27,27 @@ final class DenormalizationFieldMapping implements DenormalizationFieldMappingIn
*/
private $fieldDenormalizer;

/**
* @var PolicyInterface
*/
private $policy;

/**
* @param string $name
* @param array $groups
* @param FieldDenormalizerInterface $fieldDenormalizer
* @param PolicyInterface|null $policy
*/
public function __construct($name, array $groups = [], FieldDenormalizerInterface $fieldDenormalizer)
{
public function __construct(
$name,
array $groups = [],
FieldDenormalizerInterface $fieldDenormalizer,
PolicyInterface $policy = null
) {
$this->name = $name;
$this->groups = $groups;
$this->fieldDenormalizer = $fieldDenormalizer;
$this->policy = $policy ?? new NullPolicy();
}

/**
Expand All @@ -44,6 +59,8 @@ public function getName(): string
}

/**
* @deprecated
*
* @return array
*/
public function getGroups(): array
Expand All @@ -58,4 +75,12 @@ public function getFieldDenormalizer(): FieldDenormalizerInterface
{
return $this->fieldDenormalizer;
}

/**
* @return PolicyInterface
*/
public function getPolicy(): PolicyInterface
{
return $this->policy;
}
}