<?php
namespace Cms\TenantBundle\Form\Type;
use Cms\CoreBundle\Form\Type\ChosenChoiceType;
use Cms\TenantBundle\Entity\TenantTypeEmbeddable;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class ProductsType
* @package Cms\TenantBundle\Form\Type
*/
class TenantTypeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new CallbackTransformer(
function (TenantTypeEmbeddable $type) {
$value = trim(sprintf(
'%s.%s',
$type->getPrimary(),
$type->getSecondary()
));
if (empty($value)) {
return null;
}
return $value;
},
function (?string $type = null) use ($builder) {
if (empty($type)) {
return new TenantTypeEmbeddable();
}
$types = explode('.', $type);
if (count($types) === 1) {
$types[1] = null;
}
return (new TenantTypeEmbeddable())
->setPrimary($types[0])
->setSecondary($types[1]);
}
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'empty_data' => null,
'required' => false,
'choices' => (function () {
$choices = [
'...' => null,
];
foreach (TenantTypeEmbeddable::SECONDARY as $primary => $secondaries) {
foreach ($secondaries as $secondary) {
$key = sprintf(
'campussuite.cms.tenant.types.combined.%s.%s',
$primary,
$secondary
);
$choices[$key] = sprintf(
'%s.%s',
$primary,
$secondary
);
}
}
return $choices;
})(),
]);
}
/**
* {@inheritdoc}
*/
public function getParent(): ?string
{
return ChosenChoiceType::class;
}
}