custom/plugins/ProxaInternationalPhoneNumber/src/Subscriber/Storefront/CheckoutSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProxaInternationalPhoneNumber\Subscriber\Storefront;
  3. use ProxaInternationalPhoneNumber\Components\Validator\PhoneCode;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\Event\DataMappingEvent;
  6. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Validator\Constraints\Length;
  9. class CheckoutSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @return array
  13.      */
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             'framework.validation.address.create' => 'addValidation',
  18.             'framework.validation.address.update' => 'addValidation',
  19.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onMappingAddress',
  20.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onMappingAddress',
  21.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onMappingAddress'
  22.         ];
  23.     }
  24.     /**
  25.      * @param BuildValidationEvent $event
  26.      * @return void
  27.      */
  28.     public function addValidation(BuildValidationEvent $event): void
  29.     {
  30.         $data $event->getData();
  31.         $this->addBillingAddressValidation($data$event);
  32.     }
  33.     /**
  34.      * @param $data
  35.      * @param $event
  36.      * @return void
  37.      */
  38.     private function addBillingAddressValidation($data$event)
  39.     {
  40.         $address = !empty($data->get('billingAddress')) ? $data->get('billingAddress') : $event->getData();
  41.         if($address){
  42.             $phone $address->get('phoneNumber');
  43.             $code = (int)$address->get('phoneNumberCode');
  44.             if ($phone && $code) {
  45.                 $lengths $this->getLengths($code);
  46.                 $this->addDefinition($event$lengths);
  47.             }
  48.         }
  49.     }
  50.     /**
  51.      * @param int $code
  52.      * @return array|null
  53.      */
  54.     public function getLengths(int $code): ?array
  55.     {
  56.         $availableLength $this->getPhoneNumberLength($code);
  57.         if ($availableLength) {
  58.             $minLength current($availableLength);
  59.             $maxLength end($availableLength);
  60.             return ['minLength' => $minLength'maxLength' => $maxLength];
  61.         }
  62.         return null;
  63.     }
  64.     /**
  65.      * @param int $code
  66.      * @return mixed
  67.      */
  68.     public function getPhoneNumberLength(int $code)
  69.     {
  70.         $numberLengths $this->getInternationalNumbersLengths();
  71.         return $numberLengths[$code] ?? null;
  72.     }
  73.     /**
  74.      * @param DataMappingEvent $event
  75.      * @return void
  76.      */
  77.     public function onMappingAddress(DataMappingEvent $event): void
  78.     {
  79.         $address $event->getOutput();
  80.         if ($event->getInput()->get('phoneNumberCode')) {
  81.             $address['customFields']['phoneNumberCode'] = $event->getInput()->get('phoneNumberCode');
  82.         }
  83.         $event->setOutput($address);
  84.     }
  85.     /**
  86.      * @param BuildValidationEvent $event
  87.      * @param $lengths
  88.      * @return void
  89.      */
  90.     private function addDefinition(BuildValidationEvent $event$lengths)
  91.     {
  92.         $event->getDefinition()->add('phoneNumberCode', new PhoneCode());
  93.         if ($lengths) {
  94.             $event->getDefinition()->add('phoneNumber', new Length(['min' => $lengths['minLength']]), new Length(['max' => $lengths['maxLength']]));
  95.         }
  96.     }
  97.     /**
  98.      * @return \int[][]
  99.      */
  100.     public function getInternationalNumbersLengths(): array
  101.     {
  102.         $version1 = [6789101112131415];
  103.         $version2 = [9101112131415];
  104.         $version3 = [89101112131415];
  105.         $version4 = [789101112131415];
  106.         $version5 = [101112131415];
  107.         return [
  108.             49 => $version1,/*Deutschland (+49)*/
  109.             33 => $version2,/*Frankreich (+33)*/
  110.             39 => $version2,/*Italien (+39)*/
  111.             34 => $version2,/*Spanien (+34)*/
  112.             351 => $version2,/*Portugal (+351)*/
  113.             45 => $version3,/*Dänemark (+45)*/
  114.             358 => $version3,/*Finnland (+358)*/
  115.             46 => $version4,/*Schweden (+46)*/
  116.             41 => $version2,/*Schweiz (+41)*/
  117.             32 => $version2,/*Belgien (+32)*/
  118.             372 => $version4,/*Estland (+372)*/
  119.             386 => $version3,/*Slowenien (+386)*/
  120.             40 => $version5,/*Rumänien (+40)*/
  121.             420 => $version2,/*Tschechische Republik (+420)*/
  122.             421 => $version2,/*Slowakei (+421)*/
  123.             31 => $version3,/*Niederlande (+31)*/
  124.             30 => $version5,/*Griechenland (+30)*/
  125.             48 => $version1,/*Poland (+48)*/
  126.             370 => $version3,/*Litauen (+370)*/
  127.             43 => $version1,/*Austria (+43)*/
  128.         ];
  129.     }
  130. }