1 | <?php |
||
20 | class Api |
||
21 | { |
||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $limit; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $mappers_base_namespace; |
||
31 | |||
32 | /** |
||
33 | * @var \Vinelab\Api\ResponseHandler |
||
34 | */ |
||
35 | private $response_handler; |
||
36 | |||
37 | /** |
||
38 | * @var \Vinelab\Api\ErrorHandler |
||
39 | */ |
||
40 | private $error; |
||
41 | |||
42 | /** |
||
43 | * @var \Illuminate\Config\Repository |
||
44 | */ |
||
45 | private $config_reader; |
||
46 | |||
47 | /** |
||
48 | * @param \Vinelab\Api\ResponseHandler $response_handler |
||
49 | * @param \Vinelab\Api\ErrorHandler $error_handler |
||
50 | * @param \Illuminate\Config\Repository $config_reader |
||
51 | */ |
||
52 | public function __construct( |
||
63 | |||
64 | /** |
||
65 | * get config file values and store them in attributes. |
||
66 | */ |
||
67 | private function readConfigFile() |
||
74 | |||
75 | /** |
||
76 | * Map and respond. |
||
77 | * |
||
78 | * @param string|mixed $mapper |
||
79 | * @param mixed $data |
||
80 | * |
||
81 | * @throws ApiException |
||
82 | * |
||
83 | * @return Illuminate\Http\JsonResponse |
||
84 | */ |
||
85 | public function respond($mapper, $data) |
||
108 | |||
109 | /** |
||
110 | * check if $data is instance of Laravel Paginator. |
||
111 | * |
||
112 | * @param $data |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function isPaginatorInstance($data) |
||
120 | |||
121 | /** |
||
122 | * Get the formatted data out of the given mapper and data. |
||
123 | * |
||
124 | * @param string|mixed $mapper |
||
125 | * @param mixed $data |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function data($mapper, $data) |
||
163 | |||
164 | /** |
||
165 | * Resolve a class name of a mapper into the actual instance. |
||
166 | * |
||
167 | * @param string $classname |
||
168 | * |
||
169 | * @return mixed |
||
170 | */ |
||
171 | private function resolveMapperClassName($classname) |
||
175 | |||
176 | /** |
||
177 | * get the value of mappers_base_namespace from the config file. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getMapperNamespace() |
||
185 | |||
186 | /** |
||
187 | * Check whether the given array is an associative array (key-value). |
||
188 | * |
||
189 | * @param array $array |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isAssocArray($array) |
||
197 | |||
198 | /** |
||
199 | * Get the content only from the response. |
||
200 | * |
||
201 | * @param mixed $mapper |
||
202 | * @param mixed $data |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function content($mapper, $data) |
||
212 | |||
213 | /** |
||
214 | * An error occurred, respond accordingly. |
||
215 | * |
||
216 | * @return Illuminate\Http\JsonResponse |
||
217 | */ |
||
218 | public function error() |
||
222 | |||
223 | /** |
||
224 | * this function will be accessed as facade from anywhere to get the limit number of data for the endpoint call. |
||
225 | * |
||
226 | * @return int |
||
227 | */ |
||
228 | public function limit() |
||
240 | |||
241 | /** |
||
242 | * get the limit value from the config file which represents the default and the maximum limit. |
||
243 | * |
||
244 | * @return int |
||
245 | */ |
||
246 | public function getMaximumLimit() |
||
251 | |||
252 | /** |
||
253 | * validate the requested limit doesn't exceed the default predefined limit from the config file |
||
254 | * the user is allowed to override the default limit (form the config file) if and only if it is |
||
255 | * less then the default limit. |
||
256 | * |
||
257 | * @param $limit |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | private function validateRequestedLimitValue($request_limit) |
||
269 | |||
270 | /** |
||
271 | * override the limit of the config file. |
||
272 | * |
||
273 | * @param $limit |
||
274 | */ |
||
275 | public function setLimit($limit) |
||
279 | |||
280 | /** |
||
281 | * Set the base namespace from which to resolve mappers. |
||
282 | * |
||
283 | * @param string $namespace |
||
284 | */ |
||
285 | public function setMapperNamespace($namespace) |
||
289 | } |
||
290 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.