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 $rewrite = []; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public function __construct(array $resources) |
24
|
|
|
{ |
25
|
|
|
if (!isset($resources['resources'])) { |
26
|
|
|
throw new RuntimeException( |
27
|
|
|
'resources element is not defined' |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->rewrites = []; |
|
|
|
|
32
|
|
|
$this->globals = []; |
|
|
|
|
33
|
|
|
|
34
|
|
|
foreach ($resources['resources'] as $item) { |
35
|
|
|
if (isset($item['constraints'])) { |
36
|
|
|
foreach ($item['constraints'] as $name => $value) { |
37
|
|
|
if (!in_array($name, $this->allowed)) { |
38
|
|
|
throw new RuntimeException( |
39
|
|
|
'Invalid constraint: ' |
40
|
|
|
. 'name ' . $name |
41
|
|
|
. '; value ' . $value |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($resources['resources'] as $item) { |
49
|
|
|
if (isset($item['rewrite'])) { |
50
|
|
|
foreach ($item['rewrite'] as $field => $rewriteRule) { |
51
|
|
|
$this->rewrites[$field] = $rewriteRule; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (isset($resources['globals'])) { |
57
|
|
|
$this->globals = $resources['globals']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->resources = $resources; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function countResources() |
64
|
|
|
{ |
65
|
|
|
return count( |
66
|
|
|
$this->resources['resources'] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function create($resource, array $constraints) |
71
|
|
|
{ |
72
|
|
|
foreach ($constraints as $name => $value) { |
73
|
|
View Code Duplication |
if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
74
|
|
|
throw new RuntimeException( |
75
|
|
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
80
|
|
|
throw new RuntimeException( |
81
|
|
|
'Not allowed `' . $name . '` constraint with value `' . $value . '`' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function allowed($resource) |
88
|
|
|
{ |
89
|
|
|
$allowed = []; |
90
|
|
|
|
91
|
|
|
foreach ($this->allowed as $item) { |
92
|
|
View Code Duplication |
if (isset($this->resources['resources'][$resource]['constraints'][$item])) { |
|
|
|
|
93
|
|
|
$allowed = array_merge( |
94
|
|
|
$allowed, |
95
|
|
|
$this->resources['resources'][$resource]['constraints'][$item] |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $allowed; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getConstraints( |
104
|
|
|
$constraintName, |
105
|
|
|
$resource |
106
|
|
|
) { |
107
|
|
View Code Duplication |
if (isset($this->resources['resources'][$resource]['constraints'][$constraintName])) { |
|
|
|
|
108
|
|
|
return $this->resources['resources'][$resource]['constraints'][$constraintName]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function rewrites() |
115
|
|
|
{ |
116
|
|
|
return $this->rewrites; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function globals() |
120
|
|
|
{ |
121
|
|
|
return $this->globals; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|