forked from goodby/csv
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Mori Reo
committed
Dec 6, 2012
1 parent
83243a3
commit 68285f7
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/Goodby/CSV/Export/Standard/Collection/CallbackCollection.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,85 @@ | ||
<?php | ||
|
||
namespace Goodby\CSV\Export\Standard\Collection; | ||
|
||
use Iterator; | ||
|
||
class CallbackCollection implements Iterator | ||
{ | ||
private $callable; | ||
private $data; | ||
|
||
public function __construct($data, $callable) | ||
{ | ||
$this->callable = $callable; | ||
|
||
if (!is_callable($callable)) { | ||
throw new \InvalidArgumentException('the second argument must be callable'); | ||
} | ||
|
||
if (is_array($data)) { | ||
$ao = new \ArrayObject($data); | ||
$this->data = $ao->getIterator(); | ||
} elseif ($data instanceof Iterator) { | ||
$this->data = $data; | ||
} else { | ||
throw new \InvalidArgumentException('data must be an array or a Iterator'); | ||
} | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Return the current element | ||
* @link http://php.net/manual/en/iterator.current.php | ||
* @return mixed Can return any type. | ||
*/ | ||
public function current() | ||
{ | ||
return call_user_func($this->callable, $this->data->current()); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Move forward to next element | ||
* @link http://php.net/manual/en/iterator.next.php | ||
* @return void Any returned value is ignored. | ||
*/ | ||
public function next() | ||
{ | ||
$this->data->next(); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Return the key of the current element | ||
* @link http://php.net/manual/en/iterator.key.php | ||
* @return mixed scalar on success, or null on failure. | ||
*/ | ||
public function key() | ||
{ | ||
return $this->data->key(); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Checks if current position is valid | ||
* @link http://php.net/manual/en/iterator.valid.php | ||
* @return boolean The return value will be casted to boolean and then evaluated. | ||
* Returns true on success or false on failure. | ||
*/ | ||
public function valid() | ||
{ | ||
return $this->data->valid(); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Rewind the Iterator to the first element | ||
* @link http://php.net/manual/en/iterator.rewind.php | ||
* @return void Any returned value is ignored. | ||
*/ | ||
public function rewind() | ||
{ | ||
$this->data->rewind(); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/Goodby/CSV/Export/Tests/Standard/Unit/Collection/CallbackCollectionTest.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,27 @@ | ||
<?php | ||
|
||
namespace Goodby\CSV\Export\Tests\Standard\Unit\Collection; | ||
|
||
use Goodby\CSV\Export\Standard\Collection\CallbackCollection; | ||
|
||
class CallbackCollectionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testSample() | ||
{ | ||
$data = array(); | ||
$data[] = array('user', 'name1'); | ||
$data[] = array('user', 'name2'); | ||
$data[] = array('user', 'name3'); | ||
|
||
$collection = new CallbackCollection($data, function($mixed) { | ||
return $mixed; | ||
}); | ||
|
||
$index = 1; | ||
foreach ($collection as $each) { | ||
$this->assertEquals($each[0], 'user'); | ||
$this->assertEquals($each[1], 'name' . $index); | ||
$index++; | ||
} | ||
} | ||
} |