Passed
Pull Request — master (#1474)
by
unknown
03:11
created

GroupsExclusionStrategy   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 112
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0
wmc 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getGroupsFor() 0 26 8
A setInheritedGroups() 0 5 1
A __construct() 0 18 6
B shouldSkipProperty() 0 22 7
A shouldSkipUsingGroups() 0 9 3
A shouldSkipClass() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Exclusion;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\Metadata\ClassMetadata;
9
use JMS\Serializer\Metadata\PropertyMetadata;
10
11
final class GroupsExclusionStrategy implements ExclusionStrategyInterface
12
{
13
    public const DEFAULT_GROUP = 'Default';
14
15
    /**
16
     * @var array
17
     */
18
    private $groups = [];
19 33
20
    /**
21 33
     * @var bool
22 4
     */
23
    private $nestedGroups = false;
24
25 33
    private $inheritedGroups = false;
26 33
27 2
    public function __construct(array $groups)
28 33
    {
29
        if (empty($groups)) {
30
            $groups = [self::DEFAULT_GROUP];
31
        }
32 33
33 2
        foreach ($groups as $group) {
34
            if (is_array($group)) {
35 31
                $this->nestedGroups = true;
36 31
                break;
37
            }
38
        }
39 33
40
        if ($this->nestedGroups) {
41
            $this->groups = $groups;
42
        } else {
43
            foreach ($groups as $group) {
44 15
                $this->groups[$group] = true;
45
            }
46 15
        }
47
    }
48
49
    public function setInheritedGroups(bool $inheritedGroups): self
50
    {
51
        $this->inheritedGroups = $inheritedGroups;
52 33
53
        return $this;
54 33
    }
55 2
56
    public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool
57 2
    {
58 2
        return false;
59
    }
60
61 2
    public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool
62
    {
63
        if ($this->nestedGroups) {
64 31
            $groups = $this->getGroupsFor($navigatorContext);
65 9
66
            if (!$property->groups) {
67
                return !in_array(self::DEFAULT_GROUP, $groups);
68 29
            }
69 29
70 29
            return $this->shouldSkipUsingGroups($property, $groups);
71
        } else {
72
            if (!$property->groups) {
73 18
                return !isset($this->groups[self::DEFAULT_GROUP]);
74
            }
75
76
            foreach ($property->groups as $group) {
77 2
                if (is_scalar($group) && isset($this->groups[$group])) {
78
                    return false;
79 2
                }
80 2
            }
81 2
82
            return true;
83
        }
84
    }
85 2
86
    private function shouldSkipUsingGroups(PropertyMetadata $property, array $groups): bool
87
    {
88 2
        foreach ($property->groups as $group) {
89
            if (in_array($group, $groups)) {
90 2
                return false;
91
            }
92 2
        }
93 2
94 2
        return true;
95 2
    }
96
97
    public function getGroupsFor(Context $navigatorContext): array
98 2
    {
99
        if (!$this->nestedGroups) {
100
            return array_keys($this->groups);
101
        }
102 2
103
        $paths = $navigatorContext->getCurrentPath();
104
        $groups = $this->groups;
105 2
        foreach ($paths as $index => $path) {
106
            if (!array_key_exists($path, $groups)) {
107
                if ($index > 0) {
108
                    $groups = $this->inheritedGroups ? $groups : [self::DEFAULT_GROUP];
109
                } else {
110
                    $groups = array_filter($groups, 'is_string') ?: [self::DEFAULT_GROUP];
111
                }
112
113
                break;
114
            }
115
116
            $groups = $groups[$path];
117
            if (!array_filter($groups, 'is_string')) {
118
                $groups += [self::DEFAULT_GROUP];
119
            }
120
        }
121
122
        return $groups;
123
    }
124
}
125