1 | <?php |
||
16 | abstract class AbstractResource extends Operator implements ResourceInterface |
||
17 | { |
||
18 | const DEFAULT_MARKER_KEY = 'id'; |
||
19 | |||
20 | /** |
||
21 | * The JSON key that indicates how the API nests singular resources. For example, when |
||
22 | * performing a GET, it could respond with ``{"server": {"id": "12345"}}``. In this case, |
||
23 | * "server" is the resource key, since the essential state of the server is nested inside. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $resourceKey; |
||
28 | |||
29 | /** |
||
30 | * The key that indicates how the API nests resource collections. For example, when |
||
31 | * performing a GET, it could respond with ``{"servers": [{}, {}]}``. In this case, "servers" |
||
32 | * is the resources key, since the array of servers is nested inside. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $resourcesKey; |
||
37 | |||
38 | /** |
||
39 | * Indicates which attribute of the current resource should be used for pagination markers. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $markerKey; |
||
44 | |||
45 | /** |
||
46 | * An array of aliases that will be checked when the resource is being populated. For example, |
||
47 | * |
||
48 | * 'FOO_BAR' => 'fooBar' |
||
49 | * |
||
50 | * will extract FOO_BAR from the response, and save it as 'fooBar' in the resource. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $aliases = []; |
||
55 | |||
56 | /** |
||
57 | * Populates the current resource from a response object. |
||
58 | * |
||
59 | * @param ResponseInterface $response |
||
60 | * |
||
61 | * @return $this|ResourceInterface |
||
62 | */ |
||
63 | 73 | public function populateFromResponse(ResponseInterface $response) |
|
64 | { |
||
65 | 73 | if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) { |
|
66 | 61 | $json = Utils::jsonDecode($response); |
|
67 | 61 | if (!empty($json)) { |
|
68 | 61 | $this->populateFromArray(Utils::flattenJson($json, $this->resourceKey)); |
|
69 | 61 | } |
|
70 | 61 | } |
|
71 | |||
72 | 73 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Populates the current resource from a data array. |
||
77 | * |
||
78 | * @param array $array |
||
79 | * |
||
80 | * @return mixed|void |
||
81 | */ |
||
82 | 132 | public function populateFromArray(array $array) |
|
98 | |||
99 | 127 | private function parseDocBlockValue($type, $val) |
|
100 | 2 | { |
|
101 | 127 | if (strpos($type, '[]') === 0 && is_array($val)) { |
|
102 | 6 | $array = []; |
|
103 | 6 | foreach ($val as $subVal) { |
|
104 | 6 | $array[] = $this->model($this->normalizeModelClass(substr($type, 2)), $subVal); |
|
105 | 6 | } |
|
106 | 6 | $val = $array; |
|
107 | 127 | } elseif (strcasecmp($type, '\datetimeimmutable') === 0) { |
|
108 | 28 | $val = new \DateTimeImmutable($val); |
|
109 | 126 | } elseif ($this->isNotNativeType($type)) { |
|
110 | 11 | $val = $this->model($this->normalizeModelClass($type), $val); |
|
111 | 11 | } |
|
112 | |||
113 | 127 | return $val; |
|
114 | } |
||
115 | |||
116 | 123 | private function isNotNativeType($type) |
|
122 | |||
123 | 12 | private function normalizeModelClass($class) |
|
132 | |||
133 | 130 | private function extractTypeFromDocBlock(\ReflectionClass $reflClass, $propertyName) |
|
145 | |||
146 | /** |
||
147 | * Internal method which retrieves the values of provided keys. |
||
148 | * |
||
149 | * @param array $keys |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 65 | protected function getAttrs(array $keys) |
|
165 | |||
166 | /** |
||
167 | * @param array $definition |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | 60 | public function executeWithState(array $definition) |
|
175 | |||
176 | 38 | private function getResourcesKey() |
|
177 | { |
||
187 | |||
188 | /** |
||
189 | * {@inheritDoc} |
||
190 | */ |
||
191 | 33 | public function enumerate(array $def, array $userVals = [], callable $mapFn = null) |
|
218 | |||
219 | 5 | public function extractMultipleInstances(ResponseInterface $response, $key = null) |
|
234 | } |