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 | * API client. |
||
36 | * |
||
37 | * @var \StephaneCoinon\SendGridActivity\SendGrid |
||
38 | */ |
||
39 | protected $api = null; |
||
40 | |||
41 | /** |
||
42 | * Make a new Response instance. |
||
43 | * |
||
44 | * @param array $attributes |
||
45 | */ |
||
46 | public function __construct(array $attributes = []) |
||
50 | |||
51 | /** |
||
52 | * Build a collection of Response models from an array. |
||
53 | * |
||
54 | * @param array $items raw array of results from API response |
||
55 | * @param array $apiContext |
||
56 | * @return static[]|Collection |
||
57 | */ |
||
58 | public static function collection(array $items = [], array $apiContext = []) |
||
64 | |||
65 | /** |
||
66 | * Fill the response with an array of attributes. |
||
67 | * |
||
68 | * @param array $attributes |
||
69 | * @return self |
||
70 | */ |
||
71 | public function fill(array $attributes = []): self |
||
79 | |||
80 | /** |
||
81 | * Get the value of an attribute. |
||
82 | * |
||
83 | * @param string $name |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getAttribute(string $name) |
||
90 | |||
91 | /** |
||
92 | * Set the value of an attribute. |
||
93 | * |
||
94 | * @param string $name |
||
95 | * @param mixed $value |
||
96 | * @return self |
||
97 | */ |
||
98 | public function setAttribute(string $name, $value): self |
||
104 | |||
105 | /** |
||
106 | * Get the name of the resource unique identifier key. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getKeyName() |
||
114 | |||
115 | /** |
||
116 | * Get the value of the resource unique identifer. |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function getKey() |
||
124 | |||
125 | /** |
||
126 | * Cast an attribute to its native type. |
||
127 | * |
||
128 | * This method uses the types declared in static::$casts. |
||
129 | * |
||
130 | * @param string $key |
||
131 | * @param mixed $value |
||
132 | * @return self |
||
133 | */ |
||
134 | public function castAttribute($key, $value) |
||
143 | |||
144 | /** |
||
145 | * Cast a JSON-decoded API response to Response instance(s) |
||
146 | * |
||
147 | * @param array $apiResponse |
||
148 | * @param array $apiContext |
||
149 | * @return static|static[] |
||
150 | */ |
||
151 | public static function createFromApiResponse($apiResponse, array $apiContext = []) |
||
161 | |||
162 | /** |
||
163 | * Fetch a fresh resource. |
||
164 | * |
||
165 | * @return self |
||
166 | */ |
||
167 | public function fresh() |
||
171 | } |
||
172 |
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: