vendor/sylius/resource-bundle/src/Bundle/Form/Type/FixedCollectionType.php line 22

  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\Options;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Webmozart\Assert\Assert;
  17. final class FixedCollectionType extends AbstractType
  18. {
  19.     public function buildForm(FormBuilderInterface $builder, array $options): void
  20.     {
  21.         Assert::isIterable($options['entries']);
  22.         foreach ($options['entries'] as $entry) {
  23.             Assert::isCallable($options['entry_type']);
  24.             Assert::isCallable($options['entry_name']);
  25.             Assert::isCallable($options['entry_options']);
  26.             $entryType $options['entry_type']($entry);
  27.             $entryName $options['entry_name']($entry);
  28.             $entryOptions $options['entry_options']($entry);
  29.             $builder->add($entryName$entryTypearray_replace([
  30.                 'property_path' => '[' $entryName ']',
  31.                 'block_name' => 'entry',
  32.             ], $entryOptions));
  33.         }
  34.     }
  35.     public function configureOptions(OptionsResolver $resolver): void
  36.     {
  37.         $resolver->setRequired('entries');
  38.         $resolver->setAllowedTypes('entries', ['array'\Traversable::class]);
  39.         $resolver->setRequired('entry_type');
  40.         $resolver->setAllowedTypes('entry_type', ['string''callable']);
  41.         $resolver->setNormalizer('entry_type'$this->optionalCallableNormalizer());
  42.         $resolver->setRequired('entry_name');
  43.         $resolver->setAllowedTypes('entry_name', ['callable']);
  44.         $resolver->setDefault('entry_options', function () {
  45.             return [];
  46.         });
  47.         $resolver->setAllowedTypes('entry_options', ['array''callable']);
  48.         $resolver->setNormalizer('entry_options'$this->optionalCallableNormalizer());
  49.     }
  50.     public function getBlockPrefix(): string
  51.     {
  52.         return 'sylius_fixed_collection';
  53.     }
  54.     private function optionalCallableNormalizer(): \Closure
  55.     {
  56.         return
  57.             /**
  58.              * @param mixed $value
  59.              *
  60.              * @return mixed
  61.              */
  62.             function (Options $options$value) {
  63.                 if (is_callable($value)) {
  64.                     return $value;
  65.                 }
  66.                 return /** @return mixed */ function () use ($value) {
  67.                     return $value;
  68.                 };
  69.             }
  70.         ;
  71.     }
  72. }