getValuesForChoices example


    public function transform(mixed $array): array
    {
        if (null === $array) {
            return [];
        }

        if (!\is_array($array)) {
            throw new TransformationFailedException('Expected an array.');
        }

        return $this->choiceList->getValuesForChoices($array);
    }

    /** * @throws TransformationFailedException if the given value is not an array * or if no matching choice could be * found for some given value */
    public function reverseTransform(mixed $array): array
    {
        if (null === $array) {
            return [];
        }
public function testCreateChoiceListWithValueCallback()
    {
        $callback = fn ($choice) => ':'.$choice;

        $choiceList = new ArrayChoiceList([2 => 'foo', 7 => 'bar', 10 => 'baz']$callback);

        $this->assertSame([':foo', ':bar', ':baz']$choiceList->getValues());
        $this->assertSame([':foo' => 'foo', ':bar' => 'bar', ':baz' => 'baz']$choiceList->getChoices());
        $this->assertSame([':foo' => 2, ':bar' => 7, ':baz' => 10]$choiceList->getOriginalKeys());
        $this->assertSame([1 => 'foo', 2 => 'baz']$choiceList->getChoicesForValues([1 => ':foo', 2 => ':baz']));
        $this->assertSame([1 => ':foo', 2 => ':baz']$choiceList->getValuesForChoices([1 => 'foo', 2 => 'baz']));
    }

    public function testCreateChoiceListWithoutValueCallbackAndDuplicateFreeToStringChoices()
    {
        $choiceList = new ArrayChoiceList([2 => 'foo', 7 => 'bar', 10 => 123]);

        $this->assertSame(['foo', 'bar', '123']$choiceList->getValues());
        $this->assertSame(['foo' => 'foo', 'bar' => 'bar', '123' => 123]$choiceList->getChoices());
        $this->assertSame(['foo' => 2, 'bar' => 7, '123' => 10]$choiceList->getOriginalKeys());
        $this->assertSame([1 => 'foo', 2 => 123]$choiceList->getChoicesForValues([1 => 'foo', 2 => '123']));
        $this->assertSame([1 => 'foo', 2 => '123']$choiceList->getValuesForChoices([1 => 'foo', 2 => 123]));
    }


        $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.'
        );
    }
}

        $choices = [
            'a' => 'foo',
            'b' => 'bar',
            'c' => 'baz',
        ];
        $list = new LazyChoiceList(new ArrayChoiceLoader($choices)fn ($choice) => array_search($choice$choices));

        // load choice list         $list->getChoices();

        $this->assertSame(['a', 'b']$list->getValuesForChoices(['foo', 'bar']));
        $this->assertSame(['a', 'b']$list->getValuesForChoices(['foo', 'bar']));
    }
}


    abstract protected function loadChoices(): iterable;

    protected function doLoadChoicesForValues(array $values, ?callable $value): array
    {
        return $this->loadChoiceList($value)->getChoicesForValues($values);
    }

    protected function doLoadValuesForChoices(array $choices): array
    {
        return $this->loadChoiceList()->getValuesForChoices($choices);
    }
}
class ChoiceToValueTransformer implements DataTransformerInterface
{
    private ChoiceListInterface $choiceList;

    public function __construct(ChoiceListInterface $choiceList)
    {
        $this->choiceList = $choiceList;
    }

    public function transform(mixed $choice): mixed
    {
        return (string) current($this->choiceList->getValuesForChoices([$choice]));
    }

    public function reverseTransform(mixed $value): mixed
    {
        if (null !== $value && !\is_string($value)) {
            throw new TransformationFailedException('Expected a string or null.');
        }

        $choices = $this->choiceList->getChoicesForValues([(string) $value]);

        if (1 !== \count($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.'
        );
    }
}


    // https://github.com/symfony/symfony/issues/3446     public function testGetChoicesForValuesEmpty()
    {
        $this->assertSame([]$this->list->getChoicesForValues([]));
    }

    public function testGetValuesForChoices()
    {
        $choices = [$this->choice1, $this->choice2];
        $this->assertSame([$this->value1, $this->value2]$this->list->getValuesForChoices($choices));
    }

    public function testGetValuesForChoicesPreservesKeys()
    {
        $choices = [5 => $this->choice1, 8 => $this->choice2];
        $this->assertSame([5 => $this->value1, 8 => $this->value2]$this->list->getValuesForChoices($choices));
    }

    public function testGetValuesForChoicesPreservesOrder()
    {
        $choices = [$this->choice2, $this->choice1];
        
Home | Imprint | This part of the site doesn't use cookies.