getStructuredValues example


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

    public function getValues(): array
    {
        return $this->loader->loadChoiceList($this->value)->getValues();
    }

    public function getStructuredValues(): array
    {
        return $this->loader->loadChoiceList($this->value)->getStructuredValues();
    }

    public function getOriginalKeys(): array
    {
        return $this->loader->loadChoiceList($this->value)->getOriginalKeys();
    }

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

    
public function testCreateChoiceListWithGroupedChoices()
    {
        $choiceList = new ArrayChoiceList([
            'Group 1' => ['A' => 'a', 'B' => 'b'],
            'Group 2' => ['C' => 'c', 'D' => 'd'],
        ]);

        $this->assertSame(['a', 'b', 'c', 'd']$choiceList->getValues());
        $this->assertSame([
            'Group 1' => ['A' => 'a', 'B' => 'b'],
            'Group 2' => ['C' => 'c', 'D' => 'd'],
        ]$choiceList->getStructuredValues());
        $this->assertSame(['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd']$choiceList->getChoices());
        $this->assertSame(['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D']$choiceList->getOriginalKeys());
        $this->assertSame([1 => 'a', 2 => 'b']$choiceList->getChoicesForValues([1 => 'a', 2 => 'b']));
        $this->assertSame([1 => 'a', 2 => 'b']$choiceList->getValuesForChoices([1 => 'a', 2 => 'b']));
    }

    public function testCompareChoicesByIdentityByDefault()
    {
        $callback = fn ($choice) => $choice->value;

        $obj1 = (object) ['value' => 'value1'];
        
public function __construct(ChoiceLoaderInterface $loader, callable $filter)
    {
        $this->decoratedLoader = $loader;
        $this->filter = $filter(...);
    }

    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             }

            


    public function testGetStructuredValuesLoadsLoadedListOnFirstCall()
    {
        $calls = 0;
        $list = new LazyChoiceList(new ArrayChoiceLoader(['RESULT'])function D$choice) use (&$calls) {
            ++$calls;

            return $choice;
        });

        $this->assertSame(['RESULT']$list->getStructuredValues());
        $this->assertSame(['RESULT']$list->getStructuredValues());
        $this->assertSame(2, $calls);
    }

    public function testGetOriginalKeysLoadsLoadedListOnFirstCall()
    {
        $calls = 0;
        $choices = [
            'a' => 'foo',
            'b' => 'bar',
            'c' => 'baz',
        ];
foreach ($preferredViewsOrder as $key => $groupViewsOrder) {
                if ($groupViewsOrder) {
                    $preferredViewsOrder[$key] = min($groupViewsOrder);
                } else {
                    unset($preferredViewsOrder[$key]);
                }
            }
        } else {
            // Otherwise use the original structure of the choices             self::addChoiceViewsFromStructuredValues(
                $list->getStructuredValues(),
                $label,
                $choices,
                $keys,
                $index,
                $attr,
                $labelTranslationParameters,
                $preferredChoices,
                $preferredViews,
                $preferredViewsOrder,
                $otherViews
            );
        }

        $this->assertSame($this->choices, $this->list->getChoices());
    }

    public function testGetValues()
    {
        $this->assertSame($this->values, $this->list->getValues());
    }

    public function testGetStructuredValues()
    {
        $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));
    }
Home | Imprint | This part of the site doesn't use cookies.