src/Cms/TenantBundle/Form/Type/AuthenticationTypesType.php line 37

Open in your IDE?
  1. <?php
  2. namespace Cms\TenantBundle\Form\Type;
  3. use Cms\CoreBundle\Form\Type\ChosenChoiceType;
  4. use Platform\SecurityBundle\Model\OAuth\AuthenticationTypesBitwise;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\CallbackTransformer;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. /**
  10.  * Class AuthenticationTypesType
  11.  * @package Cms\TenantBundle\Form\Type
  12.  */
  13. class AuthenticationTypesType extends AbstractType
  14. {
  15.     /**
  16.      * {@inheritdoc}
  17.      */
  18.     public function buildForm(FormBuilderInterface $builder, array $options)
  19.     {
  20.         $builder->add('mask'ChosenChoiceType::class, array(
  21.             'label' => 'Login Types',
  22.             'helpText' => 'Select which login types the client should have enabled.',
  23.             'expanded' => false,
  24.             'multiple' => true,
  25.             'required' => false,
  26.             'choices' => (new AuthenticationTypesBitwise())->getAllFlagsRefactor(),
  27.             'group_by' => function ($value$key) {
  28.                 if (preg_match('/^(.+?)__/'$key$matches) !== 1) {
  29.                     throw new \Exception();
  30.                 }
  31.                 return $matches[1];
  32.             },
  33.             'choice_translation_domain' => 'CmsCoreBundle',
  34.             'choice_label' => function ($value$key) {
  35.                 return sprintf(
  36.                     'app.platform.security.authentication.labels.%s',
  37.                     $key
  38.                 );
  39.             },
  40.             'choice_value' => function ($value) {
  41.                 return $value;
  42.             },
  43.         ));
  44.         $builder->get('mask')
  45.             ->addModelTransformer(new CallbackTransformer(
  46.                 function ($mask) {
  47.                     return (new AuthenticationTypesBitwise($mask))->getFlags();
  48.                 },
  49.                 function (array $masks) {
  50.                     return array_reduce(
  51.                         $masks,
  52.                         function ($carry$mask) {
  53.                             return ($carry $mask);
  54.                         },
  55.                         0
  56.                     );
  57.                 }
  58.             ))
  59.         ;
  60.         // similar to how datetimes would work in doctrine, need to provide a new object here
  61.         // even if the data of the existing object changes, doctrine cannot track it
  62.         // as such, we need to create a new products object so the setter is called
  63.         $builder->addModelTransformer(new CallbackTransformer(
  64.             function (AuthenticationTypesBitwise $products) {
  65.                 return clone $products;
  66.             },
  67.             function (AuthenticationTypesBitwise $products) {
  68.                 return $products;
  69.             }
  70.         ));
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function configureOptions(OptionsResolver $resolver)
  76.     {
  77.         $resolver->setDefaults(array(
  78.             'data_class' => AuthenticationTypesBitwise::class,
  79.         ));
  80.     }
  81. }