Completed
Push — master ( ffeb4b...cbbb96 )
by Marcin
05:20
created

Filter::getByParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 16.06.2018
5
 * Time: 19:05
6
 */
7
declare(strict_types=1);
8
9
namespace mrcnpdlk\Lib\UrlSearchParser\Criteria;
10
11
12
use mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException;
13
use Traversable;
14
15
/**
16
 * Class Filter
17
 *
18
 * @package mrcnpdlk\Lib\UrlSearchParser\Criteria
19
 */
20
class Filter implements \IteratorAggregate
21
{
22
    public const DELIMITER = ',';
23
24
    public const PARAM_EQ      = 'eq';
25
    public const PARAM_GT      = 'gt';
26
    public const PARAM_GTE     = 'gte';
27
    public const PARAM_LT      = 'lt';
28
    public const PARAM_LTE     = 'lte';
29
    public const PARAM_LIKE    = 'like';
30
    public const PARAM_IN      = 'in';
31
    public const PARAM_NOTIN   = 'notin';
32
    public const PARAM_NULL    = 'null';
33
    public const PARAM_NOTNULL = 'notnull';
34
35
    public static $allowedOperators = [
36
        self::PARAM_EQ      => '=',
37
        self::PARAM_GT      => '>',
38
        self::PARAM_GTE     => '>=',
39
        self::PARAM_LT      => '<',
40
        self::PARAM_LTE     => '<=',
41
        self::PARAM_LIKE    => 'like',
42
        self::PARAM_IN      => null,
43
        self::PARAM_NOTIN   => null,
44
        self::PARAM_NULL    => null,
45
        self::PARAM_NOTNULL => null,
46
    ];
47
48
    /**
49
     * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\FilterParam[]
50
     */
51
    private $filters = [];
52
53
    /**
54
     * Filter constructor.
55
     *
56
     * @param array|FilterParam[] $filterArray
57
     *
58
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
59
     * @todo Check PLUS sign in string %2B code
60
     */
61 16
    public function __construct($filterArray = [])
62
    {
63 16
        if (!\is_array($filterArray)) {
0 ignored issues
show
introduced by
The condition is_array($filterArray) is always true.
Loading history...
64
            throw new InvalidParamException(sprintf('FILTER params are invalid. Is [%s], Array expected', \gettype($filterArray)));
65
        }
66
67 16
        foreach ($filterArray as $param => $filters) {
68 5
            if ($filters instanceof FilterParam) {
69 1
                $this->filters[] = $filters;
70 1
                continue;
71
            }
72
73 5
            if (!\is_string($param)) {
74 2
                throw new InvalidParamException(sprintf('Key in FILTER param is not a string'));
75
            }
76 3
            if (\is_array($filters)) {
77 3
                foreach ($filters as $operator => $value) {
78 3
                    if (!\array_key_exists($operator, self::$allowedOperators)) {
79 2
                        throw new InvalidParamException(sprintf('Operator [%s] in FILTER is not allowed', $operator));
80
                    }
81 1
                    if (\in_array($operator, [self::PARAM_IN, self::PARAM_NOTIN], true)) {
82 1
                        $value = explode(self::DELIMITER, $value);
83
                    }
84 1
                    if (\in_array($operator, [self::PARAM_NULL, self::PARAM_NOTNULL], true)) {
85 1
                        $value = null;
86
                    }
87 1
                    $this->filters[] = new FilterParam($param, $operator, $value);
88
                }
89 1
            } elseif (\is_string($filters)) {
90 1
                $this->filters[] = new FilterParam($param, self::PARAM_EQ, $filters);
91
            }
92
        }
93 12
    }
94
95
    /**
96
     * @param string $param
97
     *
98
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
99
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
100
     */
101 1
    public function getByParam(string $param): Filter
102
    {
103 1
        $params = [];
104 1
        foreach ($this as $item) {
105 1
            if ($item->param === $param) {
106 1
                $params[] = $item;
107
            }
108
        }
109
110 1
        return new self($params);
111
    }
112
113
    /**
114
     * Retrieve an external iterator
115
     *
116
     * @link  http://php.net/manual/en/iteratoraggregate.getiterator.php
117
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
118
     * <b>Traversable</b>
119
     * @since 5.0.0
120
     */
121 1
    public function getIterator(): Traversable
122
    {
123 1
        return new \ArrayIterator($this->filters);
124
    }
125
126
    /**
127
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\FilterParam[]
128
     */
129 2
    public function toArray(): array
130
    {
131 2
        return $this->filters;
132
    }
133
}
134