chubbyphp /
chubbyphp-deserialization
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Chubbyphp\Deserialization\Policy; |
||
| 6 | |||
| 7 | use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; |
||
| 8 | |||
| 9 | final class GroupPolicy implements PolicyInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | const ATTRIBUTE_GROUPS = 'groups'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | const GROUP_DEFAULT = 'default'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string[] |
||
| 23 | */ |
||
| 24 | private $groups; |
||
| 25 | |||
| 26 | public function __construct(array $groups = [self::GROUP_DEFAULT]) |
||
| 27 | { |
||
| 28 | $this->groups = $groups; |
||
| 29 | 5 | } |
|
| 30 | |||
| 31 | 5 | /** |
|
| 32 | 5 | * @param object|mixed $object |
|
| 33 | */ |
||
| 34 | public function isCompliant(DenormalizerContextInterface $context, $object): bool |
||
| 35 | { |
||
| 36 | if ([] === $this->groups) { |
||
| 37 | return true; |
||
| 38 | } |
||
| 39 | |||
| 40 | 5 | $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, [self::GROUP_DEFAULT]); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 41 | |||
| 42 | 5 | foreach ($this->groups as $group) { |
|
| 43 | 1 | if (in_array($group, $contextGroups, true)) { |
|
| 44 | return true; |
||
| 45 | } |
||
| 46 | 4 | } |
|
| 47 | |||
| 48 | 4 | return false; |
|
| 49 | 4 | } |
|
| 50 | } |
||
| 51 |