vendor/ibexa/checkout/src/bundle/EventSubscriber/AddressFieldsSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\Checkout\EventSubscriber;
  8. use Ibexa\Bundle\Checkout\Validator\Constraints\Phone;
  9. use Ibexa\Contracts\FieldTypeAddress\Event\MapFieldEvent;
  10. use JMS\TranslationBundle\Annotation\Ignore;
  11. use JMS\TranslationBundle\Model\Message;
  12. use JMS\TranslationBundle\Translation\TranslationContainerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Validator\Constraints\Email;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. final class AddressFieldsSubscriber implements EventSubscriberInterface, TranslationContainerInterface
  17. {
  18. private TranslatorInterface $translator;
  19. public function __construct(TranslatorInterface $translator)
  20. {
  21. $this->translator = $translator;
  22. }
  23. /**
  24. * @return array<string, string>
  25. */
  26. public static function getSubscribedEvents(): array
  27. {
  28. return [
  29. 'ibexa.address.field.tax_id' => 'setLabel',
  30. 'ibexa.address.field.email' => 'onAddressEmail',
  31. 'ibexa.address.field.phone_number' => 'onAddressPhone',
  32. 'ibexa.address.field.first_name' => 'setLabel',
  33. 'ibexa.address.field.last_name' => 'setLabel',
  34. ];
  35. }
  36. public function setLabel(MapFieldEvent $event): void
  37. {
  38. $event->setLabel(
  39. $this->translator->trans(
  40. /** @Ignore */
  41. 'ibexa_checkout.ui.billing_address.' . $event->getIdentifier(),
  42. [],
  43. 'ibexa_checkout'
  44. )
  45. );
  46. }
  47. public function onAddressEmail(MapFieldEvent $event): void
  48. {
  49. $event->setOptions([
  50. 'constraints' => [new Email()],
  51. ]);
  52. }
  53. public function onAddressPhone(MapFieldEvent $event): void
  54. {
  55. $event->setOptions([
  56. 'constraints' => [new Phone()],
  57. ]);
  58. }
  59. public static function getTranslationMessages(): array
  60. {
  61. return [
  62. (new Message('ibexa_checkout.ui.billing_address.tax_id', 'ibexa_checkout'))->setDesc('Tax ID'),
  63. (new Message('ibexa_checkout.ui.billing_address.first_name', 'ibexa_checkout'))->setDesc('First name'),
  64. (new Message('ibexa_checkout.ui.billing_address.last_name', 'ibexa_checkout'))->setDesc('Last name'),
  65. ];
  66. }
  67. }