Skip to content

Commit 1ab492b

Browse files
committed
Renamed IteratorFilter::addRelation to IteratorFilter::and; Renamed IteratorFilter::addRelationOr to IteratorFilter::or;
1 parent d0bb1f0 commit 1ab492b

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
$filter = new \ByJG\AnyDataset\Core\IteratorFilter();
13-
$filter->addRelation("field1", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, 10);
13+
$filter->and("field1", \ByJG\AnyDataset\Core\Enum\Relation::EQUAL, 10);
1414
$iterator2 = $dataset->getIterator($filter);
1515

1616
$iterator->toArray();

src/IteratorFilter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ private function evalString(Row $singleRow): bool
111111
* @param mixed $value Field string value
112112
* @return static
113113
* @desc Add a single string comparison to filter.
114+
* @deprecated use and() instead
114115
*/
115116
public function addRelation(string $name, Relation $relation, mixed $value): static
117+
{
118+
return $this->and($name, $relation, $value);
119+
}
120+
121+
/**
122+
* @param string $name Field name
123+
* @param Relation $relation Relation enum
124+
* @param mixed $value Field string value
125+
* @return static
126+
* @desc Add a single string comparison to filter.
127+
*/
128+
public function and(string $name, Relation $relation, mixed $value): static
116129
{
117130
$this->filters[] = [" and ", $name, $relation, $value];
118131
return $this;
@@ -124,8 +137,21 @@ public function addRelation(string $name, Relation $relation, mixed $value): sta
124137
* @param mixed $value Field string value
125138
* @return static
126139
* @desc Add a single string comparison to filter. This comparison use the OR operator.
140+
* @deprecated use or() instead
127141
*/
128142
public function addRelationOr(string $name, Relation $relation, mixed $value): static
143+
{
144+
return $this->or($name, $relation, $value);
145+
}
146+
147+
/**
148+
* @param string $name Field name
149+
* @param Relation $relation Relation enum
150+
* @param mixed $value Field string value
151+
* @return static
152+
* @desc Add a single string comparison to filter. This comparison use the OR operator.
153+
*/
154+
public function or(string $name, Relation $relation, mixed $value): static
129155
{
130156
$this->filters[] = [" or ", $name, $relation, $value];
131157
return $this;

tests/AnyDatasetTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use ByJG\AnyDataset\Core\IteratorFilter;
1010
use ByJG\XmlUtil\XmlDocument;
1111
use PHPUnit\Framework\TestCase;
12-
use PHPUnit\Util\Xml;
1312

1413
class AnyDatasetTest extends TestCase
1514
{
@@ -306,7 +305,7 @@ public function testIteratorFilter()
306305
$this->object->appendRow(['name' => 'jg jr', 'age' => 4]);
307306

308307
$filter = IteratorFilter::getInstance()
309-
->addRelation("age", Relation::LESS_THAN, 40);
308+
->and("age", Relation::LESS_THAN, 40);
310309

311310
$this->assertEquals([
312311
['name' => 'jf', 'age' => 15],

tests/IteratorFilterAnydatasetTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Tests\AnyDataset\Dataset;
44

5+
use ByJG\AnyDataset\Core\Enum\Relation;
56
use ByJG\AnyDataset\Core\IteratorFilter;
6-
use ByJG\AnyDataset\Core\IteratorFilterXPathFormatter;
77
use ByJG\AnyDataset\Core\Row;
8-
use ByJG\AnyDataset\Core\Enum\Relation;
98
use PHPUnit\Framework\TestCase;
109

1110
class IteratorFilterAnydatasetTest extends TestCase
@@ -65,63 +64,63 @@ public function testMatch()
6564

6665
$this->assertEquals($collection, $this->object->match($collection));
6766

68-
$this->object->addRelation('field2', Relation::EQUAL, 'other2');
67+
$this->object->and('field2', Relation::EQUAL, 'other2');
6968
$this->assertEquals([$row2], $this->object->match($collection));
7069

71-
$this->object->addRelationOr('field', Relation::EQUAL, 'last1');
70+
$this->object->or('field', Relation::EQUAL, 'last1');
7271
$this->assertEquals([$row2, $row3], $this->object->match($collection));
7372

7473

7574
//------------------------
7675

7776
$this->object = new IteratorFilter();
78-
$this->object->addRelation('field', Relation::EQUAL, 'last1');
79-
$this->object->addRelation('field2', Relation::EQUAL, 'last2');
77+
$this->object->and('field', Relation::EQUAL, 'last1');
78+
$this->object->and('field2', Relation::EQUAL, 'last2');
8079
$this->assertEquals([$row3], $this->object->match($collection));
8180

8281
// Test Greater Than
8382
$this->object = new IteratorFilter();
84-
$this->object->addRelation('val', Relation::GREATER_THAN, 50);
83+
$this->object->and('val', Relation::GREATER_THAN, 50);
8584
$this->assertEquals([$row2], $this->object->match($collection));
8685

8786
// Test Less Than
8887
$this->object = new IteratorFilter();
89-
$this->object->addRelation('val', Relation::LESS_THAN, 50);
88+
$this->object->and('val', Relation::LESS_THAN, 50);
9089
$this->assertEquals([$row3, $row4], $this->object->match($collection));
9190

9291
// Test Greater or Equal Than
9392
$this->object = new IteratorFilter();
94-
$this->object->addRelation('val', Relation::GREATER_OR_EQUAL_THAN, 50);
93+
$this->object->and('val', Relation::GREATER_OR_EQUAL_THAN, 50);
9594
$this->assertEquals([$row1, $row2], $this->object->match($collection));
9695

9796
// Test Less or Equal Than
9897
$this->object = new IteratorFilter();
99-
$this->object->addRelation('val', Relation::LESS_OR_EQUAL_THAN, 50);
98+
$this->object->and('val', Relation::LESS_OR_EQUAL_THAN, 50);
10099
$this->assertEquals([$row1, $row3, $row4], $this->object->match($collection));
101100

102101
// Test Not Equal
103102
$this->object = new IteratorFilter();
104-
$this->object->addRelation('val', Relation::NOT_EQUAL, 50);
103+
$this->object->and('val', Relation::NOT_EQUAL, 50);
105104
$this->assertEquals([$row2, $row3, $row4], $this->object->match($collection));
106105

107106
// Test Starts With
108107
$this->object = new IteratorFilter();
109-
$this->object->addRelation('field', Relation::STARTS_WITH, 'la');
108+
$this->object->and('field', Relation::STARTS_WITH, 'la');
110109
$this->assertEquals([$row3], $this->object->match($collection));
111110

112111
// Test Contains
113112
$this->object = new IteratorFilter();
114-
$this->object->addRelation('field', Relation::CONTAINS, '1');
113+
$this->object->and('field', Relation::CONTAINS, '1');
115114
$this->assertEquals([$row1, $row2, $row3], $this->object->match($collection));
116115

117116
// Test In
118117
$this->object = new IteratorFilter();
119-
$this->object->addRelation('val', Relation::IN, [10, 30, 50]);
118+
$this->object->and('val', Relation::IN, [10, 30, 50]);
120119
$this->assertEquals([$row1, $row3, $row4], $this->object->match($collection));
121120

122121
// Test Not In
123122
$this->object = new IteratorFilter();
124-
$this->object->addRelation('val', Relation::NOT_IN, [10, 30, 50]);
123+
$this->object->and('val', Relation::NOT_IN, [10, 30, 50]);
125124
$this->assertEquals([$row2], $this->object->match($collection));
126125
}
127126

tests/IteratorFilterXPathTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Tests;
44

5+
use ByJG\AnyDataset\Core\Enum\Relation;
56
use ByJG\AnyDataset\Core\IteratorFilter;
67
use ByJG\AnyDataset\Core\IteratorFilterXPathFormatter;
7-
use ByJG\AnyDataset\Core\Row;
8-
use ByJG\AnyDataset\Core\Enum\Relation;
98
use PHPUnit\Framework\TestCase;
109

1110
class IteratorFilterXPathTest extends TestCase
@@ -32,19 +31,19 @@ public function testGetXPath()
3231
$this->object->format(new IteratorFilterXPathFormatter())
3332
);
3433

35-
$this->object->addRelation('field', Relation::EQUAL, 'test');
34+
$this->object->and('field', Relation::EQUAL, 'test');
3635
$this->assertEquals(
3736
"/anydataset/row[field[@name='field'] = 'test' ]",
3837
$this->object->format(new IteratorFilterXPathFormatter())
3938
);
4039

41-
$this->object->addRelation('field2', Relation::GREATER_OR_EQUAL_THAN, 'test2');
40+
$this->object->and('field2', Relation::GREATER_OR_EQUAL_THAN, 'test2');
4241
$this->assertEquals(
4342
"/anydataset/row[field[@name='field'] = 'test' and field[@name='field2'] >= 'test2' ]",
4443
$this->object->format(new IteratorFilterXPathFormatter())
4544
);
4645

47-
$this->object->addRelation('field3', Relation::CONTAINS, 'test3');
46+
$this->object->and('field3', Relation::CONTAINS, 'test3');
4847
$this->assertEquals(
4948
"/anydataset/row[field[@name='field'] = 'test' and field[@name='field2'] >= 'test2' and contains(field[@name='field3'] , 'test3' ) ]",
5049
$this->object->format(new IteratorFilterXPathFormatter())
@@ -53,8 +52,8 @@ public function testGetXPath()
5352

5453
public function testAddRelationOr()
5554
{
56-
$this->object->addRelation('field', Relation::EQUAL, 'test');
57-
$this->object->addRelationOr('field2', Relation::EQUAL, 'test2');
55+
$this->object->and('field', Relation::EQUAL, 'test');
56+
$this->object->or('field2', Relation::EQUAL, 'test2');
5857
$this->assertEquals(
5958
"/anydataset/row[field[@name='field'] = 'test' or field[@name='field2'] = 'test2' ]",
6059
$this->object->format(new IteratorFilterXPathFormatter())
@@ -64,10 +63,10 @@ public function testAddRelationOr()
6463
public function testGroup()
6564
{
6665
$this->object->startGroup();
67-
$this->object->addRelation('field', Relation::EQUAL, 'test');
68-
$this->object->addRelation('field2', Relation::EQUAL, 'test2');
66+
$this->object->and('field', Relation::EQUAL, 'test');
67+
$this->object->and('field2', Relation::EQUAL, 'test2');
6968
$this->object->endGroup();
70-
$this->object->addRelationOr('field3', Relation::EQUAL, 'test3');
69+
$this->object->or('field3', Relation::EQUAL, 'test3');
7170
$this->assertEquals(
7271
"/anydataset/row[ ( field[@name='field'] = 'test' and field[@name='field2'] = 'test2' ) or field[@name='field3'] = 'test3' ]",
7372
$this->object->format(new IteratorFilterXPathFormatter())
@@ -77,14 +76,14 @@ public function testGroup()
7776
public function testIn()
7877
{
7978
$this->expectException(\InvalidArgumentException::class);
80-
$this->object->addRelation('field', Relation::IN, ['test', 'test2']);
79+
$this->object->and('field', Relation::IN, ['test', 'test2']);
8180
$this->object->format(new IteratorFilterXPathFormatter());
8281
}
8382

8483
public function testNotIn()
8584
{
8685
$this->expectException(\InvalidArgumentException::class);
87-
$this->object->addRelation('field', Relation::NOT_IN, ['test', 'test2']);
86+
$this->object->and('field', Relation::NOT_IN, ['test', 'test2']);
8887
$this->object->format(new IteratorFilterXPathFormatter());
8988
}
9089
}

0 commit comments

Comments
 (0)