Issues (7)

src/Collections/FiatRatesCollection.php (2 issues)

1
<?php
2
3
4
namespace Sarnado\Converter\Collections;
5
6
7
use Sarnado\Converter\Objects\CryptoRateObject;
8
use Sarnado\Converter\Objects\FiatRateObject;
9
use Tightenco\Collect\Support\Collection;
10
11
/**
12
 * Class FiatRatesCollection
13
 * @package Sarnado\Converter\Collections
14
 */
15
class FiatRatesCollection extends Collection
16
{
17
    /**
18
     * FiatRatesCollection constructor.
19
     * @param array $data
20
     */
21 30
    public function __construct(array $data)
22
    {
23 30
        if ($this->check($data))
24
        {
25 18
            parent::__construct($data);
26
        }
27
        else
0 ignored issues
show
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
28
        {
29 12
            throw new \InvalidArgumentException('Item from array must be instance of FiatRateObject');
30
        }
31 18
    }
32
33
    /**
34
     * @param array $data
35
     * @return bool
36
     */
37 30
    private function check(array $data): bool
38
    {
39 30
        if (!empty($data))
40
        {
41 18
            foreach ($data as $item)
42
            {
43 18
                if (!($item instanceof FiatRateObject))
44
                {
45 14
                    return false;
46
                }
47
            }
48
        }
49 18
        return true;
50
    }
51
52 6
    public function filterByPair(string $from, string $to): FiatRatesCollection
53
    {
54
        return $this->filter(function (FiatRateObject $rate) use ($from, $to)
55
        {
0 ignored issues
show
Opening brace should be on the same line as the declaration
Loading history...
56 3
            return $rate->getFrom() === $from && $rate->getTo() === $to;
57 6
        });
58
    }
59
}
60