Skip to content

Commit

Permalink
Make the ResultSetDecorator implement similar methods as ResultSet.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Nov 16, 2013
1 parent 862b2a2 commit abb369b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
49 changes: 26 additions & 23 deletions Cake/ORM/ResultSetDecorator.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\ORM;

use \ArrayIterator;
use \Countable;
use \IteratorAggregate;
use \JsonSerializable;
use \Serializable;
Expand All @@ -28,7 +29,7 @@
*
* @return void
*/
class ResultSetDecorator implements IteratorAggregate, Serializable, JsonSerializable {
class ResultSetDecorator implements Countable, IteratorAggregate, Serializable, JsonSerializable {

use ResultCollectionTrait;

Expand All @@ -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
*
Expand All @@ -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);
}

}
27 changes: 27 additions & 0 deletions Cake/Test/TestCase/ORM/ResultSetDecoratorTest.php
Expand Up @@ -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);
}

}

0 comments on commit abb369b

Please sign in to comment.