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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  15.  */
  16. abstract class AbstractChoiceLoader implements ChoiceLoaderInterface
  17. {
  18.     /**
  19.      * The loaded choices.
  20.      *
  21.      * @var iterable|null
  22.      */
  23.     private $choices;
  24.     /**
  25.      * @final
  26.      *
  27.      * {@inheritdoc}
  28.      */
  29.     public function loadChoiceList(?callable $value null): ChoiceListInterface
  30.     {
  31.         return new ArrayChoiceList($this->choices ?? $this->choices $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.         return $this->doLoadChoicesForValues($values$value);
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function loadValuesForChoices(array $choices, ?callable $value null)
  47.     {
  48.         if (!$choices) {
  49.             return [];
  50.         }
  51.         if ($value) {
  52.             // if a value callback exists, use it
  53.             return array_map(function ($item) use ($value) { return (string) $value($item); }, $choices);
  54.         }
  55.         return $this->doLoadValuesForChoices($choices);
  56.     }
  57.     abstract protected function loadChoices(): iterable;
  58.     protected function doLoadChoicesForValues(array $values, ?callable $value): array
  59.     {
  60.         return $this->loadChoiceList($value)->getChoicesForValues($values);
  61.     }
  62.     protected function doLoadValuesForChoices(array $choices): array
  63.     {
  64.         return $this->loadChoiceList()->getValuesForChoices($choices);
  65.     }
  66. }