/
githubmirror
/
symfony
Обзор
Документация
Войти
/
githubmirror
/
symfony
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
8.2
src/Symfony/Component/ObjectMapper/Transform/MapCollection.php
55 строк
2 KB
soyuka
[ObjectMapper] Add `targetClass` on `MapCollection` transform
23 янв 2026, 00:05
23 янв 2026, 00:05
cdaabf6
Код
Авторство
О чём код?
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\ObjectMapper\Transform; use Symfony\Component\ObjectMapper\Exception\MappingException; use Symfony\Component\ObjectMapper\ObjectMapper; use Symfony\Component\ObjectMapper\ObjectMapperAwareInterface; use Symfony\Component\ObjectMapper\ObjectMapperInterface; use Symfony\Component\ObjectMapper\TransformCallableInterface; /** * @template T of object * * @implements TransformCallableInterface<object, T> */ class MapCollection implements TransformCallableInterface, ObjectMapperAwareInterface { public function __construct( private ?ObjectMapperInterface $objectMapper = null, public readonly ?string $targetClass = null, ) { } public function withObjectMapper(ObjectMapperInterface $objectMapper): static { $clone = clone $this; $clone->objectMapper = $objectMapper; return $clone; } public function __invoke(mixed $value, object $source, ?object $target): mixed { if (!is_iterable($value)) { throw new MappingException(\sprintf('The MapCollection transform expects an iterable, "%s" given.', get_debug_type($value))); } $objectMapper = $this->objectMapper ??= new ObjectMapper(); $values = []; foreach ($value as $k => $v) { $values[$k] = $objectMapper->map($v, $this->targetClass); } return $values; } }