1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Rest; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Message\RequestInterface; |
6
|
|
|
|
7
|
|
|
class Collection extends \ArrayObject |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Guzzle\Http\Message\Response |
11
|
|
|
*/ |
12
|
|
|
protected $response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $elementClass = '\Acquia\Rest\Element'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The array key containing the collection, null if it is not nested. |
21
|
|
|
* |
22
|
|
|
* Alternately set an array of keys that the may contain the collection. |
23
|
|
|
* This is useful when working with inconsistent APIs that store collections |
24
|
|
|
* of the same elements in different properties depending on the endpoint |
25
|
|
|
* that is consumed. |
26
|
|
|
* |
27
|
|
|
* @var string|array |
28
|
|
|
*/ |
29
|
|
|
protected $collectionProperty; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Guzzle\Http\Message\RequestInterface $request |
33
|
|
|
*/ |
34
|
42 |
|
public function __construct(RequestInterface $request) |
35
|
|
|
{ |
36
|
42 |
|
$this->response = $request->send(); |
37
|
42 |
|
parent::__construct($this->response->json()); |
38
|
42 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Keys the array of objects by their identifier, constructs and returns and |
42
|
|
|
* array object. |
43
|
|
|
* |
44
|
|
|
* When the object is cast to a string, its unique identifier is returned. |
45
|
|
|
* |
46
|
|
|
* @return \ArrayObject |
47
|
|
|
* |
48
|
|
|
* @throws \OutOfBoundsException |
49
|
|
|
* |
50
|
|
|
* @see \Acquia\Rest\Element::__toString() |
51
|
|
|
*/ |
52
|
36 |
|
public function getIterator() |
53
|
|
|
{ |
54
|
36 |
|
$array = $this->getArrayCopy(); |
55
|
|
|
|
56
|
|
|
// Is the collection nested in the array? |
57
|
36 |
|
if (isset($this->collectionProperty)) { |
58
|
|
|
|
59
|
|
|
// Locate the collection in the response. |
60
|
6 |
|
$collectionFound = false; |
61
|
6 |
|
$property = NULL; |
62
|
6 |
|
foreach ((array) $this->collectionProperty as $property) { |
63
|
6 |
|
if (isset($array[$property])) { |
64
|
3 |
|
$collectionFound = true; |
65
|
3 |
|
break; |
66
|
|
|
} |
67
|
6 |
|
} |
68
|
|
|
|
69
|
6 |
|
if (!$collectionFound) { |
70
|
3 |
|
throw new \OutOfBoundsException('Collection not found in response'); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$array = $array[$property]; |
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
// Build the collection. |
77
|
33 |
|
$collection = array(); |
78
|
33 |
|
foreach ($array as $item) { |
79
|
33 |
|
$element = new $this->elementClass($item); |
80
|
33 |
|
$collection[(string) $element] = $element; |
81
|
33 |
|
} |
82
|
|
|
|
83
|
33 |
|
return new \ArrayObject($collection); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the raw response body, usually a string containing JSON. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
3 |
|
public function __toString() |
92
|
|
|
{ |
93
|
3 |
|
return $this->response->getBody(true); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.