Passed
Push — master ( cbbb96...b48ee5 )
by Marcin
34s queued 10s
created

Filter::appendParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 19
    public function __construct($filterArray = [])
62
    {
63 19
        if (!\is_array($filterArray)) {
0 ignored issues
show
introduced by
The condition is_array($filterArray) is always true.
Loading history...
64 1
            throw new InvalidParamException(sprintf('FILTER params are invalid. Is [%s], Array expected', \gettype($filterArray)));
65
        }
66
67 18
        foreach ($filterArray as $param => $filters) {
68 7
            if ($filters instanceof FilterParam) {
69 2
                $this->appendParam($filters);
70 2
                continue;
71
            }
72
73 7
            if (!\is_string($param)) {
74 2
                throw new InvalidParamException(sprintf('Key in FILTER param is not a string'));
75
            }
76 5
            if (\is_array($filters)) {
77 5
                foreach ($filters as $operator => $value) {
78 5
                    if (!\array_key_exists($operator, self::$allowedOperators)) {
79 2
                        throw new InvalidParamException(sprintf('Operator [%s] in FILTER is not allowed', $operator));
80
                    }
81 3
                    if (\in_array($operator, [self::PARAM_IN, self::PARAM_NOTIN], true)) {
82 1
                        $value = explode(self::DELIMITER, $value);
83
                    }
84 3
                    if (\in_array($operator, [self::PARAM_NULL, self::PARAM_NOTNULL], true)) {
85 1
                        $value = null;
86
                    }
87 3
                    $this->appendParam(new FilterParam($param, $operator, $value));
88
                }
89 1
            } elseif (\is_string($filters)) {
90 3
                $this->appendParam(new FilterParam($param, self::PARAM_EQ, $filters));
91
            }
92
        }
93 14
    }
94
95
    /**
96
     * @param \mrcnpdlk\Lib\UrlSearchParser\Criteria\FilterParam $filterParam
97
     *
98
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
99
     */
100 3
    public function appendParam(FilterParam $filterParam): Filter
101
    {
102 3
        $this->filters[] = $filterParam;
103
104 3
        return $this;
105
    }
106
107
    /**
108
     * @param string $paramName
109
     *
110
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
111
     * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
112
     */
113 2
    public function getByParam(string $paramName): Filter
114
    {
115 2
        $params = [];
116 2
        foreach ($this as $item) {
117 2
            if ($item->param === $paramName) {
118 2
                $params[] = $item;
119
            }
120
        }
121
122 2
        return new self($params);
123
    }
124
125
    /**
126
     * Retrieve an external iterator
127
     *
128
     * @link  http://php.net/manual/en/iteratoraggregate.getiterator.php
129
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
130
     * <b>Traversable</b>
131
     * @since 5.0.0
132
     */
133 3
    public function getIterator(): Traversable
134
    {
135 3
        return new \ArrayIterator($this->filters);
136
    }
137
138
    /**
139
     * @param \mrcnpdlk\Lib\UrlSearchParser\Criteria\FilterParam $filterParam
140
     *
141
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter
142
     */
143 1
    public function replaceParam(FilterParam $filterParam): Filter
144
    {
145 1
        $isChanged = false;
146 1
        foreach ($this->filters as &$filter) {
147 1
            if ($filter->param === $filterParam->param && $filter->operator = $filterParam->operator) {
148 1
                $filter->value = $filterParam->value;
149 1
                $isChanged     = true;
150
            }
151
        }
152 1
        unset($filter);
153 1
        if (!$isChanged) {
154 1
            $this->appendParam($filterParam);
155
        }
156
157 1
        return $this;
158
    }
159
160
    /**
161
     * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\FilterParam[]
162
     */
163 2
    public function toArray(): array
164
    {
165 2
        return $this->filters;
166
    }
167
}
168