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

Commit f52915b

Browse files
committed
Merge branch 'develop'
Merging develop to master in prep for 2.6.0
2 parents fc540f3 + cd23c45 commit f52915b

File tree

6 files changed

+294
-0
lines changed

6 files changed

+294
-0
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 2.6.0 - TBD
6+
7+
### Added
8+
9+
- [#13](https://github.com/zendframework/zend-stdlib/pull/13) adds
10+
`Zend\Stdlib\Hydrator\Iterator`, which provides mechanisms for hydrating
11+
objects when iterating a traversable. This allows creating generic collection
12+
resultsets; the original idea was pulled from
13+
[PhlyMongo](https://github.com/phly/PhlyMongo), where it was used to hydrate
14+
collections retrieved from MongoDB.
15+
16+
### Deprecated
17+
18+
- Nothing.
19+
20+
### Removed
21+
22+
- Nothing.
23+
24+
### Fixed
25+
26+
- Nothing.
27+
528
## 2.5.3 - TBD
629

730
### Added
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Stdlib\Hydrator\Iterator;
11+
12+
use ArrayIterator;
13+
use Zend\Stdlib\Hydrator\HydratorInterface;
14+
15+
class HydratingArrayIterator extends HydratingIteratorIterator
16+
{
17+
/**
18+
* @var HydratorInterface
19+
*/
20+
protected $hydrator;
21+
22+
/**
23+
* @var object
24+
*/
25+
protected $prototype;
26+
27+
/**
28+
* @param HydratorInterface $hydrator
29+
* @param array $data
30+
* @param string|object $prototype Object, or class name to use for prototype.
31+
*/
32+
public function __construct(HydratorInterface $hydrator, array $data, $prototype)
33+
{
34+
parent::__construct($hydrator, new ArrayIterator($data), $prototype);
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Stdlib\Hydrator\Iterator;
11+
12+
use Iterator;
13+
use Zend\Stdlib\Hydrator\HydratorInterface;
14+
15+
interface HydratingIteratorInterface extends Iterator
16+
{
17+
/**
18+
* This sets the prototype to hydrate.
19+
*
20+
* This prototype can be the name of the class or the object itself;
21+
* iteration will clone the object.
22+
*
23+
* @param string|object $prototype
24+
*/
25+
public function setPrototype($prototype);
26+
27+
/**
28+
* Sets the hydrator to use during iteration.
29+
*
30+
* @param HydratorInterface $hydrator
31+
*/
32+
public function setHydrator(HydratorInterface $hydrator);
33+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Stdlib\Hydrator\Iterator;
11+
12+
use Iterator;
13+
use IteratorIterator;
14+
use Zend\Stdlib\Exception\InvalidArgumentException;
15+
use Zend\Stdlib\Hydrator\HydratorInterface;
16+
17+
class HydratingIteratorIterator extends IteratorIterator implements HydratingIteratorInterface
18+
{
19+
/**
20+
* @var HydratorInterface
21+
*/
22+
protected $hydrator;
23+
24+
/**
25+
* @var object
26+
*/
27+
protected $prototype;
28+
29+
/**
30+
* @param HydratorInterface $hydrator
31+
* @param Iterator $data
32+
* @param string|object $prototype Object or class name to use for prototype.
33+
*/
34+
public function __construct(HydratorInterface $hydrator, Iterator $data, $prototype)
35+
{
36+
$this->setHydrator($hydrator);
37+
$this->setPrototype($prototype);
38+
parent::__construct($data);
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function setPrototype($prototype)
45+
{
46+
if (is_object($prototype)) {
47+
$this->prototype = $prototype;
48+
return;
49+
}
50+
51+
if (!class_exists($prototype)) {
52+
throw new InvalidArgumentException(
53+
sprintf('Method %s was passed an invalid class name: %s', __METHOD__, $prototype)
54+
);
55+
}
56+
57+
$this->prototype = new $prototype;
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
public function setHydrator(HydratorInterface $hydrator)
64+
{
65+
$this->hydrator = $hydrator;
66+
}
67+
68+
/**
69+
* @return object Returns hydrated clone of $prototype
70+
*/
71+
public function current()
72+
{
73+
$currentValue = parent::current();
74+
$object = clone $this->prototype;
75+
$this->hydrator->hydrate($currentValue, $object);
76+
return $object;
77+
}
78+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Hydrator\Iterator;
11+
12+
use ArrayObject;
13+
use Zend\Stdlib\Hydrator\ArraySerializable;
14+
use Zend\Stdlib\Hydrator\Iterator\HydratingArrayIterator;
15+
16+
class HydratingArrayIteratorTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testHydratesObjectAndClonesOnCurrent()
19+
{
20+
$data = [
21+
['foo' => 'bar'],
22+
['baz' => 'bat'],
23+
];
24+
25+
$object = new ArrayObject();
26+
27+
$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), $data, $object);
28+
29+
$hydratingIterator->rewind();
30+
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
31+
$this->assertNotSame(
32+
$object,
33+
$hydratingIterator->current(),
34+
'Hydrating Iterator did not clone the object'
35+
);
36+
37+
$hydratingIterator->next();
38+
$this->assertEquals(new ArrayObject($data[1]), $hydratingIterator->current());
39+
}
40+
41+
public function testUsingStringForObjectName()
42+
{
43+
$data = [
44+
['foo' => 'bar'],
45+
];
46+
47+
$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), $data, '\ArrayObject');
48+
49+
$hydratingIterator->rewind();
50+
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
51+
}
52+
53+
public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass()
54+
{
55+
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
56+
$hydratingIterator = new HydratingArrayIterator(new ArraySerializable(), [], 'not a real class');
57+
}
58+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Stdlib\Hydrator\Iterator;
11+
12+
use ArrayIterator;
13+
use ArrayObject;
14+
use Zend\Stdlib\Hydrator\ArraySerializable;
15+
use Zend\Stdlib\Hydrator\Iterator\HydratingIteratorIterator;
16+
17+
class HydratingIteratorIteratorTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testHydratesObjectAndClonesOnCurrent()
20+
{
21+
$data = [
22+
['foo' => 'bar'],
23+
['baz' => 'bat'],
24+
];
25+
26+
$iterator = new ArrayIterator($data);
27+
$object = new ArrayObject();
28+
29+
$hydratingIterator = new HydratingIteratorIterator(new ArraySerializable(), $iterator, $object);
30+
31+
$hydratingIterator->rewind();
32+
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
33+
$this->assertNotSame(
34+
$object,
35+
$hydratingIterator->current(),
36+
'Hydrating Iterator did not clone the object'
37+
);
38+
39+
$hydratingIterator->next();
40+
$this->assertEquals(new ArrayObject($data[1]), $hydratingIterator->current());
41+
}
42+
43+
public function testUsingStringForObjectName()
44+
{
45+
$data = [
46+
['foo' => 'bar'],
47+
];
48+
49+
$iterator = new ArrayIterator($data);
50+
51+
$hydratingIterator = new HydratingIteratorIterator(new ArraySerializable(), $iterator, '\ArrayObject');
52+
53+
$hydratingIterator->rewind();
54+
$this->assertEquals(new ArrayObject($data[0]), $hydratingIterator->current());
55+
}
56+
57+
public function testThrowingInvalidArguementExceptionWhenSettingPrototypeToInvalidClass()
58+
{
59+
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
60+
$hydratingIterator = new HydratingIteratorIterator(
61+
new ArraySerializable(),
62+
new ArrayIterator(),
63+
'not a real class'
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)