1 | <?php |
||
2 | |||
3 | namespace Rawaby88\OpenWeatherMap\Traits; |
||
4 | |||
5 | use Rawaby88\OpenWeatherMap\Services\Support\CurrentWeather; |
||
6 | |||
7 | /** |
||
8 | * Trait CWMultiResultTrait. |
||
9 | */ |
||
10 | trait CWMultiResultTrait |
||
11 | { |
||
12 | /** |
||
13 | * @var array Of CurrentWeather objects. |
||
14 | */ |
||
15 | public $list; |
||
16 | |||
17 | /** |
||
18 | * @var array Hold query parameters for api call. |
||
19 | */ |
||
20 | protected $params; |
||
21 | |||
22 | /** |
||
23 | * @var string Part of the api link. |
||
24 | */ |
||
25 | protected $apiCall; |
||
26 | |||
27 | /** |
||
28 | * Return instance of CWMultiResultInterface. |
||
29 | */ |
||
30 | public function get() |
||
31 | { |
||
32 | $data = $this->queryOrFail($this->apiCall, $this->params); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
33 | $this->list = []; |
||
34 | |||
35 | collect($data->list)->map(function ($result, $key) { |
||
36 | $this->list[$key] = new CurrentWeather($result); |
||
37 | }); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Return the $index item on the list as CurrentWeather object. |
||
44 | * @param $index |
||
45 | * @return CurrentWeather |
||
46 | */ |
||
47 | public function index($index): CurrentWeather |
||
48 | { |
||
49 | return $this->list[$index]; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Return the first item on the list as CurrentWeather object. |
||
54 | * @return CurrentWeather |
||
55 | */ |
||
56 | public function first(): CurrentWeather |
||
57 | { |
||
58 | return reset($this->list); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Return count number of the result. |
||
63 | * @return int |
||
64 | */ |
||
65 | public function count(): int |
||
66 | { |
||
67 | return count($this->list); |
||
68 | } |
||
69 | } |
||
70 |