-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
431 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/** | ||
* League.Csv (https://csv.thephpleague.com) | ||
* | ||
* (c) Ignace Nyamagana Butera <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Csv; | ||
|
||
use Iterator; | ||
use League\Csv\Serializer\DenormalizerFactory; | ||
use League\Csv\Serializer\Factory; | ||
use ReflectionException; | ||
|
||
/** | ||
* @template TClass of object | ||
*/ | ||
final class Hydrator implements TabularDataHydrator | ||
{ | ||
public function __construct(private readonly DenormalizerFactory $factory = new Factory()) | ||
{ | ||
} | ||
|
||
/** | ||
* Denormalizes data back into an object of a given class. | ||
* | ||
* @param class-string<TClass> $className | ||
* | ||
* @throws Serializer\DenormalizationFailed | ||
* @throws ReflectionException | ||
* @throws Serializer\TypeCastingFailed | ||
* | ||
* @return TClass | ||
*/ | ||
public function hydrate(string $className, array $record): object | ||
{ | ||
/** @var TClass $instance */ | ||
$instance = $this->factory | ||
->newDenormalizer($className, array_keys($record)) | ||
->denormalize($record); | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Denormalizes data back into an object of a given class. | ||
* | ||
* @param class-string<TClass> $className | ||
* @param iterable<array> $records | ||
* | ||
* @throws Serializer\DenormalizationFailed | ||
* @throws ReflectionException | ||
* @throws Serializer\TypeCastingFailed | ||
* | ||
* @return Iterator<TClass> | ||
*/ | ||
public function hydrateAll(string $className, iterable $records): Iterator | ||
{ | ||
$iterator = MapIterator::toIterator($records); | ||
$iterator->rewind(); | ||
if (!$iterator->valid()) { | ||
return; | ||
} | ||
|
||
/** @var array $current */ | ||
$current = $iterator->current(); | ||
$key = $iterator->key(); | ||
$iterator->next(); | ||
|
||
$denormalizer = $this->factory->newDenormalizer($className, array_keys($current)); | ||
|
||
while ($iterator->valid()) { | ||
/** @var TClass $instance */ | ||
$instance = $denormalizer->denormalize($current); | ||
|
||
yield $key => $instance; | ||
|
||
/** @var array $current */ | ||
$current = $iterator->current(); | ||
$key = $iterator->key(); | ||
$iterator->next(); | ||
} | ||
|
||
/** @var TClass $instance */ | ||
$instance = $denormalizer->denormalize($current); | ||
|
||
yield $key => $instance; | ||
} | ||
} |
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
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
Oops, something went wrong.