Passed
Pull Request — master (#15)
by
unknown
02:23
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\Serialization\Policy;
6
7
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
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
    private $groups;
20
21
    /**
22
     * @param array $groups
23
     */
24 4
    public function __construct(array $groups)
25
    {
26 4
        $this->groups = $groups;
27 4
    }
28
29
    /**
30
     * @param NormalizerContextInterface $context
31
     * @param object|mixed               $object
32
     *
33
     * @return bool
34
     */
35 4
    public function isCompliant(NormalizerContextInterface $context, $object): bool
36
    {
37 4
        if ([] === $this->groups) {
38 1
            return true;
39
        }
40
41 3
        $groups = $context->getAttribute(self::ATTRIBUTE_GROUPS, []);
42
43 3
        foreach ($this->groups as $group) {
44 3
            if (in_array($group, $groups, true)) {
45 3
                return true;
46
            }
47
        }
48
49 2
        return false;
50
    }
51
}
52