1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of sensorario/resources repository |
5
|
|
|
* |
6
|
|
|
* (c) Simone Gentili <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sensorario\Resources; |
13
|
|
|
|
14
|
|
|
class Container |
15
|
|
|
{ |
16
|
|
|
private $resources; |
17
|
|
|
|
18
|
|
|
private $rewrites; |
19
|
|
|
|
20
|
|
|
private $allowed = [ |
21
|
|
|
'allowed', |
22
|
|
|
'allowedRanges', |
23
|
|
|
'allowedValues', |
24
|
|
|
'defaults', |
25
|
|
|
'mandatory', |
26
|
|
|
'rules', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private $globals; |
30
|
|
|
|
31
|
|
|
private $resourceConstraints; |
32
|
|
|
|
33
|
32 |
|
public function __construct(array $resources) |
34
|
|
|
{ |
35
|
32 |
|
$this->ensureResourcesItemIsDefined($resources); |
36
|
|
|
|
37
|
31 |
|
$this->rewrites = []; |
38
|
31 |
|
$this->globals = []; |
39
|
|
|
|
40
|
31 |
|
$this->ensureValidConstraints($resources); |
41
|
|
|
|
42
|
30 |
|
$this->fillInRewrites($resources['resources']); |
43
|
|
|
|
44
|
30 |
|
if (isset($resources['globals'])) { |
45
|
1 |
|
$this->globals = $resources['globals']; |
46
|
|
|
} |
47
|
|
|
|
48
|
30 |
|
$this->resources = $resources; |
49
|
30 |
|
} |
50
|
|
|
|
51
|
31 |
|
private function ensureValidConstraints($resources) |
52
|
|
|
{ |
53
|
31 |
|
foreach ($resources['resources'] as $item) { |
54
|
30 |
|
if (isset($item['constraints'])) { |
55
|
30 |
|
foreach ($item['constraints'] as $name => $value) { |
56
|
30 |
|
$this->ensureNameIsAllowed($name, $value); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
30 |
|
} |
61
|
|
|
|
62
|
27 |
|
public function ensureNameIsAllowed($name, $value) |
63
|
|
|
{ |
64
|
27 |
|
if (!in_array($name, $this->allowed)) { |
65
|
1 |
|
throw new \Sensorario\Resources\Exceptions\InvalidConstraintException( |
66
|
|
|
'Invalid constraint: ' |
67
|
1 |
|
. 'name ' . $name |
68
|
1 |
|
. '; value ' . $value |
69
|
|
|
); |
70
|
|
|
} |
71
|
26 |
|
} |
72
|
|
|
|
73
|
2 |
|
public function countResources() |
74
|
|
|
{ |
75
|
2 |
|
return count( |
76
|
2 |
|
$this->resources['resources'] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
27 |
|
private function getResourceConstraints($resource) |
81
|
|
|
{ |
82
|
27 |
|
if (!$this->resourceConstraints) { |
83
|
27 |
|
$this->resourceConstraints = $this->resources['resources'][$resource]['constraints']; |
84
|
|
|
} |
85
|
|
|
|
86
|
27 |
|
return $this->resourceConstraints; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function create($resource, array $constraints) |
90
|
|
|
{ |
91
|
2 |
|
foreach ($constraints as $name => $value) { |
92
|
|
|
if ( |
93
|
2 |
|
!isset($this->getResourceConstraints($resource)['allowed']) |
94
|
1 |
|
|| !in_array($name, $this->getResourceConstraints($resource)['allowed']) |
95
|
|
|
) { |
96
|
1 |
|
throw new \Sensorario\Resources\Exceptions\NotAllowedConstraintException( |
97
|
1 |
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
25 |
|
public function allowed($resource) |
106
|
|
|
{ |
107
|
25 |
|
$allowed = []; |
108
|
|
|
|
109
|
25 |
|
foreach ($this->allowed as $item) { |
110
|
25 |
|
if (isset($this->getResourceConstraints($resource)[$item])) { |
111
|
24 |
|
$allowed = array_merge( |
112
|
24 |
|
$allowed, |
113
|
24 |
|
$this->getResourceConstraints($resource)[$item] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
25 |
|
return $allowed; |
119
|
|
|
} |
120
|
|
|
|
121
|
22 |
|
public function getConstraints($constraint, $resource) : array |
122
|
|
|
{ |
123
|
22 |
|
if (isset($this->getResourceConstraints($resource)[$constraint])) { |
124
|
18 |
|
return $this->getResourceConstraints($resource)[$constraint]; |
125
|
|
|
} |
126
|
|
|
|
127
|
22 |
|
return []; |
128
|
|
|
} |
129
|
|
|
|
130
|
23 |
|
public function rewrites() |
131
|
|
|
{ |
132
|
23 |
|
return $this->rewrites; |
133
|
|
|
} |
134
|
|
|
|
135
|
22 |
|
public function globals() |
136
|
|
|
{ |
137
|
22 |
|
return $this->globals; |
138
|
|
|
} |
139
|
|
|
|
140
|
22 |
|
public function mandatory($resource) |
141
|
|
|
{ |
142
|
22 |
|
return $this->getConstraints( |
143
|
22 |
|
'mandatory', |
144
|
22 |
|
$resource |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
22 |
|
public function defaults($resource) |
149
|
|
|
{ |
150
|
22 |
|
return $this->getConstraints( |
151
|
22 |
|
'defaults', |
152
|
22 |
|
$resource |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
22 |
|
public function rules($resource) |
157
|
|
|
{ |
158
|
22 |
|
return $this->getConstraints( |
159
|
22 |
|
'rules', |
160
|
22 |
|
$resource |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
22 |
|
public function allowedValues($resource) |
165
|
|
|
{ |
166
|
22 |
|
return $this->getConstraints( |
167
|
22 |
|
'allowedValues', |
168
|
22 |
|
$resource |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
22 |
|
public function ranges($resource) |
173
|
|
|
{ |
174
|
22 |
|
return $this->getConstraints( |
175
|
22 |
|
'allowedRanges', |
176
|
22 |
|
$resource |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
32 |
|
public function ensureResourcesItemIsDefined($resources) |
181
|
|
|
{ |
182
|
32 |
|
if (!isset($resources['resources'])) { |
183
|
1 |
|
throw new \Sensorario\Resources\Exceptions\EmptyConfigurationException( |
184
|
1 |
|
'resources element is not defined' |
185
|
|
|
); |
186
|
|
|
} |
187
|
31 |
|
} |
188
|
|
|
|
189
|
30 |
|
public function fillInRewrites(array $resources) |
190
|
|
|
{ |
191
|
30 |
|
foreach ($resources as $item) { |
192
|
29 |
|
if (isset($item['rewrite'])) { |
193
|
2 |
|
foreach ($item['rewrite'] as $field => $rewriteRule) { |
194
|
2 |
|
$this->rewrites[$field] = $rewriteRule; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
30 |
|
} |
199
|
|
|
} |
200
|
|
|
|