vendor/symfony/form/ChoiceList/Loader/AbstractChoiceLoader.php line 29

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\ChoiceList\Loader;
  11. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  12. use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
  13. /**
  14.  * @author Jules Pietri <jules@heahprod.com>
  15.  */
  16. abstract class AbstractChoiceLoader implements ChoiceLoaderInterface
  17. {
  18.     private ?iterable $choices;
  19.     /**
  20.      * @final
  21.      *
  22.      * {@inheritdoc}
  23.      */
  24.     public function loadChoiceList(callable $value null): ChoiceListInterface
  25.     {
  26.         return new ArrayChoiceList($this->choices ??= $this->loadChoices(), $value);
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function loadChoicesForValues(array $values, callable $value null): array
  32.     {
  33.         if (!$values) {
  34.             return [];
  35.         }
  36.         return $this->doLoadChoicesForValues($values$value);
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function loadValuesForChoices(array $choices, callable $value null): array
  42.     {
  43.         if (!$choices) {
  44.             return [];
  45.         }
  46.         if ($value) {
  47.             // if a value callback exists, use it
  48.             return array_map($value$choices);
  49.         }
  50.         return $this->doLoadValuesForChoices($choices);
  51.     }
  52.     abstract protected function loadChoices(): iterable;
  53.     protected function doLoadChoicesForValues(array $values, ?callable $value): array
  54.     {
  55.         return $this->loadChoiceList($value)->getChoicesForValues($values);
  56.     }
  57.     protected function doLoadValuesForChoices(array $choices): array
  58.     {
  59.         return $this->loadChoiceList()->getValuesForChoices($choices);
  60.     }
  61. }