<?php
namespace Cms\TenantBundle\Form\Type;
use Cms\CoreBundle\Form\Type\ChosenChoiceType;
use Platform\SecurityBundle\Model\OAuth\AuthenticationTypesBitwise;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AuthenticationTypesType
* @package Cms\TenantBundle\Form\Type
*/
class AuthenticationTypesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('mask', ChosenChoiceType::class, array(
'label' => 'Login Types',
'helpText' => 'Select which login types the client should have enabled.',
'expanded' => false,
'multiple' => true,
'required' => false,
'choices' => (new AuthenticationTypesBitwise())->getAllFlagsRefactor(),
'group_by' => function ($value, $key) {
if (preg_match('/^(.+?)__/', $key, $matches) !== 1) {
throw new \Exception();
}
return $matches[1];
},
'choice_translation_domain' => 'CmsCoreBundle',
'choice_label' => function ($value, $key) {
return sprintf(
'app.platform.security.authentication.labels.%s',
$key
);
},
'choice_value' => function ($value) {
return $value;
},
));
$builder->get('mask')
->addModelTransformer(new CallbackTransformer(
function ($mask) {
return (new AuthenticationTypesBitwise($mask))->getFlags();
},
function (array $masks) {
return array_reduce(
$masks,
function ($carry, $mask) {
return ($carry | $mask);
},
0
);
}
))
;
// similar to how datetimes would work in doctrine, need to provide a new object here
// even if the data of the existing object changes, doctrine cannot track it
// as such, we need to create a new products object so the setter is called
$builder->addModelTransformer(new CallbackTransformer(
function (AuthenticationTypesBitwise $products) {
return clone $products;
},
function (AuthenticationTypesBitwise $products) {
return $products;
}
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => AuthenticationTypesBitwise::class,
));
}
}