Skip to content

Commit

Permalink
Moving some code around
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 26, 2013
1 parent 21f2edb commit 2ddd2d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
22 changes: 2 additions & 20 deletions Cake/ORM/ResultCollectionTrait.php
Expand Up @@ -16,8 +16,8 @@
*/
namespace Cake\ORM;

use \Cake\Utility\CollectionTrait;
use \Iterator;
use \JsonSerializable;
use \Serializable;

/**
Expand All @@ -26,14 +26,7 @@
*/
trait ResultCollectionTrait {

/**
* Returns an array representation of the results
*
* @return array
*/
public function toArray() {
return iterator_to_array($this);
}
use CollectionTrait;

/**
* Serialize a resultset.
Expand All @@ -58,15 +51,4 @@ public function unserialize($serialized) {
$this->_results = unserialize($serialized);
}

/**
* Convert a result set into JSON.
*
* Part of JsonSerializable interface.
*
* @return array The data to convert to JSON
*/
public function jsonSerialize() {
return $this->toArray();
}

}
4 changes: 4 additions & 0 deletions Cake/Test/TestCase/Utility/CollectionTest.php
Expand Up @@ -17,6 +17,10 @@
use Cake\TestSuite\TestCase;
use Cake\Utility\Collection;

/**
* CollectionTest
*
*/
class CollectionTest extends TestCase {

/**
Expand Down
3 changes: 2 additions & 1 deletion Cake/Utility/Collection.php
Expand Up @@ -23,13 +23,14 @@
use Cake\Utility\MapReduce;
use InvalidArgumentException;
use IteratorIterator;
use \JsonSerializable;
use LimitIterator;

/**
* A collection is an immutable list of elements with a handful of functions to
* iterate, group, transform and extract information from it.
*/
class Collection extends IteratorIterator {
class Collection extends IteratorIterator implements JsonSerializable {

use CollectionTrait;

Expand Down
21 changes: 20 additions & 1 deletion Cake/Utility/CollectionTrait.php
Expand Up @@ -22,7 +22,6 @@
use Cake\Utility\MapReduce;
use LimitIterator;


/**
* Offers a handful of method to manipulate iterators
*/
Expand Down Expand Up @@ -528,6 +527,26 @@ public function sample($size = 10) {
return new Collection(new LimitIterator($this, 0, $size));
}

/**
* Returns an array representation of the results
*
* @return array
*/
public function toArray() {
return iterator_to_array($this);
}

/**
* Convert a result set into JSON.
*
* Part of JsonSerializable interface.
*
* @return array The data to convert to JSON
*/
public function jsonSerialize() {
return $this->toArray();
}

/**
* Returns a callable that can be used to extract a property or column from
* an array or object based on a dot separated path.
Expand Down

0 comments on commit 2ddd2d3

Please sign in to comment.