Passed
Push — master ( 265f32...0e6955 )
by Dominik
02:02 queued 12s
created

GroupPolicy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
    /**
27
     * @param array $groups
28
     */
29 5
    public function __construct(array $groups = [self::GROUP_DEFAULT])
30
    {
31 5
        $this->groups = $groups;
32 5
    }
33
34
    /**
35
     * @param DenormalizerContextInterface $context
36
     * @param object|mixed                 $object
37
     *
38
     * @return bool
39
     */
40 5
    public function isCompliant(DenormalizerContextInterface $context, $object): bool
41
    {
42 5
        if ([] === $this->groups) {
43 1
            return true;
44
        }
45
46 4
        $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, [self::GROUP_DEFAULT]);
47
48 4
        foreach ($this->groups as $group) {
49 4
            if (in_array($group, $contextGroups, true)) {
50 4
                return true;
51
            }
52
        }
53
54 2
        return false;
55
    }
56
}
57