1 | <?php |
||
11 | class Response |
||
12 | { |
||
13 | /** |
||
14 | * Name of the key containing the unique resource id. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $key = 'id'; |
||
19 | |||
20 | /** |
||
21 | * Attributes to cast to a native type. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $casts = []; |
||
26 | |||
27 | /** |
||
28 | * Name of key under which multiple results are nested. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $dataKey = ''; |
||
33 | |||
34 | /** |
||
35 | * Make a new Response instance. |
||
36 | * |
||
37 | * @param array $attributes |
||
38 | */ |
||
39 | public function __construct(array $attributes = []) |
||
43 | |||
44 | /** |
||
45 | * Build a collection of Response models from an array. |
||
46 | * |
||
47 | * @param array $items raw array of results from API response |
||
48 | * @param array $apiContext |
||
49 | * @return static[]|Collection |
||
50 | */ |
||
51 | public static function collection(array $items = [], array $apiContext = []) |
||
57 | |||
58 | /** |
||
59 | * Fill the response with an array of attributes. |
||
60 | * |
||
61 | * @param array $attributes |
||
62 | * @return self |
||
63 | */ |
||
64 | public function fill(array $attributes = []): self |
||
72 | |||
73 | /** |
||
74 | * Get the value of an attribute. |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function getAttribute(string $name) |
||
83 | |||
84 | /** |
||
85 | * Set the value of an attribute. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * @param mixed $value |
||
89 | * @return self |
||
90 | */ |
||
91 | public function setAttribute(string $name, $value): self |
||
97 | |||
98 | /** |
||
99 | * Get the name of the resource unique identifier key. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getKeyName() |
||
107 | |||
108 | /** |
||
109 | * Get the value of the resource unique identifer. |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function getKey() |
||
117 | |||
118 | /** |
||
119 | * Cast an attribute to its native type. |
||
120 | * |
||
121 | * This method uses the types declared in static::$casts. |
||
122 | * |
||
123 | * @param string $key |
||
124 | * @param mixed $value |
||
125 | * @return self |
||
126 | */ |
||
127 | public function castAttribute($key, $value) |
||
136 | |||
137 | /** |
||
138 | * Cast a JSON-decoded API response to Response instance(s) |
||
139 | * |
||
140 | * @param array $apiResponse |
||
141 | * @param array $apiContext |
||
142 | * @return static|static[] |
||
143 | */ |
||
144 | public static function createFromApiResponse($apiResponse, array $apiContext = []) |
||
154 | |||
155 | /** |
||
156 | * Fetch a fresh resource. |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | public function fresh(): self |
||
164 | } |
||
165 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: