Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.9 #16

Merged
merged 2 commits into from
May 30, 2024
Merged

4.9 #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Enum/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ class Relation
* "Contains" unary comparator
*/
const CONTAINS = 7;

/**
* "In" unary comparator
*/
const IN = 8;

/**
* "Not In" unary comparator
*/
const NOT_IN = 9;
}
10 changes: 9 additions & 1 deletion src/IteratorFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ private function evalString(Row $singleRow)
$result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) === 0);
break;

case Relation::IN:
$result[$pos] = $result[$pos] && in_array($valueparam, $value);
break;

case Relation::NOT_IN:
$result[$pos] = $result[$pos] && !in_array($valueparam, $value);
break;

default: // Relation::CONTAINS:
$result[$pos] = $result[$pos] && (strpos(is_null($valueparam) ? "" : $valueparam, $value) !== false);
break;
Expand All @@ -129,7 +137,7 @@ private function evalString(Row $singleRow)
/**
* @param string $name Field name
* @param int $relation Relation enum
* @param string $value Field string value
* @param string|array $value Field string value
* @return IteratorFilter
* @desc Add a single string comparison to filter.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/IteratorFilterFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class IteratorFilterFormatter
*
* @param string $name
* @param string $relation
* @param string $value
* @param string|array $value
* @param array $param
* @return string
*/
Expand Down
10 changes: 9 additions & 1 deletion src/IteratorFilterXPathFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function getRelation($name, $relation, $value, &$param)
{
$str = is_numeric($value) ? "" : "'";
$field = "field[@name='" . $name . "'] ";
$value = " $str$value$str ";
if (is_string($value)) {
$value = " $str$value$str ";
}

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

case Relation::IN:
throw new \InvalidArgumentException('XPath does not support IN');

case Relation::NOT_IN:
throw new \InvalidArgumentException('XPath does not support NOT IN');

default: // Relation::CONTAINS:
$return = " contains($field, $value) ";
break;
Expand Down
129 changes: 129 additions & 0 deletions tests/IteratorFilterAnydatasetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Tests\AnyDataset\Dataset;

use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Core\IteratorFilterXPathFormatter;
use ByJG\AnyDataset\Core\Row;
use ByJG\AnyDataset\Core\Enum\Relation;
use PHPUnit\Framework\TestCase;

class IteratorFilterAnydatasetTest extends TestCase
{

/**
* @var IteratorFilter
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
$this->object = new IteratorFilter();
}

public function testMatch()
{

$collection = [
$row1 = new Row(
[
'id' => 1,
'field' => 'value1',
'field2' => 'value2',
'val' => 50,
]
),
$row2 = new Row(
[
'id' => 2,
'field' => 'other1',
'field2' => 'other2',
'val' => 80,
]
),
$row3 = new Row(
[
'id' => 3,
'field' => 'last1',
'field2' => 'last2',
'val' => 30,
]
),
$row4 = new Row(
[
'id' => 4,
'field' => 'xy',
'field2' => 'zy',
'val' => 10,
]
),
];

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

$this->object->addRelation('field2', Relation::EQUAL, 'other2');
$this->assertEquals([$row2], $this->object->match($collection));

$this->object->addRelationOr('field', Relation::EQUAL, 'last1');
$this->assertEquals([$row2, $row3], $this->object->match($collection));


//------------------------

$this->object = new IteratorFilter();
$this->object->addRelation('field', Relation::EQUAL, 'last1');
$this->object->addRelation('field2', Relation::EQUAL, 'last2');
$this->assertEquals([$row3], $this->object->match($collection));

// Test Greater Than
$this->object = new IteratorFilter();
$this->object->addRelation('val', Relation::GREATER_THAN, 50);
$this->assertEquals([$row2], $this->object->match($collection));

// Test Less Than
$this->object = new IteratorFilter();
$this->object->addRelation('val', Relation::LESS_THAN, 50);
$this->assertEquals([$row3, $row4], $this->object->match($collection));

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

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

// Test Not Equal
$this->object = new IteratorFilter();
$this->object->addRelation('val', Relation::NOT_EQUAL, 50);
$this->assertEquals([$row2, $row3, $row4], $this->object->match($collection));

// Test Starts With
$this->object = new IteratorFilter();
$this->object->addRelation('field', Relation::STARTS_WITH, 'la');
$this->assertEquals([$row3], $this->object->match($collection));

// Test Contains
$this->object = new IteratorFilter();
$this->object->addRelation('field', Relation::CONTAINS, '1');
$this->assertEquals([$row1, $row2, $row3], $this->object->match($collection));

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

// Test Not In
$this->object = new IteratorFilter();
$this->object->addRelation('val', Relation::NOT_IN, [10, 30, 50]);
$this->assertEquals([$row2], $this->object->match($collection));
}


}
57 changes: 15 additions & 42 deletions tests/IteratorFilterTest.php → tests/IteratorFilterXPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ByJG\AnyDataset\Core\Enum\Relation;
use PHPUnit\Framework\TestCase;

class IteratorFilterTest extends TestCase
class IteratorFilterXPathTest extends TestCase
{

/**
Expand Down Expand Up @@ -51,47 +51,6 @@ public function testGetXPath()
);
}

public function testMatch()
{

$collection = [
$row1 = new Row(
[
'field' => 'value1',
'field2' => 'value2'
]
),
$row2 = new Row(
[
'field' => 'other1',
'field2' => 'other2'
]
),
$row3 = new Row(
[
'field' => 'last1',
'field2' => 'last2'
]
)
];

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

$this->object->addRelation('field2', Relation::EQUAL, 'other2');
$this->assertEquals([ $row2], $this->object->match($collection));

$this->object->addRelationOr('field', Relation::EQUAL, 'last1');
$this->assertEquals([ $row2, $row3], $this->object->match($collection));


//------------------------

$this->object = new IteratorFilter();
$this->object->addRelation('field', Relation::EQUAL, 'last1');
$this->object->addRelation('field2', Relation::EQUAL, 'last2');
$this->assertEquals([ $row3], $this->object->match($collection));
}

public function testAddRelationOr()
{
$this->object->addRelation('field', Relation::EQUAL, 'test');
Expand All @@ -114,4 +73,18 @@ public function testGroup()
$this->object->format(new IteratorFilterXPathFormatter())
);
}

public function testIn()
{
$this->expectException(\InvalidArgumentException::class);
$this->object->addRelation('field', Relation::IN, ['test', 'test2']);
$this->object->format(new IteratorFilterXPathFormatter());
}

public function testNotIn()
{
$this->expectException(\InvalidArgumentException::class);
$this->object->addRelation('field', Relation::NOT_IN, ['test', 'test2']);
$this->object->format(new IteratorFilterXPathFormatter());
}
}
8 changes: 8 additions & 0 deletions tests/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,26 @@ public function testRemoveFieldName()
{
$this->fill();

$this->assertEquals(["field1" => [10, 20, 30], "field2" => 40], $this->object->toArray());

$this->object->removeField('field1');
$this->assertEquals(null, $this->object->get('field1'));
$this->assertEquals(40, $this->object->get('field2'));

$this->assertEquals(["field2" => 40], $this->object->toArray());
}

public function testRemoveFieldName2()
{
$this->fill();

$this->assertEquals(["field1" => [10, 20, 30], "field2" => 40], $this->object->toArray());

$this->object->removeField('field2');
$this->assertEquals(10, $this->object->get('field1'));
$this->assertEquals(null, $this->object->get('field2'));

$this->assertEquals(["field1" => [10, 20, 30]], $this->object->toArray());
}

public function testRemoveFieldNameValue()
Expand Down