<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Bundle\Checkout\EventSubscriber;
use Ibexa\Bundle\Checkout\Validator\Constraints\Phone;
use Ibexa\Contracts\FieldTypeAddress\Event\MapFieldEvent;
use JMS\TranslationBundle\Annotation\Ignore;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Contracts\Translation\TranslatorInterface;
final class AddressFieldsSubscriber implements EventSubscriberInterface, TranslationContainerInterface
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
'ibexa.address.field.tax_id' => 'setLabel',
'ibexa.address.field.email' => 'onAddressEmail',
'ibexa.address.field.phone_number' => 'onAddressPhone',
'ibexa.address.field.first_name' => 'setLabel',
'ibexa.address.field.last_name' => 'setLabel',
];
}
public function setLabel(MapFieldEvent $event): void
{
$event->setLabel(
$this->translator->trans(
/** @Ignore */
'ibexa_checkout.ui.billing_address.' . $event->getIdentifier(),
[],
'ibexa_checkout'
)
);
}
public function onAddressEmail(MapFieldEvent $event): void
{
$event->setOptions([
'constraints' => [new Email()],
]);
}
public function onAddressPhone(MapFieldEvent $event): void
{
$event->setOptions([
'constraints' => [new Phone()],
]);
}
public static function getTranslationMessages(): array
{
return [
(new Message('ibexa_checkout.ui.billing_address.tax_id', 'ibexa_checkout'))->setDesc('Tax ID'),
(new Message('ibexa_checkout.ui.billing_address.first_name', 'ibexa_checkout'))->setDesc('First name'),
(new Message('ibexa_checkout.ui.billing_address.last_name', 'ibexa_checkout'))->setDesc('Last name'),
];
}
}