Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Hydrating Iterator #13

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Hydrator/Iterator/HydratingArrayIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator\Iterator;

use ArrayIterator;
use Zend\Stdlib\Hydrator\HydratorInterface;

class HydratingArrayIterator extends HydratingIteratorIterator
{
/**
* @var HydratorInterface
*/
protected $hydrator;

/**
* @var object
*/
protected $prototype;

public function __construct(HydratorInterface $hydrator, array $data, $prototype)
{
parent::__construct($hydrator, new ArrayIterator($data), $prototype);
}
}
31 changes: 31 additions & 0 deletions src/Hydrator/Iterator/HydratingIteratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator\Iterator;

use Iterator;
use Zend\Stdlib\Hydrator\HydratorInterface;

interface HydratingIteratorInterface extends Iterator
{
/**
* This sets the prototype of that will be hydrated. This can be the name of the class or
* the object itself. The iterator will clone the object
*
* @param string|object $prototype
*/
public function setPrototype($prototype);

/**
* Sets the hydrator to use for the prototype
*
* @param HydratorInterface $hydrator
*/
public function setHydrator(HydratorInterface $hydrator);
}
64 changes: 64 additions & 0 deletions src/Hydrator/Iterator/HydratingIteratorIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Hydrator\Iterator;

use Iterator;
use IteratorIterator;
use Zend\Stdlib\Exception\InvalidArgumentException;
use Zend\Stdlib\Hydrator\HydratorInterface;

class HydratingIteratorIterator extends IteratorIterator implements HydratingIteratorInterface
{
/**
* @var HydratorInterface
*/
protected $hydrator;

/**
* @var object
*/
protected $prototype;

public function __construct(HydratorInterface $hydrator, Iterator $data, $prototype)
{
$this->setHydrator($hydrator);
$this->setPrototype($prototype);
parent::__construct($data);
}

public function setPrototype($prototype)
{
if (is_object($prototype)) {
$this->prototype = $prototype;
return;
}

if (!class_exists($prototype)) {
throw new InvalidArgumentException(
sprintf('Method %s was passed an invalid class name: %s', __METHOD__, $prototype)
);
}

$this->prototype = new $prototype;
}

public function setHydrator(HydratorInterface $hydrator)
{
$this->hydrator = $hydrator;
}

public function current()
{
$currentValue = parent::current();
$object = clone $this->prototype;
$this->hydrator->hydrate($currentValue, $object);
return $object;
}
}
58 changes: 58 additions & 0 deletions test/Hydrator/Iterator/HydratingArrayIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Hydrator\Iterator;

use ArrayObject;
use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\Iterator\HydratingArrayIterator;

class HydratingArrayIteratorTest extends \PHPUnit_Framework_TestCase
{
public function testHydratesObjectAndClonesOnCurrent()
{
$data = [
['foo' => 'bar'],
['baz' => 'bat']
];

$object = new ArrayObject();

$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), $data, $object);

$hydratingIterator->rewind();
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
$this->assertNotSame(
$object,
$hydratingIterator->current(),
'Hydrating Iterator did not clone the object'
);

$hydratingIterator->next();
$this->assertEquals(new ArrayObject($data[1]), $hydratingIterator->current());
}

public function testUsingStringForObjectName()
{
$data = [
['foo' => 'bar']
];

$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), $data, '\ArrayObject');

$hydratingIterator->rewind();
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
}

public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass()
{
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), [], 'not a real class');
}
}
62 changes: 62 additions & 0 deletions test/Hydrator/Iterator/HydratingIteratorIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Hydrator\Iterator;

use ArrayIterator;
use ArrayObject;
use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\Iterator\HydratingIteratorIterator;

class HydratingIteratorIteratorTest extends \PHPUnit_Framework_TestCase
{
public function testHydratesObjectAndClonesOnCurrent()
{
$data = [
['foo' => 'bar'],
['baz' => 'bat']
];

$iterator = new ArrayIterator($data);
$object = new ArrayObject();

$hydratingIterator = new HydratingIteratorIterator(new ArraySerializable(), $iterator, $object);

$hydratingIterator->rewind();
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
$this->assertNotSame(
$object,
$hydratingIterator->current(),
'Hydrating Iterator did not clone the object'
);

$hydratingIterator->next();
$this->assertEquals(new ArrayObject($data[1]), $hydratingIterator->current());
}

public function testUsingStringForObjectName()
{
$data = [
['foo' => 'bar']
];

$iterator = new ArrayIterator($data);

$hydratingIterator = new HydratingIteratorIterator(new ArraySerializable(), $iterator, '\ArrayObject');

$hydratingIterator->rewind();
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
}

public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass()
{
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$hydratingIterator = new HydratingIteratorIterator(new ArraySerializable(), new ArrayIterator(), 'not a real class');
}
}