Skip to content
Permalink
Browse files

Implemented Collection::filter()

  • Loading branch information...
lorenzo committed Dec 25, 2013
1 parent 0efeef5 commit de593156a11e5422eb55f560bd7571c3fa16078d
@@ -61,4 +61,25 @@ public function testEeach() {
->with(3, 'c');
$collection->each($callable);
}
/**
* Tests that it is possible to chain filter() as it returns a collection object
*
* @return void
*/
public function testFilterChaining() {
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->once())
->method('__invoke')
->with(3, 'c');
$filtered = $collection->filter(function ($value, $key, $iterator) {
return $value > 2;
});
$this->assertInstanceOf('\Cake\Utility\Collection', $filtered);
$filtered->each($callable);
}
}
@@ -0,0 +1,42 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Utility\Iterator;
use Cake\TestSuite\TestCase;
use Cake\Utility\Iterator\FilterIterator;
class FilterIteratorTest extends TestCase {
public function testFilter() {
$items = new \ArrayIterator([1, 2, 3]);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->at(0))
->method('__invoke')
->with(1, 0, $items)
->will($this->returnValue(false));
$callable->expects($this->at(1))
->method('__invoke')
->with(2, 1, $items)
->will($this->returnValue(true));
$callable->expects($this->at(2))
->method('__invoke')
->with(3, 2, $items)
->will($this->returnValue(false));
$filter = new FilterIterator($items, $callable);
$this->assertEquals([1 => 2], iterator_to_array($filter));
}
}
@@ -14,9 +14,10 @@
*/
namespace Cake\Utility;
use \ArrayIterator;
use \InvalidArgumentException;
use \IteratorIterator;
use ArrayIterator;
use Cake\Utility\Iterator\FilterIterator;
use InvalidArgumentException;
use IteratorIterator;
class Collection extends IteratorIterator {
@@ -59,8 +60,21 @@ public function each(callable $c) {
return $this;
}
/**
* Looks through each value in the collection, and returns another collection with
* all the values that pass a truth test. Only the values for which the callback
* returns true will be present in the resulting collection.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration, the key of the element and the passed $items iterator
* as arguments, in that order.
*
* @param callable $c the method that will receive each of the elements and
* returns true whether or not they should be in the resulting collection.
* @return \Cake\Utility\Iterator\FilterIterator;
*/
public function filter(callable $c) {
return new FilterIterator($this, $c);
}
public function some(callable $c) {
@@ -0,0 +1,43 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Utility\Iterator;
use Cake\Utility\Collection;
use CallbackFilterIterator;
use Iterator;
/**
* Create a filtered iterator from another iterator. The filtering is done by
* passing a callback function to each of the elements and taking them out if
* it does not return true.
*/
class FilterIterator extends Collection {
/**
* Creates a filtered iterator using the callback to determine which items are
* accepted or rejected.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration, the key of the element and the passed $items iterator
* as arguments, in that order.
*
* @return void
*/
public function __construct(Iterator $items, callable $callback) {
$wrapper = new CallbackFilterIterator($items, $callback);
parent::__construct($wrapper);
}
}

0 comments on commit de59315

Please sign in to comment.
You can’t perform that action at this time.