1 | <?php |
||
15 | abstract class ApiResource implements ArrayAccess, ResourceContract |
||
16 | { |
||
17 | use ArrayAccessTrait; |
||
18 | |||
19 | /** |
||
20 | * @var \Laravel\Forge\ApiProvider |
||
21 | */ |
||
22 | protected $api; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data = []; |
||
28 | |||
29 | /** |
||
30 | * @var \Laravel\Forge\Contracts\ResourceContract |
||
31 | */ |
||
32 | protected $owner; |
||
33 | |||
34 | /** |
||
35 | * Create new resource instance. |
||
36 | * |
||
37 | * @param \Laravel\Forge\ApiProvider $api = null |
||
38 | * @param array $data = [] |
||
39 | * @param \Laravel\Forge\Contracts\ResourceContract $owner |
||
40 | */ |
||
41 | public function __construct(ApiProvider $api = null, array $data = [], ResourceContract $owner = null) |
||
42 | { |
||
43 | $this->api = $api; |
||
44 | $this->data = $data; |
||
45 | $this->owner = $owner; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Resource type. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | abstract public static function resourceType(); |
||
54 | |||
55 | /** |
||
56 | * Resource path (relative to owner or API root). |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | abstract public function resourcePath(); |
||
61 | |||
62 | /** |
||
63 | * Create new Resource instance from HTTP response. |
||
64 | * |
||
65 | * @param \Psr\Http\Message\ResponseInterface $response |
||
66 | * @param \Laravel\Forge\ApiProvider $api |
||
67 | * @param \Laravel\Forge\Contracts\ResourceContract $owner = null |
||
68 | * |
||
69 | * @return static |
||
70 | */ |
||
71 | public static function createFromResponse(ResponseInterface $response, ApiProvider $api, ResourceContract $owner = null) |
||
82 | |||
83 | /** |
||
84 | * Throw HTTP Not Found exception. |
||
85 | * |
||
86 | * @throws \InvalidArgumentException |
||
87 | */ |
||
88 | protected static function throwNotFoundException() |
||
92 | |||
93 | /** |
||
94 | * Determines if current resource has an owner. |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function hasResourceOwner(): bool |
||
102 | |||
103 | /** |
||
104 | * Get current resource owner. |
||
105 | * |
||
106 | * @return \Laravel\Forge\Contracts\ResourceContract|null |
||
107 | */ |
||
108 | public function resourceOwner() |
||
112 | |||
113 | /** |
||
114 | * Get API provider. |
||
115 | * |
||
116 | * @return \Laravel\Forge\ApiProvider |
||
117 | */ |
||
118 | public function getApi(): ApiProvider |
||
122 | |||
123 | /** |
||
124 | * Get underlying API provider's HTTP client. |
||
125 | * |
||
126 | * @return \GuzzleHttp\ClientInterface |
||
127 | */ |
||
128 | public function getHttpClient(): ClientInterface |
||
132 | |||
133 | /** |
||
134 | * Get resource data. |
||
135 | * |
||
136 | * @param string|int $key |
||
137 | * @param mixed $default = null |
||
138 | * |
||
139 | * @return mixed|null |
||
140 | */ |
||
141 | public function getData($key, $default = null) |
||
145 | |||
146 | /** |
||
147 | * Get full resource data. |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function getFullData() |
||
155 | |||
156 | /** |
||
157 | * Resource API URL. |
||
158 | * |
||
159 | * @param string $path = '' |
||
160 | * @param bool $withPropagation = true |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function apiUrl(string $path = '', bool $withPropagation = true): string |
||
175 | |||
176 | /** |
||
177 | * Resource ID. |
||
178 | * |
||
179 | * @return int |
||
180 | */ |
||
181 | public function id(): int |
||
185 | |||
186 | /** |
||
187 | * Resource name. |
||
188 | * |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function name() |
||
195 | |||
196 | /** |
||
197 | * Resource status. |
||
198 | * |
||
199 | * @return string|null |
||
200 | */ |
||
201 | public function status() |
||
205 | |||
206 | /** |
||
207 | * Get resource creation date. |
||
208 | * |
||
209 | * @return string|null |
||
210 | */ |
||
211 | public function createdAt() |
||
215 | |||
216 | /** |
||
217 | * Update resource data. |
||
218 | * |
||
219 | * @param array $payload |
||
220 | * |
||
221 | * @throws UpdateResourceException |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function update(array $payload): bool |
||
247 | |||
248 | /** |
||
249 | * Delete current resource. |
||
250 | * |
||
251 | * @throws \Laravel\Forge\Exceptions\Resources\DeleteResourceException |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function delete() |
||
265 | |||
266 | /** |
||
267 | * Throw resource exception. |
||
268 | * |
||
269 | * @param \Psr\Http\Message\ResponseInterface $response |
||
270 | * @param string $action |
||
271 | * @param string $exceptionClass |
||
272 | * |
||
273 | * @throws \Exception |
||
274 | */ |
||
275 | protected function throwResourceException(ResponseInterface $response, string $action, string $exceptionClass) |
||
287 | } |
||
288 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: