Skip to content

Commit

Permalink
Merge branch '4.9' into 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Apr 5, 2024
2 parents 00c4843 + 2bd2cb0 commit 30b8a35
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 48 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Anydataset Core Module. Anydataset is an agnostic data source abstraction layer

## Current Implementations

{:.table}

| Object | Data Source | Read | Write | Reference |
|------------------------|-----------------------|:----:|:-----:|-----------------------------------------------------|
| DbDriverInterface | Relational DB | yes | yes | [Github](https://github.com/byjg/anydataset-db) |
Expand Down Expand Up @@ -199,4 +197,4 @@ flowchart TD
```

----
[Open source ByJG](http://opensource.byjg.com)
[Open source ByJG](http://opensource.byjg.com)
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());
}
}

0 comments on commit 30b8a35

Please sign in to comment.