MapEntity example

/** * Yields the entity matching the criteria provided in the route. * * @author Fabien Potencier <fabien@symfony.com> * @author Jérémy Derussé <jeremy@derusse.com> */
final class EntityValueResolver implements ValueResolverInterface
{
    public function __construct(
        private ManagerRegistry $registry,
        private ?ExpressionLanguage $expressionLanguage = null,
        private MapEntity $defaults = new MapEntity(),
    ) {
    }

    public function resolve(Request $request, ArgumentMetadata $argument): array
    {
        if (\is_object($request->attributes->get($argument->getName()))) {
            return [];
        }

        $options = $argument->getAttributes(MapEntity::class, ArgumentMetadata::IS_INSTANCEOF);
        $options = ($options[0] ?? $this->defaults)->withDefaults($this->defaults, $argument->getType());

        
$request = new Request();
        $argument = new ArgumentMetadata('arg', null, false, false, null);

        $this->assertSame([]$resolver->resolve($request$argument));
    }

    public function testResolveWithoutAttribute()
    {
        $manager = $this->getMockBuilder(ObjectManager::class)->getMock();
        $registry = $this->createRegistry($manager);
        $resolver = new EntityValueResolver($registry, null, new MapEntity(disabled: true));

        $request = new Request();
        $argument = $this->createArgument();

        $this->assertSame([]$resolver->resolve($request$argument));
    }

    public function testResolveWithoutManager()
    {
        $registry = $this->createRegistry(null);
        $resolver = new EntityValueResolver($registry);

        
Home | Imprint | This part of the site doesn't use cookies.