Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Cake/Test/TestCase/Utility/Iterator/ReplaceIteratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\ReplaceIterator; | ||
|
||
/** | ||
* ReplaceIterator Test | ||
* | ||
*/ | ||
class ReplaceIteratorTest extends TestCase { | ||
|
||
/** | ||
* Tests that the iterator works correctly | ||
* | ||
* @return void | ||
*/ | ||
public function testReplace() { | ||
$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(1)); | ||
$callable->expects($this->at(1)) | ||
->method('__invoke') | ||
->with(2, 1, $items) | ||
->will($this->returnValue(4)); | ||
$callable->expects($this->at(2)) | ||
->method('__invoke') | ||
->with(3, 2, $items) | ||
->will($this->returnValue(9)); | ||
|
||
$map = new ReplaceIterator($items, $callable); | ||
$this->assertEquals([1, 4, 9], iterator_to_array($map)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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; | ||
|
||
/** | ||
* Creates an iterator from another iterator that will modify each of the values | ||
* by converting them using a callback function. | ||
*/ | ||
class ReplaceIterator extends Collection { | ||
|
||
/** | ||
* The callback function to be used to modify each of the values | ||
* | ||
* @var callable | ||
*/ | ||
protected $_callback; | ||
|
||
/** | ||
* Creates an iterator from another iterator that will modify each of the values | ||
* by converting them using a callback function. | ||
* | ||
* 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 array|\Traversable $items the items to be filtered | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function __construct($items, callable $callback) { | ||
$this->_callback = $callback; | ||
parent::__construct($items); | ||
} | ||
|
||
/** | ||
* Returns the value returned by the callback after passing the current value in | ||
* the iteration | ||
* | ||
* @return mixed | ||
*/ | ||
public function current() { | ||
$callback = $this->_callback; | ||
return $callback(parent::current(), $this->key(), $this->getInnerIterator()); | ||
} | ||
|
||
} |