diff --git a/src/QueryExpressionFilter.php b/src/QueryExpressionFilter.php index b848b4d..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) @@ -133,8 +134,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..786f2e8 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,68 @@ public function testInvalidOr() $filter->doesMatch(array('id' => 100)); } + + /** + * @dataProvider dpFetchValue + */ + public function testFetchValue($data, $fetch, $expected, $separator = '.') + { + $filter = new QueryExpressionFilter(array(), $separator); + $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 + ), + ); + } }