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
|
27 |
|
if (!in_array($name, $this->allowed)) { |
57
|
1 |
|
throw new \Sensorario\Resources\Exceptions\InvalidConstraintException( |
58
|
|
|
'Invalid constraint: ' |
59
|
1 |
|
. 'name ' . $name |
60
|
30 |
|
. '; value ' . $value |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
30 |
|
} |
67
|
|
|
|
68
|
2 |
|
public function countResources() |
69
|
|
|
{ |
70
|
2 |
|
return count( |
71
|
2 |
|
$this->resources['resources'] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
27 |
|
private function getResourceConstraints($resource) |
76
|
|
|
{ |
77
|
27 |
|
if (!$this->resourceConstraints) { |
78
|
27 |
|
$this->resourceConstraints = $this->resources['resources'][$resource]['constraints']; |
79
|
|
|
} |
80
|
|
|
|
81
|
27 |
|
return $this->resourceConstraints; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function create($resource, array $constraints) |
85
|
|
|
{ |
86
|
2 |
|
foreach ($constraints as $name => $value) { |
87
|
|
|
if ( |
88
|
2 |
|
!isset($this->getResourceConstraints($resource)['allowed']) |
89
|
1 |
|
|| !in_array($name, $this->getResourceConstraints($resource)['allowed']) |
90
|
|
|
) { |
91
|
1 |
|
throw new \Sensorario\Resources\Exceptions\NotAllowedConstraintException( |
92
|
1 |
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
25 |
|
public function allowed($resource) |
101
|
|
|
{ |
102
|
25 |
|
$allowed = []; |
103
|
|
|
|
104
|
25 |
|
foreach ($this->allowed as $item) { |
105
|
25 |
|
if (isset($this->getResourceConstraints($resource)[$item])) { |
106
|
24 |
|
$allowed = array_merge( |
107
|
24 |
|
$allowed, |
108
|
24 |
|
$this->getResourceConstraints($resource)[$item] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
25 |
|
return $allowed; |
114
|
|
|
} |
115
|
|
|
|
116
|
22 |
|
public function getConstraints($constraint, $resource) : array |
117
|
|
|
{ |
118
|
22 |
|
if (isset($this->getResourceConstraints($resource)[$constraint])) { |
119
|
18 |
|
return $this->getResourceConstraints($resource)[$constraint]; |
120
|
|
|
} |
121
|
|
|
|
122
|
22 |
|
return []; |
123
|
|
|
} |
124
|
|
|
|
125
|
23 |
|
public function rewrites() |
126
|
|
|
{ |
127
|
23 |
|
return $this->rewrites; |
128
|
|
|
} |
129
|
|
|
|
130
|
22 |
|
public function globals() |
131
|
|
|
{ |
132
|
22 |
|
return $this->globals; |
133
|
|
|
} |
134
|
|
|
|
135
|
22 |
|
public function mandatory($resource) |
136
|
|
|
{ |
137
|
22 |
|
return $this->getConstraints( |
138
|
22 |
|
'mandatory', |
139
|
22 |
|
$resource |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
22 |
|
public function defaults($resource) |
144
|
|
|
{ |
145
|
22 |
|
return $this->getConstraints( |
146
|
22 |
|
'defaults', |
147
|
22 |
|
$resource |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
22 |
|
public function rules($resource) |
152
|
|
|
{ |
153
|
22 |
|
return $this->getConstraints( |
154
|
22 |
|
'rules', |
155
|
22 |
|
$resource |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
22 |
|
public function allowedValues($resource) |
160
|
|
|
{ |
161
|
22 |
|
return $this->getConstraints( |
162
|
22 |
|
'allowedValues', |
163
|
22 |
|
$resource |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
22 |
|
public function ranges($resource) |
168
|
|
|
{ |
169
|
22 |
|
return $this->getConstraints( |
170
|
22 |
|
'allowedRanges', |
171
|
22 |
|
$resource |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
32 |
|
public function ensureResourcesItemIsDefined($resources) |
176
|
|
|
{ |
177
|
32 |
|
if (!isset($resources['resources'])) { |
178
|
1 |
|
throw new \Sensorario\Resources\Exceptions\EmptyConfigurationException( |
179
|
1 |
|
'resources element is not defined' |
180
|
|
|
); |
181
|
|
|
} |
182
|
31 |
|
} |
183
|
|
|
|
184
|
30 |
|
public function fillInRewrites(array $resources) |
185
|
|
|
{ |
186
|
30 |
|
foreach ($resources as $item) { |
187
|
29 |
|
if (isset($item['rewrite'])) { |
188
|
2 |
|
foreach ($item['rewrite'] as $field => $rewriteRule) { |
189
|
2 |
|
$this->rewrites[$field] = $rewriteRule; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
30 |
|
} |
194
|
|
|
} |
195
|
|
|
|