Skip to content

Commit bee0b2d

Browse files
authored
Merge pull request #11 from EventSaucePHP/feature/hydrate-list-of-objects
Fixed #9: Added ability to map a list of payloads to objects.
2 parents 930bb09 + 602ea4a commit bee0b2d

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.2.0] - 2022-04-01
9+
10+
### Added
11+
12+
- Added `hydrateObjects`; a way to hydrate a list of objects in one go
13+
814
## [0.1.0] - 2022-01-03
915

1016
### Added

src/ListOfObjects.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EventSauce\ObjectHydrator;
6+
7+
use ArrayIterator;
8+
use IteratorAggregate;
9+
use Traversable;
10+
use function iterator_to_array;
11+
12+
/**
13+
* @template T
14+
*/
15+
class ListOfObjects implements IteratorAggregate
16+
{
17+
public function __construct(private iterable $objects)
18+
{
19+
}
20+
21+
22+
23+
/**
24+
* @return Traversable<T>
25+
*/
26+
public function getIterator(): Traversable
27+
{
28+
return $this->objects instanceof Traversable
29+
? $this->objects
30+
: new ArrayIterator($this->objects);
31+
}
32+
33+
/**
34+
* @return T[]
35+
*/
36+
public function toArray(): array
37+
{
38+
return $this->objects instanceof Traversable
39+
? iterator_to_array($this->objects, false)
40+
: (array) $this->objects;
41+
}
42+
}

src/ObjectHydrator.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace EventSauce\ObjectHydrator;
66

7+
use Generator;
78
use Throwable;
89
use function array_key_exists;
910
use function count;
@@ -86,4 +87,24 @@ public function hydrateObject(string $className, array $payload): object
8687
throw UnableToHydrateObject::dueToError($className, $exception);
8788
}
8889
}
90+
91+
/**
92+
* @param class-string<T> $className
93+
* @param iterable<array> $payloads;
94+
*
95+
* @return ListOfObjects<T>
96+
*/
97+
public function hydrateObjects(string $className, iterable $payloads): ListOfObjects
98+
{
99+
$generator = $this->doHydrateObjects($className, $payloads);
100+
101+
return new ListOfObjects($generator);
102+
}
103+
104+
private function doHydrateObjects(string $className, iterable $payloads): Generator
105+
{
106+
foreach ($payloads as $payload) {
107+
yield $this->hydrateObject($className, $payload);
108+
}
109+
}
89110
}

src/ObjectHydratorTestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ public function properties_can_be_mapped_from_a_specific_key(): void
3939
self::assertEquals('Frank', $object->name);
4040
}
4141

42+
/**
43+
* @test
44+
*/
45+
public function mapping_to_a_list_of_objects(): void
46+
{
47+
$hydrator = $this->createObjectHydrator();
48+
$input = [['my_name' => 'Frank'], ['my_name' => 'Renske']];
49+
50+
$objects = $hydrator->hydrateObjects(ClassWithMappedStringProperty::class, $input);
51+
52+
self::assertContainsOnlyInstancesOf(ClassWithMappedStringProperty::class, $objects);
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function mapping_to_an_array_of_objects(): void
59+
{
60+
$hydrator = $this->createObjectHydrator();
61+
$input = [['my_name' => 'Frank'], ['my_name' => 'Renske']];
62+
63+
$objects = $hydrator->hydrateObjects(ClassWithMappedStringProperty::class, $input)->toArray();
64+
65+
self::assertIsArray($objects);
66+
self::assertCount(2, $objects);
67+
self::assertContainsOnlyInstancesOf(ClassWithMappedStringProperty::class, $objects);
68+
}
69+
4270
/**
4371
* @test
4472
*/

0 commit comments

Comments
 (0)