From 388041a92761d9978b7b0656b505b964908fd5a9 Mon Sep 17 00:00:00 2001 From: "Chuck \"MANCHUCK\" Reeves" Date: Sat, 20 Jun 2015 19:37:38 -0400 Subject: [PATCH 1/3] Added new Hydrating Iterator and Hydrating Array Iterator --- .../Iterator/HydratingArrayIterator.php | 31 +++++++++ .../Iterator/HydratingIteratorInterface.php | 31 +++++++++ .../Iterator/HydratingIteratorIterator.php | 64 +++++++++++++++++++ .../Iterator/HydratingArrayIteratorTest.php | 58 +++++++++++++++++ .../HydratingIteratorIteratorTest.php | 62 ++++++++++++++++++ 5 files changed, 246 insertions(+) create mode 100644 src/Hydrator/Iterator/HydratingArrayIterator.php create mode 100644 src/Hydrator/Iterator/HydratingIteratorInterface.php create mode 100644 src/Hydrator/Iterator/HydratingIteratorIterator.php create mode 100644 test/Hydrator/Iterator/HydratingArrayIteratorTest.php create mode 100644 test/Hydrator/Iterator/HydratingIteratorIteratorTest.php diff --git a/src/Hydrator/Iterator/HydratingArrayIterator.php b/src/Hydrator/Iterator/HydratingArrayIterator.php new file mode 100644 index 000000000..ebab36c99 --- /dev/null +++ b/src/Hydrator/Iterator/HydratingArrayIterator.php @@ -0,0 +1,31 @@ +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; + } +} diff --git a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php new file mode 100644 index 000000000..81b6aaf21 --- /dev/null +++ b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php @@ -0,0 +1,58 @@ + 'bar'), + array('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 = array( + array('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(), array(), 'not a real class'); + } +} diff --git a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php new file mode 100644 index 000000000..cf0ba1ca4 --- /dev/null +++ b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php @@ -0,0 +1,62 @@ + 'bar'), + array('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 = array( + array('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'); + } +} From f90251f01e63f9e4f25494850f7c8d31408836ce Mon Sep 17 00:00:00 2001 From: "Chuck \"MANCHUCK\" Reeves" Date: Sat, 20 Jun 2015 20:03:29 -0400 Subject: [PATCH 2/3] CS Fixes --- .../Iterator/HydratingArrayIteratorTest.php | 16 ++++++++-------- .../Iterator/HydratingIteratorIteratorTest.php | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php index 81b6aaf21..310b5e176 100644 --- a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php @@ -17,10 +17,10 @@ class HydratingArrayIteratorTest extends \PHPUnit_Framework_TestCase { public function testHydratesObjectAndClonesOnCurrent() { - $data = array( - array('foo' => 'bar'), - array('baz' => 'bat') - ); + $data = [ + ['foo' => 'bar'], + ['baz' => 'bat'] + ]; $object = new ArrayObject(); @@ -40,9 +40,9 @@ public function testHydratesObjectAndClonesOnCurrent() public function testUsingStringForObjectName() { - $data = array( - array('foo' => 'bar') - ); + $data = [ + ['foo' => 'bar'] + ]; $hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), $data, '\ArrayObject'); @@ -53,6 +53,6 @@ public function testUsingStringForObjectName() public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass() { $this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException'); - $hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), array(), 'not a real class'); + $hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), [], 'not a real class'); } } diff --git a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php index cf0ba1ca4..fd11979c9 100644 --- a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php @@ -18,10 +18,10 @@ class HydratingIteratorIteratorTest extends \PHPUnit_Framework_TestCase { public function testHydratesObjectAndClonesOnCurrent() { - $data = array( - array('foo' => 'bar'), - array('baz' => 'bat') - ); + $data = [ + ['foo' => 'bar'], + ['baz' => 'bat'] + ]; $iterator = new ArrayIterator($data); $object = new ArrayObject(); @@ -42,9 +42,9 @@ public function testHydratesObjectAndClonesOnCurrent() public function testUsingStringForObjectName() { - $data = array( - array('foo' => 'bar') - ); + $data = [ + ['foo' => 'bar'] + ]; $iterator = new ArrayIterator($data); From 0152f84c7d09e92d4cda27fc78d3fa2689b579b1 Mon Sep 17 00:00:00 2001 From: "Chuck \"MANCHUCK\" Reeves" Date: Mon, 22 Jun 2015 12:35:07 -0400 Subject: [PATCH 3/3] Removing starting back slash from @samsonasik comment --- src/Hydrator/Iterator/HydratingArrayIterator.php | 2 +- src/Hydrator/Iterator/HydratingIteratorInterface.php | 2 +- src/Hydrator/Iterator/HydratingIteratorIterator.php | 4 ++-- test/Hydrator/Iterator/HydratingArrayIteratorTest.php | 2 +- test/Hydrator/Iterator/HydratingIteratorIteratorTest.php | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Hydrator/Iterator/HydratingArrayIterator.php b/src/Hydrator/Iterator/HydratingArrayIterator.php index ebab36c99..fb9a2b0e7 100644 --- a/src/Hydrator/Iterator/HydratingArrayIterator.php +++ b/src/Hydrator/Iterator/HydratingArrayIterator.php @@ -9,7 +9,7 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use \ArrayIterator; +use ArrayIterator; use Zend\Stdlib\Hydrator\HydratorInterface; class HydratingArrayIterator extends HydratingIteratorIterator diff --git a/src/Hydrator/Iterator/HydratingIteratorInterface.php b/src/Hydrator/Iterator/HydratingIteratorInterface.php index 15ecec699..65935ec87 100644 --- a/src/Hydrator/Iterator/HydratingIteratorInterface.php +++ b/src/Hydrator/Iterator/HydratingIteratorInterface.php @@ -9,7 +9,7 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use \Iterator; +use Iterator; use Zend\Stdlib\Hydrator\HydratorInterface; interface HydratingIteratorInterface extends Iterator diff --git a/src/Hydrator/Iterator/HydratingIteratorIterator.php b/src/Hydrator/Iterator/HydratingIteratorIterator.php index ed410c0a7..5a56e2446 100644 --- a/src/Hydrator/Iterator/HydratingIteratorIterator.php +++ b/src/Hydrator/Iterator/HydratingIteratorIterator.php @@ -9,8 +9,8 @@ namespace Zend\Stdlib\Hydrator\Iterator; -use \Iterator; -use \IteratorIterator; +use Iterator; +use IteratorIterator; use Zend\Stdlib\Exception\InvalidArgumentException; use Zend\Stdlib\Hydrator\HydratorInterface; diff --git a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php index 310b5e176..40ed4290b 100644 --- a/test/Hydrator/Iterator/HydratingArrayIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingArrayIteratorTest.php @@ -9,7 +9,7 @@ namespace Hydrator\Iterator; -use \ArrayObject; +use ArrayObject; use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\Iterator\HydratingArrayIterator; diff --git a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php index fd11979c9..7d869a72d 100644 --- a/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php +++ b/test/Hydrator/Iterator/HydratingIteratorIteratorTest.php @@ -9,8 +9,8 @@ namespace ZendTest\Stdlib\Hydrator\Iterator; -use \ArrayIterator; -use \ArrayObject; +use ArrayIterator; +use ArrayObject; use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Stdlib\Hydrator\Iterator\HydratingIteratorIterator;