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

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.     /**
  19.      * The loaded choice list.
  20.      *
  21.      * @var ArrayChoiceList
  22.      */
  23.     private $choiceList;
  24.     /**
  25.      * @final
  26.      *
  27.      * {@inheritdoc}
  28.      */
  29.     public function loadChoiceList(callable $value null): ChoiceListInterface
  30.     {
  31.         return $this->choiceList ?? ($this->choiceList = new ArrayChoiceList($this->loadChoices(), $value));
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function loadChoicesForValues(array $values, callable $value null)
  37.     {
  38.         if (!$values) {
  39.             return [];
  40.         }
  41.         if ($this->choiceList) {
  42.             return $this->choiceList->getChoicesForValues($values);
  43.         }
  44.         return $this->doLoadChoicesForValues($values$value);
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function loadValuesForChoices(array $choices, callable $value null)
  50.     {
  51.         if (!$choices) {
  52.             return [];
  53.         }
  54.         if ($value) {
  55.             // if a value callback exists, use it
  56.             return array_map($value$choices);
  57.         }
  58.         if ($this->choiceList) {
  59.             return $this->choiceList->getValuesForChoices($choices);
  60.         }
  61.         return $this->doLoadValuesForChoices($choices);
  62.     }
  63.     abstract protected function loadChoices(): iterable;
  64.     protected function doLoadChoicesForValues(array $values, ?callable $value): array
  65.     {
  66.         return $this->loadChoiceList($value)->getChoicesForValues($values);
  67.     }
  68.     protected function doLoadValuesForChoices(array $choices): array
  69.     {
  70.         return $this->loadChoiceList()->getValuesForChoices($choices);
  71.     }
  72. }