Skip to content

Commit 2a80886

Browse files
committed
Added Relation::IN and Relation::NOT_IN
1 parent 30b8a35 commit 2a80886

File tree

3 files changed

+39
-68
lines changed

3 files changed

+39
-68
lines changed

src/IteratorFilter.php

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class IteratorFilter
1010
/**
1111
* @var array
1212
*/
13-
private $filters;
13+
private array $filters;
1414

1515
/**
1616
* IteratorFilter Constructor
@@ -23,7 +23,7 @@ public function __construct()
2323
/**
2424
* @return IteratorFilter
2525
*/
26-
public static function getInstance()
26+
public static function getInstance(): IteratorFilter
2727
{
2828
return new IteratorFilter();
2929
}
@@ -32,7 +32,7 @@ public static function getInstance()
3232
* @param array $array
3333
* @return Row[]
3434
*/
35-
public function match($array)
35+
public function match(array $array): array
3636
{
3737
$returnArray = [];
3838

@@ -49,12 +49,12 @@ public function match($array)
4949
* Get the filter
5050
*
5151
* @param IteratorFilterFormatter $formatter
52-
* @param string $tableName
52+
* @param string|null $tableName
5353
* @param array $params
5454
* @param string $returnFields
5555
* @return string
5656
*/
57-
public function format(IteratorFilterFormatter $formatter, $tableName = null, &$params = [], $returnFields = "*")
57+
public function format(IteratorFilterFormatter $formatter, string $tableName = null, array &$params = [], string $returnFields = "*"): string
5858
{
5959
return $formatter->format($this->filters, $tableName, $params, $returnFields);
6060
}
@@ -64,7 +64,7 @@ public function format(IteratorFilterFormatter $formatter, $tableName = null, &$
6464
* @param Row $singleRow
6565
* @return bool
6666
*/
67-
private function evalString(Row $singleRow)
67+
private function evalString(Row $singleRow): bool
6868
{
6969
$result = [];
7070
$finalResult = false;
@@ -85,47 +85,18 @@ private function evalString(Row $singleRow)
8585
$field = [$singleRow->get($name)];
8686

8787
foreach ($field as $valueparam) {
88-
switch ($relation) {
89-
case Relation::EQUAL:
90-
$result[$pos] = $result[$pos] && ($valueparam == $value);
91-
break;
92-
93-
case Relation::GREATER_THAN:
94-
$result[$pos] = $result[$pos] && ($valueparam > $value);
95-
break;
96-
97-
case Relation::LESS_THAN:
98-
$result[$pos] = $result[$pos] && ($valueparam < $value);
99-
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-
case Relation::IN:
118-
$result[$pos] = $result[$pos] && in_array($valueparam, $value);
119-
break;
120-
121-
case Relation::NOT_IN:
122-
$result[$pos] = $result[$pos] && !in_array($valueparam, $value);
123-
break;
124-
125-
default: // Relation::CONTAINS:
126-
$result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) !== false);
127-
break;
128-
}
88+
$result[$pos] = match ($relation) {
89+
Relation::EQUAL => $result[$pos] && ($valueparam == $value),
90+
Relation::GREATER_THAN => $result[$pos] && ($valueparam > $value),
91+
Relation::LESS_THAN => $result[$pos] && ($valueparam < $value),
92+
Relation::GREATER_OR_EQUAL_THAN => $result[$pos] && ($valueparam >= $value),
93+
Relation::LESS_OR_EQUAL_THAN => $result[$pos] && ($valueparam <= $value),
94+
Relation::NOT_EQUAL => $result[$pos] && ($valueparam != $value),
95+
Relation::STARTS_WITH => $result[$pos] && (str_starts_with(is_null($valueparam) ? "" : $valueparam, $value)),
96+
Relation::IN => $result[$pos] && in_array($valueparam, $value),
97+
Relation::NOT_IN => $result[$pos] && !in_array($valueparam, $value),
98+
default => $result[$pos] && (str_contains(is_null($valueparam) ? "" : $valueparam, $value)),
99+
};
129100
}
130101
}
131102

@@ -137,11 +108,11 @@ private function evalString(Row $singleRow)
137108
/**
138109
* @param string $name Field name
139110
* @param int $relation Relation enum
140-
* @param string|array $value Field string value
141-
* @return IteratorFilter
111+
* @param array|string $value Field string value
112+
* @return static
142113
* @desc Add a single string comparison to filter.
143114
*/
144-
public function addRelation($name, $relation, $value)
115+
public function addRelation(string $name, int $relation, array|string $value): static
145116
{
146117
$this->filters[] = [" and ", $name, $relation, $value];
147118
return $this;
@@ -150,31 +121,31 @@ public function addRelation($name, $relation, $value)
150121
/**
151122
* @param string $name Field name
152123
* @param int $relation Relation enum
153-
* @param string $value Field string value
154-
* @return IteratorFilter
124+
* @param array|string $value Field string value
125+
* @return static
155126
* @desc Add a single string comparison to filter. This comparison use the OR operator.
156127
*/
157-
public function addRelationOr($name, $relation, $value)
128+
public function addRelationOr(string $name, int $relation, array|string $value): static
158129
{
159130
$this->filters[] = [" or ", $name, $relation, $value];
160131
return $this;
161132
}
162133

163134
/**
164135
* Add a "("
165-
* @return IteratorFilter
136+
* @return static
166137
*/
167-
public function startGroup()
138+
public function startGroup(): static
168139
{
169140
$this->filters[] = ["(", "", "", ""];
170141
return $this;
171142
}
172143

173144
/**
174145
* Add a ")"
175-
* @return IteratorFilter
146+
* @return static
176147
*/
177-
public function endGroup()
148+
public function endGroup(): static
178149
{
179150
$this->filters[] = [")", "", "", ""];
180151
return $this;
@@ -183,7 +154,7 @@ public function endGroup()
183154
/**
184155
* @return array
185156
*/
186-
public function getRawFilters()
157+
public function getRawFilters(): array
187158
{
188159
return $this->filters;
189160
}

src/IteratorFilterFormatter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ abstract class IteratorFilterFormatter
1010
*
1111
* @param string $name
1212
* @param string $relation
13-
* @param string|array $value
13+
* @param array|string $value
1414
* @param array $param
1515
* @return string
1616
*/
17-
abstract public function getRelation($name, $relation, $value, &$param);
17+
abstract public function getRelation(string $name, string $relation, array|string $value, array &$param): string;
1818

1919
/**
2020
* Get formatted field
2121
*
2222
* @param array $filters
23-
* @param string $tableName
23+
* @param string|null $tableName
2424
* @param array $params
2525
* @param string $returnFields
2626
* @return string
2727
*/
28-
abstract public function format($filters, $tableName = null, &$params = [], $returnFields = "*");
28+
abstract public function format(array $filters, string $tableName = null, array &$params = [], string $returnFields = "*"): string;
2929

3030
/**
3131
* Get Filter
@@ -34,7 +34,7 @@ abstract public function format($filters, $tableName = null, &$params = [], $ret
3434
* @param array $param
3535
* @return string
3636
*/
37-
public function getFilter($filters, &$param)
37+
public function getFilter(array $filters, array &$param): string
3838
{
3939
$filter = "";
4040
$param = array();

src/IteratorFilterXPathFormatter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class IteratorFilterXPathFormatter extends IteratorFilterFormatter
99
/**
1010
* @inheritDoc
1111
*/
12-
public function format($filters, $tableName = null, &$params = [], $returnFields = "*")
13-
{
12+
public function format(array $filters, string $tableName = null, array &$params = [], string $returnFields = "*"): string
13+
{
1414
$param = [];
1515
$xpathFilter = $this->getFilter($filters, $param);
1616

@@ -19,13 +19,13 @@ public function format($filters, $tableName = null, &$params = [], $returnFields
1919
}
2020

2121
return "/anydataset/row[" . $xpathFilter . "]";
22-
}
22+
}
2323

2424
/**
2525
* @inheritDoc
2626
*/
27-
public function getRelation($name, $relation, $value, &$param)
28-
{
27+
public function getRelation(string $name, string $relation, array|string $value, array &$param): string
28+
{
2929
$str = is_numeric($value) ? "" : "'";
3030
$field = "field[@name='" . $name . "'] ";
3131
if (is_string($value)) {

0 commit comments

Comments
 (0)