loadValuesForChoices example


        return $this->getOption()->loadChoiceList($value);
    }

    public function loadChoicesForValues(array $values, callable $value = null): array
    {
        return $this->getOption()->loadChoicesForValues($values$value);
    }

    public function loadValuesForChoices(array $choices, callable $value = null): array
    {
        return $this->getOption()->loadValuesForChoices($choices$value);
    }
}


    public function loadValuesForChoices(array $choices, callable $value = null): array
    {
        $choices = array_filter($choices);

        // If no callable is set, choices are the same as values         if (null === $value) {
            return $choices;
        }

        return parent::loadValuesForChoices($choices$value);
    }
}


    public function testLoadValuesForChoicesCastsCallbackItemsToString()
    {
        $choices = [
           (object) ['id' => 2],
           (object) ['id' => 3],
        ];

        $value = fn ($item) => $item->id;

        $this->assertSame(['2', '3'], self::$loader->loadValuesForChoices($choices$value));
    }

    public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
    {
        $this->assertSame(
            self::$loader->loadValuesForChoices(self::$choices, self::$value),
            self::$lazyChoiceList->getValuesForChoices(self::$choices),
            'Choice list should not be reloaded.'
        );
    }
}
 $loader->loadChoiceList());
    }

    public function testLoadValuesForChoices()
    {
        $evenValues = [1 => '2', 3 => '4'];

        $filter = fn ($choice) => 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader([range(1, 4)])$filter);

        $this->assertSame($evenValues$loader->loadValuesForChoices(range(1, 4)));
    }

    public function testLoadChoicesForValues()
    {
        $evenChoices = [1 => 2, 3 => 4];
        $values = array_map('strval', range(1, 4));

        $filter = fn ($choice) => 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4))$filter);

        
$this->om,
            $this->class,
            null
        );

        $choices = [$this->obj1, $this->obj2, $this->obj3];

        $this->repository->expects($this->once())
            ->method('findAll')
            ->willReturn($choices);

        $this->assertSame(['1', '2']$loader->loadValuesForChoices([$this->obj2, $this->obj3]));

        // no further loads on subsequent calls
        $this->assertSame(['1', '2']$loader->loadValuesForChoices([$this->obj2, $this->obj3]));
    }

    public function testLoadValuesForChoicesDoesNotLoadIfEmptyChoices()
    {
        $loader = new DoctrineChoiceLoader(
            $this->om,
            $this->class,
            

        return $this->loader->loadChoiceList($this->value)->getOriginalKeys();
    }

    public function getChoicesForValues(array $values): array
    {
        return $this->loader->loadChoicesForValues($values$this->value);
    }

    public function getValuesForChoices(array $choices): array
    {
        return $this->loader->loadValuesForChoices($choices$this->value);
    }
}
return $choices ?? [];
    }

    public function loadChoicesForValues(array $values, callable $value = null): array
    {
        return array_filter($this->decoratedLoader->loadChoicesForValues($values$value)$this->filter);
    }

    public function loadValuesForChoices(array $choices, callable $value = null): array
    {
        return $this->decoratedLoader->loadValuesForChoices(array_filter($choices$this->filter)$value);
    }
}
$type = new FormType();
        $decorated = new CallbackChoiceLoader(static fn () => $choices);
        $loader1 = new ChoiceLoader($type$decorated);
        $loader2 = new ChoiceLoader($typenew ArrayChoiceLoader());

        $this->assertEquals($choiceList$loader1->loadChoiceList());
        $this->assertEquals($choiceList$loader2->loadChoiceList());

        $this->assertSame($choices$loader1->loadChoicesForValues($choices));
        $this->assertSame($choices$loader2->loadChoicesForValues($choices));

        $this->assertSame($choices$loader1->loadValuesForChoices($choices));
        $this->assertSame($choices$loader2->loadValuesForChoices($choices));
    }
}

    }

    public function testLoadChoicesForValuesDropsNonExistentChoices()
    {
        $this->assertSame([], self::$loader->loadChoicesForValues(['foo']));
    }

    public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
    {
        $this->assertSame(
            self::$loader->loadValuesForChoices(self::$choices, self::$value),
            self::$lazyChoiceList->getValuesForChoices(self::$choices),
            'Choice list should not be reloaded.'
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.