Skip to content

Commit 36924a9

Browse files
authored
Merge pull request #16 from byjg/4.9
4.9
2 parents 742fe02 + 9b9c235 commit 36924a9

File tree

7 files changed

+181
-45
lines changed

7 files changed

+181
-45
lines changed

src/Enum/Relation.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ class Relation
4949
* "Contains" unary comparator
5050
*/
5151
const CONTAINS = 7;
52+
53+
/**
54+
* "In" unary comparator
55+
*/
56+
const IN = 8;
57+
58+
/**
59+
* "Not In" unary comparator
60+
*/
61+
const NOT_IN = 9;
5262
}

src/IteratorFilter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ private function evalString(Row $singleRow)
114114
$result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) === 0);
115115
break;
116116

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+
117125
default: // Relation::CONTAINS:
118126
$result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) !== false);
119127
break;
@@ -129,7 +137,7 @@ private function evalString(Row $singleRow)
129137
/**
130138
* @param string $name Field name
131139
* @param int $relation Relation enum
132-
* @param string $value Field string value
140+
* @param string|array $value Field string value
133141
* @return IteratorFilter
134142
* @desc Add a single string comparison to filter.
135143
*/

src/IteratorFilterFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ abstract class IteratorFilterFormatter
1010
*
1111
* @param string $name
1212
* @param string $relation
13-
* @param string $value
13+
* @param string|array $value
1414
* @param array $param
1515
* @return string
1616
*/

src/IteratorFilterXPathFormatter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function getRelation($name, $relation, $value, &$param)
2828
{
2929
$str = is_numeric($value) ? "" : "'";
3030
$field = "field[@name='" . $name . "'] ";
31-
$value = " $str$value$str ";
31+
if (is_string($value)) {
32+
$value = " $str$value$str ";
33+
}
3234

3335
switch ($relation) {
3436
case Relation::EQUAL:
@@ -59,6 +61,12 @@ public function getRelation($name, $relation, $value, &$param)
5961
$return = " starts-with($field, $value) ";
6062
break;
6163

64+
case Relation::IN:
65+
throw new \InvalidArgumentException('XPath does not support IN');
66+
67+
case Relation::NOT_IN:
68+
throw new \InvalidArgumentException('XPath does not support NOT IN');
69+
6270
default: // Relation::CONTAINS:
6371
$return = " contains($field, $value) ";
6472
break;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Tests\AnyDataset\Dataset;
4+
5+
use ByJG\AnyDataset\Core\IteratorFilter;
6+
use ByJG\AnyDataset\Core\IteratorFilterXPathFormatter;
7+
use ByJG\AnyDataset\Core\Row;
8+
use ByJG\AnyDataset\Core\Enum\Relation;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class IteratorFilterAnydatasetTest extends TestCase
12+
{
13+
14+
/**
15+
* @var IteratorFilter
16+
*/
17+
protected $object;
18+
19+
/**
20+
* Sets up the fixture, for example, opens a network connection.
21+
* This method is called before a test is executed.
22+
*/
23+
protected function setUp(): void
24+
{
25+
$this->object = new IteratorFilter();
26+
}
27+
28+
public function testMatch()
29+
{
30+
31+
$collection = [
32+
$row1 = new Row(
33+
[
34+
'id' => 1,
35+
'field' => 'value1',
36+
'field2' => 'value2',
37+
'val' => 50,
38+
]
39+
),
40+
$row2 = new Row(
41+
[
42+
'id' => 2,
43+
'field' => 'other1',
44+
'field2' => 'other2',
45+
'val' => 80,
46+
]
47+
),
48+
$row3 = new Row(
49+
[
50+
'id' => 3,
51+
'field' => 'last1',
52+
'field2' => 'last2',
53+
'val' => 30,
54+
]
55+
),
56+
$row4 = new Row(
57+
[
58+
'id' => 4,
59+
'field' => 'xy',
60+
'field2' => 'zy',
61+
'val' => 10,
62+
]
63+
),
64+
];
65+
66+
$this->assertEquals($collection, $this->object->match($collection));
67+
68+
$this->object->addRelation('field2', Relation::EQUAL, 'other2');
69+
$this->assertEquals([$row2], $this->object->match($collection));
70+
71+
$this->object->addRelationOr('field', Relation::EQUAL, 'last1');
72+
$this->assertEquals([$row2, $row3], $this->object->match($collection));
73+
74+
75+
//------------------------
76+
77+
$this->object = new IteratorFilter();
78+
$this->object->addRelation('field', Relation::EQUAL, 'last1');
79+
$this->object->addRelation('field2', Relation::EQUAL, 'last2');
80+
$this->assertEquals([$row3], $this->object->match($collection));
81+
82+
// Test Greater Than
83+
$this->object = new IteratorFilter();
84+
$this->object->addRelation('val', Relation::GREATER_THAN, 50);
85+
$this->assertEquals([$row2], $this->object->match($collection));
86+
87+
// Test Less Than
88+
$this->object = new IteratorFilter();
89+
$this->object->addRelation('val', Relation::LESS_THAN, 50);
90+
$this->assertEquals([$row3, $row4], $this->object->match($collection));
91+
92+
// Test Greater or Equal Than
93+
$this->object = new IteratorFilter();
94+
$this->object->addRelation('val', Relation::GREATER_OR_EQUAL_THAN, 50);
95+
$this->assertEquals([$row1, $row2], $this->object->match($collection));
96+
97+
// Test Less or Equal Than
98+
$this->object = new IteratorFilter();
99+
$this->object->addRelation('val', Relation::LESS_OR_EQUAL_THAN, 50);
100+
$this->assertEquals([$row1, $row3, $row4], $this->object->match($collection));
101+
102+
// Test Not Equal
103+
$this->object = new IteratorFilter();
104+
$this->object->addRelation('val', Relation::NOT_EQUAL, 50);
105+
$this->assertEquals([$row2, $row3, $row4], $this->object->match($collection));
106+
107+
// Test Starts With
108+
$this->object = new IteratorFilter();
109+
$this->object->addRelation('field', Relation::STARTS_WITH, 'la');
110+
$this->assertEquals([$row3], $this->object->match($collection));
111+
112+
// Test Contains
113+
$this->object = new IteratorFilter();
114+
$this->object->addRelation('field', Relation::CONTAINS, '1');
115+
$this->assertEquals([$row1, $row2, $row3], $this->object->match($collection));
116+
117+
// Test In
118+
$this->object = new IteratorFilter();
119+
$this->object->addRelation('val', Relation::IN, [10, 30, 50]);
120+
$this->assertEquals([$row1, $row3, $row4], $this->object->match($collection));
121+
122+
// Test Not In
123+
$this->object = new IteratorFilter();
124+
$this->object->addRelation('val', Relation::NOT_IN, [10, 30, 50]);
125+
$this->assertEquals([$row2], $this->object->match($collection));
126+
}
127+
128+
129+
}

tests/IteratorFilterTest.php renamed to tests/IteratorFilterXPathTest.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use ByJG\AnyDataset\Core\Enum\Relation;
99
use PHPUnit\Framework\TestCase;
1010

11-
class IteratorFilterTest extends TestCase
11+
class IteratorFilterXPathTest extends TestCase
1212
{
1313

1414
/**
@@ -51,47 +51,6 @@ public function testGetXPath()
5151
);
5252
}
5353

54-
public function testMatch()
55-
{
56-
57-
$collection = [
58-
$row1 = new Row(
59-
[
60-
'field' => 'value1',
61-
'field2' => 'value2'
62-
]
63-
),
64-
$row2 = new Row(
65-
[
66-
'field' => 'other1',
67-
'field2' => 'other2'
68-
]
69-
),
70-
$row3 = new Row(
71-
[
72-
'field' => 'last1',
73-
'field2' => 'last2'
74-
]
75-
)
76-
];
77-
78-
$this->assertEquals($collection, $this->object->match($collection));
79-
80-
$this->object->addRelation('field2', Relation::EQUAL, 'other2');
81-
$this->assertEquals([ $row2], $this->object->match($collection));
82-
83-
$this->object->addRelationOr('field', Relation::EQUAL, 'last1');
84-
$this->assertEquals([ $row2, $row3], $this->object->match($collection));
85-
86-
87-
//------------------------
88-
89-
$this->object = new IteratorFilter();
90-
$this->object->addRelation('field', Relation::EQUAL, 'last1');
91-
$this->object->addRelation('field2', Relation::EQUAL, 'last2');
92-
$this->assertEquals([ $row3], $this->object->match($collection));
93-
}
94-
9554
public function testAddRelationOr()
9655
{
9756
$this->object->addRelation('field', Relation::EQUAL, 'test');
@@ -114,4 +73,18 @@ public function testGroup()
11473
$this->object->format(new IteratorFilterXPathFormatter())
11574
);
11675
}
76+
77+
public function testIn()
78+
{
79+
$this->expectException(\InvalidArgumentException::class);
80+
$this->object->addRelation('field', Relation::IN, ['test', 'test2']);
81+
$this->object->format(new IteratorFilterXPathFormatter());
82+
}
83+
84+
public function testNotIn()
85+
{
86+
$this->expectException(\InvalidArgumentException::class);
87+
$this->object->addRelation('field', Relation::NOT_IN, ['test', 'test2']);
88+
$this->object->format(new IteratorFilterXPathFormatter());
89+
}
11790
}

tests/RowTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,26 @@ public function testRemoveFieldName()
134134
{
135135
$this->fill();
136136

137+
$this->assertEquals(["field1" => [10, 20, 30], "field2" => 40], $this->object->toArray());
138+
137139
$this->object->removeField('field1');
138140
$this->assertEquals(null, $this->object->get('field1'));
139141
$this->assertEquals(40, $this->object->get('field2'));
142+
143+
$this->assertEquals(["field2" => 40], $this->object->toArray());
140144
}
141145

142146
public function testRemoveFieldName2()
143147
{
144148
$this->fill();
145149

150+
$this->assertEquals(["field1" => [10, 20, 30], "field2" => 40], $this->object->toArray());
151+
146152
$this->object->removeField('field2');
147153
$this->assertEquals(10, $this->object->get('field1'));
148154
$this->assertEquals(null, $this->object->get('field2'));
155+
156+
$this->assertEquals(["field1" => [10, 20, 30]], $this->object->toArray());
149157
}
150158

151159
public function testRemoveFieldNameValue()

0 commit comments

Comments
 (0)