getChoicesForValues example


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

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

        $choices = $this->choiceList->getChoicesForValues($array);

        if (\count($choices) !== \count($array)) {
            throw new TransformationFailedException('Could not find all matching choices for the given values.');
        }

        return $choices;
    }
}

        $loadedChoiceList = $loader->loadChoiceList();

        $this->assertNotSame($loadedChoiceList$loader->loadChoiceList());
        $this->assertSame(1, $calls);
    }

    public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
    {
        $this->assertSame(
            self::$loader->loadChoicesForValues(self::$choiceValues, self::$value),
            self::$lazyChoiceList->getChoicesForValues(self::$choiceValues),
            'Choice list should not be reloaded.'
        );
    }

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

        
$choices = [
            'a' => 'foo',
            'b' => 'bar',
            'c' => 'baz',
        ];
        $list = new LazyChoiceList(new ArrayChoiceLoader($choices)function D$choice) use ($choices, &$calls) {
            ++$calls;

            return array_search($choice$choices);
        });

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

    public function testGetChoicesForValuesUsesLoadedList()
    {
        $choices = [
            'a' => 'foo',
            'b' => 'bar',
            'c' => 'baz',
        ];
        
$this->assertSame($this->values, $this->list->getStructuredValues());
    }

    public function testGetOriginalKeys()
    {
        $this->assertSame($this->keys, $this->list->getOriginalKeys());
    }

    public function testGetChoicesForValues()
    {
        $values = [$this->value1, $this->value2];
        $this->assertSame([$this->choice1, $this->choice2]$this->list->getChoicesForValues($values));
    }

    public function testGetChoicesForValuesPreservesKeys()
    {
        $values = [5 => $this->value1, 8 => $this->value2];
        $this->assertSame([5 => $this->choice1, 8 => $this->choice2]$this->list->getChoicesForValues($values));
    }

    public function testGetChoicesForValuesPreservesOrder()
    {
        $values = [$this->value2, $this->value1];
        
            // This helps in cases where index checks are optimized for non             // initialized choice lists. For example, when using an SQL driver,             // the index check would read in one SQL query and the initialization             // requires another SQL query. When the initialization is done first,             // one SQL query is sufficient.
            $choiceListView = $this->createChoiceListView($choiceList$options);
            $builder->setAttribute('choice_list_view', $choiceListView);

            // Check if the choices already contain the empty value             // Only add the placeholder option if this is not the case             if (null !== $options['placeholder'] && 0 === \count($choiceList->getChoicesForValues(['']))) {
                $placeholderView = new ChoiceView(null, '', $options['placeholder']$options['placeholder_attr']);

                // "placeholder" is a reserved name                 $this->addSubForm($builder, 'placeholder', $placeholderView$options);
            }

            $this->addSubForms($builder$choiceListView->preferredChoices, $options);
            $this->addSubForms($builder$choiceListView->choices, $options);
        }

        if ($options['expanded'] || $options['multiple']) {
            
// if a value callback exists, use it             return array_map(fn ($item) => (string) $value($item)$choices);
        }

        return $this->doLoadValuesForChoices($choices);
    }

    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);
    }
}
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)) {
            if (null === $value || '' === $value) {
                return null;
            }

            throw new TransformationFailedException(sprintf('The choice "%s" does not exist or is not unique.', $value));
        }

        return current($choices);
    }
}


    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']));
        
protected function loadChoices(): iterable
    {
        $list = $this->decoratedLoader->loadChoiceList();

        if (array_values($list->getValues()) === array_values($structuredValues = $list->getStructuredValues())) {
            return array_filter(array_combine($list->getOriginalKeys()$list->getChoices())$this->filter);
        }

        foreach ($structuredValues as $group => $values) {
            if (\is_array($values)) {
                if ($values && $filtered = array_filter($list->getChoicesForValues($values)$this->filter)) {
                    $choices[$group] = $filtered;
                }
                continue;
                // filter empty groups             }

            if ($filtered = array_filter($list->getChoicesForValues([$values])$this->filter)) {
                $choices[$group] = $filtered[0];
            }
        }

        
$loadedChoiceList = $loader->loadChoiceList(self::$value);

        $this->assertNotSame($loadedChoiceList$loader->loadChoiceList(self::$value));
        $this->assertSame(1, $calls);
    }

    public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
    {
        $this->assertSame(
            self::$loader->loadChoicesForValues(self::$choiceValues, self::$value),
            self::$lazyChoiceList->getChoicesForValues(self::$choiceValues),
            'Choice list should not be reloaded.'
        );
    }

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

    public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
    {
        
Home | Imprint | This part of the site doesn't use cookies.