|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright 2013 Johannes M. Schmitt <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace JMS\Serializer\Exclusion; |
|
22
|
|
|
|
|
23
|
|
|
use JMS\Serializer\Context; |
|
24
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
25
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
|
26
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
27
|
|
|
|
|
28
|
|
|
final class GroupsExclusionStrategy implements ExclusionStrategyInterface |
|
29
|
|
|
{ |
|
30
|
|
|
const DEFAULT_GROUP = 'Default'; |
|
31
|
|
|
|
|
32
|
|
|
private $groups = []; |
|
33
|
|
|
private $nestedGroups = false; |
|
34
|
|
|
|
|
35
|
33 |
|
public function __construct(array $groups) |
|
36
|
|
|
{ |
|
37
|
33 |
|
if (empty($groups)) { |
|
38
|
4 |
|
$groups = [self::DEFAULT_GROUP]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
33 |
|
foreach ($groups as $group) { |
|
42
|
33 |
|
if (is_array($group)) { |
|
43
|
2 |
|
$this->nestedGroups = true; |
|
44
|
33 |
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
33 |
|
if ($this->nestedGroups) { |
|
49
|
2 |
|
$this->groups = $groups; |
|
50
|
|
|
} else { |
|
51
|
31 |
|
foreach ($groups as $group) { |
|
52
|
31 |
|
$this->groups[$group] = true; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
33 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
15 |
|
public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool |
|
61
|
|
|
{ |
|
62
|
15 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritDoc} |
|
67
|
|
|
*/ |
|
68
|
33 |
|
public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool |
|
69
|
|
|
{ |
|
70
|
33 |
|
if ($this->nestedGroups) { |
|
71
|
2 |
|
$groups = $this->getGroupsFor($navigatorContext); |
|
72
|
|
|
|
|
73
|
2 |
|
if (!$property->groups) { |
|
74
|
2 |
|
return !in_array(self::DEFAULT_GROUP, $groups); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return $this->shouldSkipUsingGroups($property, $groups); |
|
78
|
|
|
} else { |
|
79
|
|
|
|
|
80
|
31 |
|
if (!$property->groups) { |
|
81
|
9 |
|
return !isset($this->groups[self::DEFAULT_GROUP]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
29 |
|
foreach ($property->groups as $group) { |
|
85
|
29 |
|
if (isset($this->groups[$group])) { |
|
86
|
29 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
18 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
private function shouldSkipUsingGroups(PropertyMetadata $property, $groups) |
|
94
|
|
|
{ |
|
95
|
2 |
|
foreach ($property->groups as $group) { |
|
96
|
2 |
|
if (in_array($group, $groups)) { |
|
97
|
2 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
private function getGroupsFor(Context $navigatorContext) |
|
105
|
|
|
{ |
|
106
|
2 |
|
$paths = $navigatorContext->getCurrentPath(); |
|
107
|
|
|
|
|
108
|
2 |
|
$groups = $this->groups; |
|
109
|
2 |
|
foreach ($paths as $index => $path) { |
|
110
|
2 |
|
if (!array_key_exists($path, $groups)) { |
|
111
|
2 |
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
if (!is_array($groups[$path])) { |
|
115
|
|
|
throw new RuntimeException(sprintf('The group value for the property path "%s" should be an array, "%s" given', $index, gettype($groups[$path]))); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
$groups = $groups[$path]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
return $groups; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|