<?php
namespace App\Form\Type;
use App\Entity\ApartmentsPricing;
use App\Entity\ExpressPricing;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GreaterThan;
class ExpressPricingType extends AbstractType implements DataMapperInterface
{
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('provided', CheckboxType::class, [
'required' => false,
]);
$builder->add('price', NumberType::class, [
'required' => false,
'constraints' => new GreaterThan(0),
]);
$builder->setDataMapper($this);
}
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => ExpressPricing::class,
'empty_data' => null,
]);
}
/**
* @inheritDoc
*/
public function mapDataToForms($viewData, $forms)
{
if (null === $viewData) {
return null;
}
if (!$viewData instanceof ExpressPricing) {
throw new Exception\UnexpectedTypeException($viewData, ExpressPricing::class);
}
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$forms['provided']->setData($viewData->isProvided());
$forms['price']->setData($viewData->getPrice());
}
/**
* @inheritDoc
*/
public function mapFormsToData($forms, &$viewData): void
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$viewData = new ExpressPricing(
$forms['provided']->getData(),
$forms['price']->getData()
);
}
}