1 | <?php |
||
2 | |||
3 | |||
4 | namespace Sarnado\Converter\Collections; |
||
5 | |||
6 | use Sarnado\Converter\Objects\CryptoRateObject; |
||
7 | use Tightenco\Collect\Support\Collection; |
||
8 | |||
9 | /** |
||
10 | * Class CryptoRatesCollection |
||
11 | * @package Sarnado\Converter\Collections |
||
12 | */ |
||
13 | class CryptoRatesCollection extends Collection |
||
14 | { |
||
15 | /** |
||
16 | * CryptoRatesCollection constructor. |
||
17 | * @param array $data |
||
18 | */ |
||
19 | 30 | public function __construct(array $data = []) |
|
20 | { |
||
21 | 30 | if ($this->check($data)) |
|
22 | { |
||
23 | 18 | parent::__construct($data); |
|
24 | } |
||
25 | else |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
26 | { |
||
27 | 12 | throw new \InvalidArgumentException('Item from array must be instance of CryptoRateObject'); |
|
28 | } |
||
29 | 18 | } |
|
30 | |||
31 | /** |
||
32 | * @param array $data |
||
33 | * @return bool |
||
34 | */ |
||
35 | 30 | private function check(array $data): bool |
|
36 | { |
||
37 | 30 | if (!empty($data)) |
|
38 | { |
||
39 | 24 | foreach ($data as $item) |
|
40 | { |
||
41 | 24 | if (!($item instanceof CryptoRateObject)) |
|
42 | { |
||
43 | 16 | return false; |
|
44 | } |
||
45 | } |
||
46 | } |
||
47 | 18 | return true; |
|
48 | } |
||
49 | |||
50 | 6 | public function filterByPair(string $crypto, string $fiat): CryptoRatesCollection |
|
51 | { |
||
52 | return $this->filter(function (CryptoRateObject $rate) use ($crypto, $fiat) |
||
53 | { |
||
0 ignored issues
–
show
|
|||
54 | 3 | return $rate->getCrypto() === $crypto && $rate->getFiat() === $fiat; |
|
55 | 6 | }); |
|
56 | } |
||
57 | } |
||
58 |