From 383a8aa3077a6439de97aaea7ac2eff7018fb7bc Mon Sep 17 00:00:00 2001 From: Bitdeli Chef Date: Wed, 11 Dec 2013 14:59:25 +0000 Subject: [PATCH 1/3] Add a Bitdeli badge to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 916909d..d89a92e 100644 --- a/README.md +++ b/README.md @@ -245,3 +245,7 @@ License ------- For the full copyright and license information, please view the LICENSE file that was distributed with this source code. + + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/braincrafted/arrayquery/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + From 933e4a7756c977b7b74ecba5232149ff01f80744 Mon Sep 17 00:00:00 2001 From: Devin Date: Mon, 21 Jul 2014 20:33:12 -0700 Subject: [PATCH 2/3] Preserve Keys I needed the keys from the parent array so I added a way to preserve keys using findAll and findScalar --- src/Braincrafted/ArrayQuery/ArrayQuery.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Braincrafted/ArrayQuery/ArrayQuery.php b/src/Braincrafted/ArrayQuery/ArrayQuery.php index b5bdb4e..48e2e52 100644 --- a/src/Braincrafted/ArrayQuery/ArrayQuery.php +++ b/src/Braincrafted/ArrayQuery/ArrayQuery.php @@ -142,9 +142,9 @@ public function where($key, $value, $operator = '=', $filters = array()) * * @return array The full result */ - public function findAll() + public function findAll($preserveKeys = false) { - return $this->execute(); + return $this->execute(false, false, $preserveKeys); } /** @@ -162,9 +162,9 @@ public function findOne() * * @return array An array of scalar values. */ - public function findScalar() + public function findScalar($preserveKeys = false) { - return $this->execute(false, true); + return $this->execute(false, true, $preserveKeys); } /** @@ -184,7 +184,7 @@ public function findOneScalar() * * @throws \InvalidArgumentException when `$scalar` is `true` and more than one field is selected. */ - public function execute($one = false, $scalar = false) + public function execute($one = false, $scalar = false, $preserveKeys = false) { if (true === $scalar && (count($this->select) > 1 || true === isset($this->select['*']))) { throw new \InvalidArgumentException('$scalar can only be true if only one field is selected.'); @@ -192,7 +192,7 @@ public function execute($one = false, $scalar = false) $result = []; - foreach ($this->from as $item) { + foreach ($this->from as $key => $item) { if (true === $this->evaluateWhere($item)) { $resultItem = $this->evaluateSelect($item, $scalar); @@ -200,7 +200,12 @@ public function execute($one = false, $scalar = false) return $resultItem; } - $result[] = $resultItem; + if ($preserveKeys) { + $result[$key] = $resultItem; + } + else { + $result[] = $resultItem; + } } } From 358f386c221bbf0ae1f23ccfb83505d36f8d3b31 Mon Sep 17 00:00:00 2001 From: Devin Dombrowski Date: Wed, 30 Jul 2014 16:04:27 -0700 Subject: [PATCH 3/3] Added tests for preserved keys Added methods testPreserveKeys and testPreserveKeysScalar --- .../ArrayQuery/ArrayQueryTest.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/Braincrafted/ArrayQuery/ArrayQueryTest.php b/tests/Braincrafted/ArrayQuery/ArrayQueryTest.php index e043980..6c2476b 100644 --- a/tests/Braincrafted/ArrayQuery/ArrayQueryTest.php +++ b/tests/Braincrafted/ArrayQuery/ArrayQueryTest.php @@ -322,4 +322,64 @@ public function testFindOneScalar() $this->assertEquals('foo', $result); } + + /** + * @covers Braincrafted\ArrayQuery\ArrayQuery::select() + * @covers Braincrafted\ArrayQuery\ArrayQuery::from() + * @covers Braincrafted\ArrayQuery\ArrayQuery::execute() + * @covers Braincrafted\ArrayQuery\ArrayQuery::where() + * @covers Braincrafted\ArrayQuery\ArrayQuery::evaluateWhere() + * @covers Braincrafted\ArrayQuery\ArrayQuery::findAll() + */ + public function testPreserveKeys() + { + $this->selectEvaluation->shouldReceive('evaluate')->with('foo', m::any())->andReturn('foo'); + + $this->whereEvaluation + ->shouldReceive('evaluate') + ->with([ 'name' => 'foo' ], [ 'key' => 'name', 'value' => 'foo', 'operator' => '=', 'filters' => [] ]) + ->once() + ->andReturn(true); + + $data = [ 'lorem' => [ 'name' => 'foo' ] ]; + $q = (new ArrayQuery($this->selectEvaluation, $this->whereEvaluation)) + ->select('name') + ->from($data) + ->where('name', 'foo', '='); + $result = $q->findAll(true); + + $keys = array_keys($result); + + $this->assertEquals('lorem', $keys[0]); + } + + /** + * @covers Braincrafted\ArrayQuery\ArrayQuery::select() + * @covers Braincrafted\ArrayQuery\ArrayQuery::from() + * @covers Braincrafted\ArrayQuery\ArrayQuery::execute() + * @covers Braincrafted\ArrayQuery\ArrayQuery::where() + * @covers Braincrafted\ArrayQuery\ArrayQuery::evaluateWhere() + * @covers Braincrafted\ArrayQuery\ArrayQuery::findScalar() + */ + public function testPreserveKeysScalar() + { + $this->selectEvaluation->shouldReceive('evaluate')->with('foo', m::any())->andReturn('foo'); + + $this->whereEvaluation + ->shouldReceive('evaluate') + ->with([ 'name' => 'foo' ], [ 'key' => 'name', 'value' => 'foo', 'operator' => '=', 'filters' => [] ]) + ->once() + ->andReturn(true); + + $data = [ 'lorem' => [ 'name' => 'foo' ] ]; + $q = (new ArrayQuery($this->selectEvaluation, $this->whereEvaluation)) + ->select('name') + ->from($data) + ->where('name', 'foo', '='); + $result = $q->findScalar(true); + + $keys = array_keys($result); + + $this->assertEquals('lorem', $keys[0]); + } }