|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sensorario\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use \RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
class Container |
|
8
|
|
|
extends ContainerBase |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
private $resources; |
|
11
|
|
|
|
|
12
|
|
|
private $allowed = [ |
|
13
|
|
|
'allowed', |
|
14
|
|
|
'allowedRanges', |
|
15
|
|
|
'allowedValues', |
|
16
|
|
|
'defaults', |
|
17
|
|
|
'mandatory', |
|
18
|
|
|
'rules', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
private function ensureValidConstraints($resources) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($resources['resources'] as $item) { |
|
24
|
|
|
if (isset($item['constraints'])) { |
|
25
|
|
|
foreach ($item['constraints'] as $name => $value) { |
|
26
|
|
|
if (!in_array($name, $this->allowed)) { |
|
27
|
|
|
throw new RuntimeException( |
|
28
|
|
|
'Invalid constraint: ' |
|
29
|
|
|
. 'name ' . $name |
|
30
|
|
|
. '; value ' . $value |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(array $resources) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!isset($resources['resources'])) { |
|
41
|
|
|
throw new RuntimeException( |
|
42
|
|
|
'resources element is not defined' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->rewrites = []; |
|
|
|
|
|
|
47
|
|
|
$this->globals = []; |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$this->ensureValidConstraints($resources); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($resources['resources'] as $item) { |
|
52
|
|
|
if (isset($item['rewrite'])) { |
|
53
|
|
|
foreach ($item['rewrite'] as $field => $rewriteRule) { |
|
54
|
|
|
$this->rewrites[$field] = $rewriteRule; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (isset($resources['globals'])) { |
|
60
|
|
|
$this->globals = $resources['globals']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->resources = $resources; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function countResources() |
|
67
|
|
|
{ |
|
68
|
|
|
return count( |
|
69
|
|
|
$this->resources['resources'] |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function create($resource, array $constraints) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($constraints as $name => $value) { |
|
76
|
|
View Code Duplication |
if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
|
|
77
|
|
|
throw new RuntimeException( |
|
78
|
|
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
|
|
83
|
|
|
throw new RuntimeException( |
|
84
|
|
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function allowed($resource) |
|
91
|
|
|
{ |
|
92
|
|
|
$allowed = []; |
|
93
|
|
|
|
|
94
|
|
|
foreach ($this->allowed as $item) { |
|
95
|
|
View Code Duplication |
if (isset($this->resources['resources'][$resource]['constraints'][$item])) { |
|
|
|
|
|
|
96
|
|
|
$allowed = array_merge( |
|
97
|
|
|
$allowed, |
|
98
|
|
|
$this->resources['resources'][$resource]['constraints'][$item] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $allowed; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getConstraints( |
|
107
|
|
|
$constraintName, |
|
108
|
|
|
$resource |
|
109
|
|
|
) { |
|
110
|
|
View Code Duplication |
if (isset($this->resources['resources'][$resource]['constraints'][$constraintName])) { |
|
|
|
|
|
|
111
|
|
|
return $this->resources['resources'][$resource]['constraints'][$constraintName]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return []; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function rewrites() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->rewrites; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function globals() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->globals; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|