<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-29
* Time: 19:23
*/
namespace App\Form\Type;
use App\Entity\ApartmentsPricing;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception;
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 ApartmentsPricingType extends AbstractType implements DataMapperInterface
{
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('one_hour_price', NumberType::class, [
'required' => false,
'constraints' => new GreaterThan(0),
]);
$builder->add('two_hours_price', NumberType::class, [
'required' => false,
'constraints' => new GreaterThan(0),
]);
$builder->add('night_price', NumberType::class, [
'required' => false,
'constraints' => new GreaterThan(0),
]);
$builder->setDataMapper($this);
}
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => ApartmentsPricing::class,
'empty_data' => null,
]);
}
/**
* @inheritDoc
*/
public function mapDataToForms($viewData, $forms)
{
if (null === $viewData) {
return null;
}
if (!$viewData instanceof ApartmentsPricing) {
throw new Exception\UnexpectedTypeException($viewData, ApartmentsPricing::class);
}
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$forms['one_hour_price']->setData($viewData->getOneHourPrice());
$forms['two_hours_price']->setData($viewData->getTwoHoursPrice());
$forms['night_price']->setData($viewData->getNightPrice());
}
/**
* @inheritDoc
*/
public function mapFormsToData($forms, &$viewData): void
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);
$viewData = new ApartmentsPricing(
$forms['one_hour_price']->getData(),
$forms['two_hours_price']->getData(),
$forms['night_price']->getData()
);
}
}