/
githubmirror
/
symfony
Обзор
Документация
Войти
/
githubmirror
/
symfony
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
8.2
src/Symfony/Component/ObjectMapper/Tests/Fixtures/MapStruct/MapStructMapperMetadataFactory.php
53 строки
2 KB
soyuka
[ObjectMapper] Object to Object mapper component
24 мар 2025, 14:33
24 мар 2025, 14:33
3f99b03
Код
Авторство
О чём код?
<?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\Tests\Fixtures\MapStruct; use Symfony\Component\ObjectMapper\Metadata\Mapping; use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface; use Symfony\Component\ObjectMapper\ObjectMapperInterface; /** * A Metadata factory that implements the basics behind https://mapstruct.org/. * * @author Antoine Bluchet <soyuka@gmail.com> */ final class MapStructMapperMetadataFactory implements ObjectMapperMetadataFactoryInterface { public function __construct(private readonly string $mapper) { if (!is_a($mapper, ObjectMapperInterface::class, true)) { throw new \RuntimeException(\sprintf('Mapper should implement "%s".', ObjectMapperInterface::class)); } } public function create(object $object, ?string $property = null, array $context = []): array { $refl = new \ReflectionClass($this->mapper); $mapTo = []; $source = $property ?? $object::class; foreach (($property ? $refl->getMethod('map') : $refl)->getAttributes(Map::class) as $mappingAttribute) { $map = $mappingAttribute->newInstance(); if ($map->source === $source) { $mapTo[] = new Mapping(source: $map->source, target: $map->target, if: $map->if, transform: $map->transform); continue; } } // Default is to map properties to a property of the same name if (!$mapTo && $property) { $mapTo[] = new Mapping(source: $property, target: $property); } return $mapTo; } }