1 | <?php |
||
21 | class Hal |
||
22 | { |
||
23 | /** |
||
24 | * The uri represented by this representation. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $uri; |
||
29 | |||
30 | /** |
||
31 | * The data for this resource. An associative array of key value pairs. |
||
32 | * |
||
33 | * array( |
||
34 | * 'price' => 30.00, |
||
35 | * 'colour' => 'blue' |
||
36 | * ) |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $data; |
||
41 | |||
42 | /** |
||
43 | * An array of embedded Hal objects representing embedded resources. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $resources = array(); |
||
48 | |||
49 | /** |
||
50 | * A collection of \Nocarrier\HalLink objects keyed by the link relation to |
||
51 | * this resource. |
||
52 | * |
||
53 | * array( |
||
54 | * 'next' => [HalLink] |
||
55 | * ) |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $links = null; |
||
60 | |||
61 | /** |
||
62 | * A list of rel types for links that will force a rel type to array for one element |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $arrayLinkRels = array(); |
||
67 | |||
68 | /** |
||
69 | * A list of rel types for links that will force a rel type to array for one element |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $arrayResourceRels = array(); |
||
74 | |||
75 | /** |
||
76 | * Whether xml attribute markers should be stripped when rendering |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $shouldStripAttributes = true; |
||
81 | |||
82 | /** |
||
83 | * Construct a new Hal object from an array of data. You can markup the |
||
84 | * $data array with certain keys and values in order to affect the |
||
85 | * generated JSON or XML documents if required to do so. |
||
86 | * |
||
87 | * '@' prefix on any array key will cause the value to be set as an |
||
88 | * attribute on the XML element generated by the parent. i.e, array('x' => |
||
89 | * array('@href' => 'http://url')) will yield <x href='http://url'></x> in |
||
90 | * the XML representation. The @ prefix will be stripped from the JSON |
||
91 | * representation. |
||
92 | * |
||
93 | * Specifying the key 'value' will cause the value of this key to be set as |
||
94 | * the value of the XML element instead of a child. i.e, array('x' => |
||
95 | * array('value' => 'example')) will yield <x>example</x> in the XML |
||
96 | * representation. This will not affect the JSON representation. |
||
97 | * |
||
98 | * @param mixed $uri |
||
99 | * @param array|Traversable $data |
||
100 | * |
||
101 | * @throws \RuntimeException |
||
102 | */ |
||
103 | public function __construct($uri = null, $data = array()) |
||
115 | |||
116 | /** |
||
117 | * Decode a application/hal+json document into a Nocarrier\Hal object. |
||
118 | * |
||
119 | * @param string $data |
||
120 | * @param int $depth |
||
121 | * @static |
||
122 | * @access public |
||
123 | * @return \Nocarrier\Hal |
||
124 | */ |
||
125 | public static function fromJson($data, $depth = 0) |
||
129 | |||
130 | /** |
||
131 | * Decode a application/hal+xml document into a Nocarrier\Hal object. |
||
132 | * |
||
133 | * @param int $depth |
||
134 | * |
||
135 | * @static |
||
136 | * @access public |
||
137 | * @return \Nocarrier\Hal |
||
138 | */ |
||
139 | public static function fromXml($data, $depth = 0) |
||
143 | |||
144 | /** |
||
145 | * Add a link to the resource, identified by $rel, located at $uri. |
||
146 | * |
||
147 | * @param string $rel |
||
148 | * @param string $uri |
||
149 | * @param array $attributes |
||
150 | * Other attributes, as defined by HAL spec and RFC 5988. |
||
151 | * @param bool $forceArray whether to force a rel to be an array if it has only one entry |
||
152 | * @return \Nocarrier\Hal |
||
153 | */ |
||
154 | public function addLink($rel, $uri, array $attributes = array(), $forceArray = false) |
||
158 | |||
159 | /** |
||
160 | * Add a link instance to the resource, identified by $rel. |
||
161 | * |
||
162 | * @param string $rel |
||
163 | * @param HalLink $link |
||
164 | * @param bool $forceArray whether to force a rel to be an array if it has only one entry |
||
165 | * @return \Nocarrier\Hal |
||
166 | */ |
||
167 | public function addHalLink($rel, HalLink $link, $forceArray = false) |
||
177 | |||
178 | /** |
||
179 | * Add an embedded resource, identified by $rel and represented by $resource. |
||
180 | * |
||
181 | * @param string $rel |
||
182 | * @param \Nocarrier\Hal $resource |
||
183 | * |
||
184 | * @return \Nocarrier\Hal |
||
185 | */ |
||
186 | public function addResource($rel, \Nocarrier\Hal $resource = null, $forceArray = true) |
||
202 | |||
203 | /** |
||
204 | * Set an embedded resource, identified by $rel and represented by $resource |
||
205 | * |
||
206 | * Using this method signifies that $rel will only ever be a single object |
||
207 | * (only really relevant to JSON rendering) |
||
208 | * |
||
209 | * @param string $rel |
||
210 | * @param Hal $resource |
||
211 | */ |
||
212 | public function setResource($rel, $resource) |
||
230 | |||
231 | /** |
||
232 | * Set resource's data |
||
233 | */ |
||
234 | public function setData(Array $data = null) |
||
239 | |||
240 | /** |
||
241 | * Return an array of data (key => value pairs) representing this resource. |
||
242 | * |
||
243 | * @param null|string data key |
||
244 | * @return mixed Returns an array if no key is passed in, otherwise returns the data |
||
245 | */ |
||
246 | public function getData($key = null) |
||
254 | |||
255 | /** |
||
256 | * Return an array of Nocarrier\HalLink objects representing resources |
||
257 | * related to this one. |
||
258 | * |
||
259 | * @return array A collection of \Nocarrier\HalLink |
||
260 | */ |
||
261 | public function getLinks() |
||
265 | |||
266 | /** |
||
267 | * Lookup and return an array of HalLink objects for a given relation. |
||
268 | * Will also resolve CURIE rels if required. |
||
269 | * |
||
270 | * @param string $rel The link relation required |
||
271 | * @return array|bool |
||
272 | * Array of HalLink objects if found. Otherwise false. |
||
273 | */ |
||
274 | public function getLink($rel) |
||
278 | |||
279 | /** |
||
280 | * Return an array of Nocarrier\Hal objected embedded in this one. |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | public function getResources() |
||
292 | |||
293 | /** |
||
294 | * Return an array of Nocarrier\Hal objected embedded in this one. |
||
295 | * |
||
296 | * @return Hal |
||
297 | */ |
||
298 | public function getResource($rel) |
||
308 | |||
309 | /** |
||
310 | * Return an array of Nocarrier\Hal objected embedded in this one. Each key |
||
311 | * may contain an array of resources, or a single resource. For a |
||
312 | * consistent approach, use getResources |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | public function getRawResources() |
||
320 | |||
321 | /** |
||
322 | * Get the first resource for a given rel. Useful if you're only expecting |
||
323 | * one resource, or you don't care about subsequent resources |
||
324 | * |
||
325 | * @return Hal |
||
326 | */ |
||
327 | public function getFirstResource($rel) |
||
337 | |||
338 | /** |
||
339 | * Get the first link for a given rel. Useful if you're only expecting |
||
340 | * one link, or you don't care about subsequent links |
||
341 | * |
||
342 | * @return HalLink |
||
343 | */ |
||
344 | public function getFirstLink($rel) |
||
354 | |||
355 | /** |
||
356 | * Set resource's URI |
||
357 | */ |
||
358 | public function setUri($uri) |
||
363 | |||
364 | /** |
||
365 | * Get resource's URI. |
||
366 | * |
||
367 | * @return mixed |
||
368 | */ |
||
369 | public function getUri() |
||
373 | |||
374 | /** |
||
375 | * Return the current object in a application/hal+json format (links and |
||
376 | * resources). |
||
377 | * |
||
378 | * @param bool $pretty |
||
379 | * Enable pretty-printing. |
||
380 | * @param bool $encode |
||
381 | * Run through json_encode |
||
382 | * @return string|array |
||
383 | */ |
||
384 | public function asJson($pretty = false, $encode = true) |
||
390 | |||
391 | /** |
||
392 | * Return the current object in a application/hal+xml format (links and |
||
393 | * resources). |
||
394 | * |
||
395 | * @param bool $pretty Enable pretty-printing |
||
396 | * @return string |
||
397 | */ |
||
398 | public function asXml($pretty = false) |
||
404 | |||
405 | /** |
||
406 | * Create a CURIE link template, used for abbreviating custom link |
||
407 | * relations. |
||
408 | * |
||
409 | * e.g, |
||
410 | * $hal->addCurie('acme', 'http://.../rels/{rel}'); |
||
411 | * $hal->addLink('acme:test', 'http://.../test'); |
||
412 | * |
||
413 | * @param string $name |
||
414 | * @param string $uri |
||
415 | * |
||
416 | * @return \Nocarrier\Hal |
||
417 | */ |
||
418 | public function addCurie($name, $uri) |
||
422 | |||
423 | /** |
||
424 | * Get a list of rel types for links that will be forced to an array for one element |
||
425 | */ |
||
426 | public function getArrayLinkRels() |
||
430 | |||
431 | /** |
||
432 | * Get a list of rel types for resources that will be forced to an array for one element |
||
433 | */ |
||
434 | public function getArrayResourceRels() |
||
438 | |||
439 | public function getShouldStripAttributes() |
||
443 | |||
444 | public function setShouldStripAttributes($shouldStripAttributes) |
||
449 | |||
450 | } |
||
451 |
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..