1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\HalClient\Resource; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use TomPHP\HalClient\Exception\FieldNotFoundException; |
7
|
|
|
use TomPHP\HalClient\Exception\LinkNotFoundException; |
8
|
|
|
use TomPHP\HalClient\Exception\ResourceNotFoundException; |
9
|
|
|
|
10
|
|
|
final class Resource implements ResourceNode |
11
|
|
|
{ |
12
|
|
|
/** @var array */ |
13
|
|
|
private $fields = []; |
14
|
|
|
|
15
|
|
|
/** @var Link[] */ |
16
|
|
|
private $links = []; |
17
|
|
|
|
18
|
|
|
/** @var Resource[] */ |
19
|
|
|
private $resources = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param FieldNode[] $fields |
23
|
|
|
* @param Link[] $links |
24
|
|
|
* @param ResourceNode[] $resources |
25
|
|
|
*/ |
26
|
|
|
public function __construct(array $fields, array $links = [], array $resources = []) |
27
|
|
|
{ |
28
|
|
|
Assertion::allIsInstanceOf($fields, FieldNode::class); |
29
|
|
|
Assertion::allIsInstanceOf($links, Link::class); |
30
|
|
|
Assertion::allIsInstanceOf($resources, ResourceNode::class); |
31
|
|
|
|
32
|
|
|
$this->fields = $fields; |
33
|
|
|
$this->links = $links; |
34
|
|
|
$this->resources = $resources; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* |
40
|
|
|
* @return FieldNode |
41
|
|
|
*/ |
42
|
|
|
public function __get($name) |
43
|
|
|
{ |
44
|
|
|
return $this->getField($name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* |
50
|
|
|
* @return FieldNode |
51
|
|
|
* |
52
|
|
|
* @throws FieldNotFoundException |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function getField($name) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (!array_key_exists($name, $this->fields)) { |
57
|
|
|
throw new FieldNotFoundException($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->fields[$name]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @return string[] */ |
|
|
|
|
64
|
|
|
public function getLinks() |
65
|
|
|
{ |
66
|
|
|
return array_keys($this->links); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Link |
71
|
|
|
* |
72
|
|
|
* @throws LinkNotFoundException |
73
|
|
|
*/ |
74
|
|
|
public function getLink($name) |
75
|
|
|
{ |
76
|
|
|
if (!array_key_exists($name, $this->links)) { |
77
|
|
|
throw new LinkNotFoundException($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->links[$name]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Resource |
85
|
|
|
*/ |
86
|
|
|
public function getResource($name) |
87
|
|
|
{ |
88
|
|
|
if (!array_key_exists($name, $this->resources)) { |
89
|
|
|
throw new ResourceNotFoundException($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->resources[$name]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function matches($criteria) |
96
|
|
|
{ |
97
|
|
|
foreach ($criteria as $name => $value) { |
98
|
|
|
if (!$this->matchesItem($name, $value)) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|int $name |
108
|
|
|
* @param mixed $value |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
private function matchesItem($name, $value) |
113
|
|
|
{ |
114
|
|
|
if ($this->isResourceSearchCriteria($name, $value)) { |
115
|
|
|
return $this->matchesResource($value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->matchField($name, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string|int $name |
123
|
|
|
* @param mixed $value |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
private function isResourceSearchCriteria($name, $value) |
128
|
|
|
{ |
129
|
|
|
return is_int($name) |
130
|
|
|
&& is_array($value) |
131
|
|
|
&& array_key_exists(0, $value) |
132
|
|
|
&& $value[0] === 'resource'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @return bool */ |
136
|
|
|
private function matchesResource(array $value) |
137
|
|
|
{ |
138
|
|
|
$name = $value[1]; |
|
|
|
|
139
|
|
|
$search = $value[2]; |
140
|
|
|
|
141
|
|
|
if (!array_key_exists($name, $this->resources)) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->resources[$name]->matches($search); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name |
150
|
|
|
* @param mixed $value |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function matchField($name, $value) |
155
|
|
|
{ |
156
|
|
|
if (!array_key_exists($name, $this->fields)) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->fields[$name]->matches($value); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..