1 | <?php |
||
49 | class PropertyList implements PropertyListInterface |
||
50 | { |
||
51 | /** |
||
52 | * Property values |
||
53 | * |
||
54 | * @var array[] |
||
55 | */ |
||
56 | protected $values = []; |
||
57 | /** |
||
58 | * Property names |
||
59 | * |
||
60 | * @var \stdClass[] |
||
61 | */ |
||
62 | protected $names = []; |
||
63 | /** |
||
64 | * Name cursor mapping |
||
65 | * |
||
66 | * @var int[] |
||
67 | */ |
||
68 | protected $nameToCursor = []; |
||
69 | /** |
||
70 | * Internal cursor |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $cursor = 0; |
||
75 | |||
76 | /** |
||
77 | * Unset a property |
||
78 | * |
||
79 | * @param \stdClass|string $iri IRI |
||
80 | * @throws ErrorException |
||
81 | */ |
||
82 | 1 | public function offsetUnset($iri) |
|
83 | { |
||
84 | 1 | throw new ErrorException( |
|
85 | 1 | sprintf(ErrorException::CANNOT_UNSET_PROPERTY_STR, $iri), |
|
86 | 1 | ErrorException::CANNOT_UNSET_PROPERTY, |
|
87 | 1 | E_WARNING |
|
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return the number of properties |
||
93 | * |
||
94 | * @return int Number of properties |
||
95 | */ |
||
96 | 2 | public function count() |
|
97 | { |
||
98 | 2 | return count($this->values); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Return the current property values |
||
103 | * |
||
104 | * @return array Property values |
||
105 | */ |
||
106 | 8 | public function current() |
|
107 | { |
||
108 | 8 | return $this->values[$this->cursor]; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Move forward to next element |
||
113 | */ |
||
114 | 8 | public function next() |
|
115 | { |
||
116 | 8 | ++$this->cursor; |
|
117 | 8 | } |
|
118 | |||
119 | /** |
||
120 | * Return the current IRI key |
||
121 | * |
||
122 | * @return \stdClass IRI key |
||
123 | */ |
||
124 | 8 | public function key() |
|
125 | { |
||
126 | 8 | return $this->names[$this->cursor]; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Checks if current position is valid |
||
131 | * |
||
132 | * @return boolean The current position is valid |
||
133 | */ |
||
134 | 17 | public function valid() |
|
135 | { |
||
136 | 17 | return isset($this->values[$this->cursor]); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Rewind the Iterator to the first element |
||
141 | */ |
||
142 | 17 | public function rewind() |
|
143 | { |
||
144 | 17 | $this->cursor = 0; |
|
145 | 17 | } |
|
146 | |||
147 | /** |
||
148 | * Add a property |
||
149 | * |
||
150 | * @param \stdClass|Iri $property Property |
||
151 | */ |
||
152 | 37 | public function add($property) |
|
166 | |||
167 | /** |
||
168 | * Return whether a property exists |
||
169 | * |
||
170 | * @param \stdClass|Iri|string $iri IRI |
||
171 | * @return boolean Property exists |
||
172 | */ |
||
173 | 37 | public function offsetExists($iri) |
|
184 | |||
185 | /** |
||
186 | * Get a particular property cursor by its profiled name |
||
187 | * |
||
188 | * @param Iri $iri IRI |
||
189 | * @return int Property cursor |
||
190 | * @throws OutOfBoundsException If the property name is unknown |
||
191 | */ |
||
192 | 31 | protected function getProfiledPropertyCursor($iri) |
|
193 | { |
||
194 | 31 | $iriStr = strval($iri); |
|
195 | |||
196 | // If the property name is unknown |
||
197 | 31 | if (!isset($this->nameToCursor[$iriStr])) { |
|
198 | 31 | $this->handleUnknownName($iriStr); |
|
199 | } |
||
200 | |||
201 | 6 | return $this->nameToCursor[$iriStr]; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Handle an unknown property name |
||
206 | * |
||
207 | * @param string $name Property name |
||
208 | * @throws OutOfBoundsException If the property name is unknown |
||
209 | */ |
||
210 | 40 | protected function handleUnknownName($name) |
|
217 | |||
218 | /** |
||
219 | * Get a particular property cursor by its name |
||
220 | * |
||
221 | * @param string $name Property name |
||
222 | * @return int Property cursor |
||
223 | */ |
||
224 | 10 | protected function getPropertyCursor($name) |
|
225 | { |
||
226 | // Run through all property names |
||
227 | 10 | foreach ($this->names as $cursor => $iri) { |
|
228 | 5 | if (in_array($name, [$iri->name, strval($iri)])) { |
|
229 | 5 | return $cursor; |
|
230 | } |
||
231 | } |
||
232 | |||
233 | 8 | return $this->handleUnknownName($name); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Set a particular property |
||
238 | * |
||
239 | * @param \stdClass|Iri|string $iri IRI |
||
240 | * @param array $value Property values |
||
241 | */ |
||
242 | 9 | public function offsetSet($iri, $value) |
|
251 | |||
252 | /** |
||
253 | * Get a particular property |
||
254 | * |
||
255 | * @param \stdClass|Iri|string $iri IRI |
||
256 | * @return array Property values |
||
257 | * @throws OutOfBoundsException If the property name is unknown |
||
258 | */ |
||
259 | 15 | public function &offsetGet($iri) |
|
266 | } |
||
267 |