IteratorFilter   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 72.31%

Importance

Changes 0
Metric Value
wmc 35
eloc 60
dl 0
loc 174
ccs 47
cts 65
cp 0.7231
rs 9.6
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 3 1
A match() 0 11 3
A getRawFilters() 0 3 1
D evalString() 0 60 24
A addRelationOr() 0 4 1
A format() 0 3 1
A endGroup() 0 4 1
A addRelation() 0 4 1
A startGroup() 0 4 1
1
<?php
2
3
namespace ByJG\AnyDataset\Core;
4
5
use ByJG\AnyDataset\Core\Enum\Relation;
6
7
class IteratorFilter
8
{
9
10
    /**
11
     * @var array
12
     */
13
    private $filters;
14
15
    /**
16
     * IteratorFilter Constructor
17
     */
18 5
    public function __construct()
19
    {
20 5
        $this->filters = [];
21
    }
22
23
    /**
24
     * @return IteratorFilter
25
     */
26 1
    public static function getInstance()
27
    {
28 1
        return new IteratorFilter();
29
    }
30
31
    /**
32
     * @param array $array
33
     * @return Row[]
34
     */
35 2
    public function match($array)
36
    {
37 2
        $returnArray = [];
38
39 2
        foreach ($array as $sr) {
40 2
            if ($this->evalString($sr)) {
41 2
                $returnArray[] = $sr;
42
            }
43
        }
44
45 2
        return $returnArray;
46
    }
47
48
    /**
49
     * Get the filter
50
     *
51
     * @param IteratorFilterFormatter $formatter
52
     * @param string $tableName
53
     * @param array $params
54
     * @param string $returnFields
55
     * @return string
56
     */
57 3
    public function format(IteratorFilterFormatter $formatter, $tableName = null, &$params = [], $returnFields = "*")
58
    {
59 3
        return $formatter->format($this->filters, $tableName, $params, $returnFields);
60
    }
61
62
63
    /**
64
     * @param Row $singleRow
65
     * @return bool
66
     */
67 2
    private function evalString(Row $singleRow)
68
    {
69 2
        $result = [];
70 2
        $finalResult = false;
71 2
        $pos = 0;
72
73 2
        $result[0] = true;
74
75 2
        foreach ($this->filters as $filter) {
76 2
            if (($filter[0] == ")") || ($filter[0] == " or ")) {
77 1
                $finalResult = $finalResult || $result[$pos];
78 1
                $result[++$pos] = true;
79
            }
80
81 2
            $name = $filter[1];
82 2
            $relation = $filter[2];
83 2
            $value = $filter[3];
84
85 2
            $field = [$singleRow->get($name)];
86
87 2
            foreach ($field as $valueparam) {
88
                switch ($relation) {
89 2
                    case Relation::EQUAL:
90 1
                        $result[$pos] = $result[$pos] && ($valueparam == $value);
91 1
                        break;
92
93 1
                    case Relation::GREATER_THAN:
94
                        $result[$pos] = $result[$pos] && ($valueparam > $value);
95
                        break;
96
97 1
                    case Relation::LESS_THAN:
98 1
                        $result[$pos] = $result[$pos] && ($valueparam < $value);
99 1
                        break;
100
101
                    case Relation::GREATER_OR_EQUAL_THAN:
102
                        $result[$pos] = $result[$pos] && ($valueparam >= $value);
103
                        break;
104
105
                    case Relation::LESS_OR_EQUAL_THAN:
106
                        $result[$pos] = $result[$pos] && ($valueparam <= $value);
107
                        break;
108
109
                    case Relation::NOT_EQUAL:
110
                        $result[$pos] = $result[$pos] && ($valueparam != $value);
111
                        break;
112
113
                    case Relation::STARTS_WITH:
114
                        $result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) === 0);
115
                        break;
116
117
                    default: // Relation::CONTAINS:
118
                        $result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) !== false);
119
                        break;
120
                }
121
            }
122
        }
123
124 2
        $finalResult = $finalResult || $result[$pos];
125
126 2
        return $finalResult;
127
    }
128
129
    /**
130
     * @param string $name Field name
131
     * @param int $relation Relation enum
132
     * @param string $value Field string value
133
     * @return IteratorFilter
134
     * @desc Add a single string comparison to filter.
135
     */
136 5
    public function addRelation($name, $relation, $value)
137
    {
138 5
        $this->filters[] = [" and ", $name, $relation, $value];
139 5
        return $this;
140
    }
141
142
    /**
143
     * @param string $name Field name
144
     * @param int $relation Relation enum
145
     * @param string $value Field string value
146
     * @return IteratorFilter
147
     * @desc Add a single string comparison to filter. This comparison use the OR operator.
148
     */
149 3
    public function addRelationOr($name, $relation, $value)
150
    {
151 3
        $this->filters[] = [" or ", $name, $relation, $value];
152 3
        return $this;
153
    }
154
155
    /**
156
     * Add a "("
157
     * @return IteratorFilter
158
     */
159 1
    public function startGroup()
160
    {
161 1
        $this->filters[] = ["(", "", "", ""];
162 1
        return $this;
163
    }
164
165
    /**
166
     * Add a ")"
167
     * @return IteratorFilter
168
     */
169 1
    public function endGroup()
170
    {
171 1
        $this->filters[] = [")", "", "", ""];
172 1
        return $this;
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public function getRawFilters()
179
    {
180
        return $this->filters;
181
    }
182
}
183