<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-30
* Time: 12:50
*/
namespace App\Form\Type;
use App\Entity\PhoneCallRestrictions;
use App\Service\Features;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PhoneCallRestrictionsType extends AbstractType implements DataMapperInterface
{
public function __construct(
private Features $features
) {}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$hourChoices = range(0, 24);
$builder->add('time_from', ChoiceType::class, [
'empty_data' => 0,
'choices' => $hourChoices,
'choice_label' => function ($value): string {
return sprintf('%02d', $value);
},
]);
$builder->add('time_to', ChoiceType::class, [
'empty_data' => 24,
'choices' => $hourChoices,
'choice_label' => function ($value): string {
return sprintf('%02d', $value);
}
]);
$answeringToChoices = PhoneCallRestrictions::getList();
if(false == $this->features->extended_profile_form()) {
unset($answeringToChoices['ANSWERING_MESSENGER']);
}
$builder->add('answering_to', ChoiceType::class, [
'multiple' => true,
'expanded' => true,
// 'empty_data' => PhoneCallRestrictions::ANSWERING_CALLS_AND_SMS,
'choices' => $answeringToChoices,
'choice_translation_domain' => 'phone_call_restrictions',
]);
$builder->setDataMapper($this);
}
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => PhoneCallRestrictions::class,
'empty_data' => null,
]);
}
/**
* @inheritDoc
*/
public function mapDataToForms($viewData, $forms)
{
if (null === $viewData) {
return null;
}
if (!$viewData instanceof PhoneCallRestrictions) {
throw new Exception\UnexpectedTypeException($viewData, PhoneCallRestrictions::class);
}
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$forms['time_from']->setData($viewData->getTimeFrom());
$forms['time_to']->setData($viewData->getTimeTo());
$forms['answering_to']->setData($viewData->getAnsweringTo());
}
/**
* @inheritDoc
*/
public function mapFormsToData($forms, &$viewData): void
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$viewData = new PhoneCallRestrictions(
$forms['time_from']->getData(),
$forms['time_to']->getData(),
$forms['answering_to']->getData()
);
}
}