From abb369bd28eeaf04107af02ae49bc659d5961d63 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 16 Nov 2013 16:34:29 -0500 Subject: [PATCH] Make the ResultSetDecorator implement similar methods as ResultSet. Since the decorator is supposed to be an adapter between traversable objects and the ORM\ResultSet class is should implement the same methods as ResultSet. I think there is probably a separate interface that needs to be extracted here. --- Cake/ORM/ResultSetDecorator.php | 49 ++++++++++--------- .../TestCase/ORM/ResultSetDecoratorTest.php | 27 ++++++++++ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Cake/ORM/ResultSetDecorator.php b/Cake/ORM/ResultSetDecorator.php index 1306990bbc4..3d76fdeafcf 100644 --- a/Cake/ORM/ResultSetDecorator.php +++ b/Cake/ORM/ResultSetDecorator.php @@ -17,6 +17,7 @@ namespace Cake\ORM; use \ArrayIterator; +use \Countable; use \IteratorAggregate; use \JsonSerializable; use \Serializable; @@ -28,7 +29,7 @@ * * @return void */ -class ResultSetDecorator implements IteratorAggregate, Serializable, JsonSerializable { +class ResultSetDecorator implements Countable, IteratorAggregate, Serializable, JsonSerializable { use ResultCollectionTrait; @@ -39,21 +40,6 @@ class ResultSetDecorator implements IteratorAggregate, Serializable, JsonSeriali */ protected $_results; -/** - * Internal index pointer. - * - * @var integer - */ - protected $_index = 0; - -/** - * The array form of the results. Used to - * facilitate one(). - * - * @var array - */ - protected $_arrayResults; - /** * Constructor * @@ -78,18 +64,35 @@ public function getIterator() { /** * Get a single result from the results. * - * @return mixed + * Calling this method will convert the underlying data into + * an array and will return the first result in the data set. + * + * @return mixed The first value in the results will be returned. */ public function one() { - if (empty($this->_arrayResults) && !is_array($this->_results)) { - $this->_arrayResults = iterator_to_array($this->_results); + if (!is_array($this->_results)) { + $this->_results = iterator_to_array($this->_results); } - $index = $this->_index; - $this->_index++; - if (!isset($this->_arrayResults[$index])) { + if (count($this->_results) < 1) { return false; } - return $this->_arrayResults[$index]; + return current($this->_results); + } + +/** + * Make this object countable. + * + * Part of the Countable interface. Calling this method + * will convert the underlying traversable object into an array and + * get the count of the underlying data. + * + * @return integer. + */ + public function count() { + if (!is_array($this->_results)) { + $this->_results = iterator_to_array($this->_results); + } + return count($this->_results); } } diff --git a/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php b/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php index a14f392d57c..cfaa3816f2c 100644 --- a/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php +++ b/Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php @@ -69,4 +69,31 @@ public function testSerialization() { $serialized = serialize($decorator); $this->assertEquals([1, 2, 3], unserialize($serialized)->toArray()); } + +/** + * Test the one() method which is part of the ResultSet duck type. + * + * @return void + */ + public function testOne() { + $data = new \ArrayIterator([1, 2, 3]); + $decorator = new ResultSetDecorator($data); + + $this->assertEquals(1, $decorator->one()); + $this->assertEquals(1, $decorator->one()); + } + +/** + * Test the count() method which is part of the ResultSet duck type. + * + * @return void + */ + public function testCount() { + $data = new \ArrayIterator([1, 2, 3]); + $decorator = new ResultSetDecorator($data); + + $this->assertEquals(3, $decorator->count()); + $this->assertCount(3, $decorator); + } + }