<?php
declare(strict_types=1);
namespace App\Form\EventListener\Global;
use App\Entity\Shop\ShopInterface;
use App\Form\FormHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class AddWeekDaysFieldSubscriber extends FormHelper implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'preSetData'];
}
public function preSetData(FormEvent $event): void
{
$choices = ShopInterface::WEEKDAYS;
if (($key = array_search($event->getData()->getDay(), $choices, true)) !== false) {
unset($choices[$key]);
}
$event->getForm()->add(
'days',
ChoiceType::class,
$this->getChoiceParameters('app.shop_owner.pages.schedule.list.days', self::PLACEHOLDER_SAME_AS_LABEL, true, [
'choices' => $choices,
'multiple' => true,
])
);
}
}