From 24a1eafab3e76fa41a5b9d93f5aaf0d8d22216c1 Mon Sep 17 00:00:00 2001 From: maximkou Date: Thu, 28 Aug 2014 17:22:06 +0400 Subject: [PATCH 1/2] Dotted column notation --- src/QueryExpressionFilter.php | 14 ++++- tests/QueryExpressionFilterTest.php | 79 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/QueryExpressionFilter.php b/src/QueryExpressionFilter.php index b848b4d..2bddb0c 100644 --- a/src/QueryExpressionFilter.php +++ b/src/QueryExpressionFilter.php @@ -133,8 +133,18 @@ private function matchValue($data, $column, $expectation) private function fetchValue($data, $column) { - // TODO: dotted notation - return isset($data[$column]) ? $data[$column] : null; + $path = explode($this->selectorSeparator, $column); + + $current = $data; + foreach ($path as $field) { + if (is_array($current) && isset($current[$field])) { + $current = $current[$field]; + } else { + return null; + } + } + + return $current; } private function isObject($value) diff --git a/tests/QueryExpressionFilterTest.php b/tests/QueryExpressionFilterTest.php index 46ba0d9..703ade6 100644 --- a/tests/QueryExpressionFilterTest.php +++ b/tests/QueryExpressionFilterTest.php @@ -18,6 +18,22 @@ public function testAttributeValue() 'id' => 200 ))); + $filterNested = new QueryExpressionFilter(array( + 'nested.attribute' => 250 + )); + + $this->assertTrue($filterNested->doesMatch(array( + 'nested' => array( + 'attribute' => 250 + ) + ))); + + $this->assertFalse($filterNested->doesMatch(array( + 'nested' => array( + 'attribute' => 300 + ) + ))); + return $filter; } @@ -363,4 +379,67 @@ public function testInvalidOr() $filter->doesMatch(array('id' => 100)); } + + /** + * @dataProvider dpFetchValue + */ + public function testFetchValue($data, $fetch, $expected) + { + $filter = new QueryExpressionFilter(array()); + $method = new \ReflectionMethod($filter, 'fetchValue'); + $method->setAccessible(true); + + $this->assertEquals( + $expected, + $method->invoke($filter, $data, $fetch) + ); + } + + public function dpFetchValue() + { + return array( + array( + array('id' => 1), + 'id', + 1 + ), + array( + array( + 'org' => array( + 'peoples' => array( + 'john' => array( + 'weight' => 80 + ) + ) + ) + ), + 'org.peoples.john', + array( + 'weight' => 80 + ) + ), + array( + array( + 'org' => array( + 'peoples' => array( + 'john' => array( + 'weight' => 80 + ) + ), + 'computers' => array( + 'pc' => 'Intel', + 'macbook' => 'Macbook Pro' + ) + ) + ), + 'org.computers.pc', + 'Intel' + ), + array( + array('id' => 1), + 'unexpected', + null + ), + ); + } } From 07a82730389278ae7da46253926d5ba815be8ebd Mon Sep 17 00:00:00 2001 From: maximkou Date: Thu, 28 Aug 2014 17:32:34 +0400 Subject: [PATCH 2/2] Allow to use custom separator --- src/QueryExpressionFilter.php | 5 +++-- tests/QueryExpressionFilterTest.php | 9 +++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/QueryExpressionFilter.php b/src/QueryExpressionFilter.php index 2bddb0c..a0d63f6 100644 --- a/src/QueryExpressionFilter.php +++ b/src/QueryExpressionFilter.php @@ -7,11 +7,12 @@ class QueryExpressionFilter implements Filter { private $queryExpression; - private $selectorSeparator = '.'; + private $selectorSeparator; - public function __construct($queryExpression) + public function __construct($queryExpression, $selectorSeparator = '.') { $this->queryExpression = $queryExpression; + $this->selectorSeparator = $selectorSeparator; } public function doesMatch($data) diff --git a/tests/QueryExpressionFilterTest.php b/tests/QueryExpressionFilterTest.php index 703ade6..786f2e8 100644 --- a/tests/QueryExpressionFilterTest.php +++ b/tests/QueryExpressionFilterTest.php @@ -383,9 +383,9 @@ public function testInvalidOr() /** * @dataProvider dpFetchValue */ - public function testFetchValue($data, $fetch, $expected) + public function testFetchValue($data, $fetch, $expected, $separator = '.') { - $filter = new QueryExpressionFilter(array()); + $filter = new QueryExpressionFilter(array(), $separator); $method = new \ReflectionMethod($filter, 'fetchValue'); $method->setAccessible(true); @@ -432,8 +432,9 @@ public function dpFetchValue() ) ) ), - 'org.computers.pc', - 'Intel' + 'org:computers:pc', + 'Intel', + ':' ), array( array('id' => 1),