Skip to content

Commit

Permalink
add CallbackCollection class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mori Reo committed Dec 6, 2012
1 parent 83243a3 commit 68285f7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/Goodby/CSV/Export/Standard/Collection/CallbackCollection.php
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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Goodby\CSV\Export\Standard\Collection\PdoCollection;
use Goodby\CSV\TestHelper\DbManager;

use Goodby\CSV\Export\Standard\Collection\CallbackCollection;

class PdoCollectionTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -44,4 +46,23 @@ public function testUsage()
$this->assertEquals("name", $line["name"]);
}
}

public function testUsageWithCallbackCollection()
{
$pdo = $this->manager->getPdo();

$stmt = $pdo->prepare("SELECT * FROM collection_test");
$stmt->execute();

$pdoCollection = new PdoCollection($stmt);

$callbackCollection = new CallbackCollection($pdoCollection, function($row) {
$row['test'] = 'test';
return $row;
});

foreach ($callbackCollection as $line) {
$this->assertEquals('test', $line['test']);
}
}
}
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++;
}
}
}

0 comments on commit 68285f7

Please sign in to comment.