-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from EventSaucePHP/feature/hydrate-list-of-obj…
…ects Fixed #9: Added ability to map a list of payloads to objects.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EventSauce\ObjectHydrator; | ||
|
||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Traversable; | ||
use function iterator_to_array; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
class ListOfObjects implements IteratorAggregate | ||
{ | ||
public function __construct(private iterable $objects) | ||
{ | ||
} | ||
|
||
|
||
|
||
/** | ||
* @return Traversable<T> | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
return $this->objects instanceof Traversable | ||
? $this->objects | ||
: new ArrayIterator($this->objects); | ||
} | ||
|
||
/** | ||
* @return T[] | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->objects instanceof Traversable | ||
? iterator_to_array($this->objects, false) | ||
: (array) $this->objects; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters