src/V4/Form/AsyncSubresourceChoicesLoader.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\V4\Form;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use App\Form\Type\SubresourceChoicesTrait;
  5. use App\Service\Cache\CacheManager;
  6. use App\V4\Logger\SentryLogger;
  7. use Psr\Cache\CacheException;
  8. use Psr\Cache\InvalidArgumentException;
  9. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  10. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  11. /**
  12.  * @see https://stackoverflow.com/questions/35456199/symfony-2-8-dynamic-choicetype-options
  13.  */
  14. class AsyncSubresourceChoicesLoader implements ChoiceLoaderInterface
  15. {
  16.     use SubresourceChoicesTrait;
  17.     /**
  18.      * @var CollectionDataProviderInterface
  19.      */
  20.     private $collectionDataProvider;
  21.     /**
  22.      * @var CacheManager
  23.      */
  24.     private $cacheManager;
  25.     /**
  26.      * @var SentryLogger
  27.      */
  28.     private $sentryLogger;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $entityFQCN;
  33.     /**
  34.      * @var string
  35.      */
  36.     private $valueProperty;
  37.     /**
  38.      * @var ArrayChoiceList
  39.      */
  40.     private $choiceList;
  41.     /**
  42.      * @var array
  43.      */
  44.     private $additionalFilters;
  45.     public function __construct(
  46.         CollectionDataProviderInterface $collectionDataProvider,
  47.         CacheManager $cacheManager,
  48.         SentryLogger $sentryLogger,
  49.         string $entityFQCN,
  50.         string $valueProperty 'id',
  51.         array $additionalFilters = []
  52.     ) {
  53.         $this->collectionDataProvider $collectionDataProvider;
  54.         $this->cacheManager $cacheManager;
  55.         $this->sentryLogger $sentryLogger;
  56.         $this->entityFQCN $entityFQCN;
  57.         $this->valueProperty $valueProperty;
  58.         $this->additionalFilters $additionalFilters;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function loadChoiceList($value null)
  64.     {
  65.         if (!$this->choiceList) {
  66.             $this->choiceList = new ArrayChoiceList([], $value);
  67.         }
  68.         return $this->choiceList;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function loadChoicesForValues(array $values$value null)
  74.     {
  75.         $matches $this->findMatchesOrEmpty($values);
  76.         $this->choiceList = new ArrayChoiceList($matches$value);
  77.         return $matches;
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function loadValuesForChoices(array $choices$value null)
  83.     {
  84.         $ids = [];
  85.         foreach ($choices as $choice) {
  86.             if (is_callable($value)) {
  87.                 $ids[] = $value($choice);
  88.                 continue;
  89.             }
  90.             $ids[] = $choice;
  91.         }
  92.         $this->choiceList = new ArrayChoiceList($choices$value);
  93.         return $ids;
  94.     }
  95.     /**
  96.      * @throws CacheException
  97.      * @throws InvalidArgumentException
  98.      */
  99.     private function findMatchesOrEmpty(array $ids)
  100.     {
  101.         if (empty($ids)) {
  102.             return [];
  103.         }
  104.         sort($ids);
  105.         return $this->getEntityChoices(
  106.             $this->collectionDataProvider,
  107.             $this->cacheManager,
  108.             $this->sentryLogger,
  109.             $this->entityFQCN,
  110.             array_merge($this->additionalFilters, [$this->valueProperty => $ids])
  111.         );
  112.     }
  113. }