vendor/store.shopware.com/tonurpackstation6/src/Subscriber/AddressValidationSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace Tonur\Packstation\Subscriber;
  3. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Validator\Constraints\Length;
  8. use Tonur\Packstation\Component\Validator\Constraints\PostNumber;
  9. use Tonur\Packstation\Util\PluginConfigUtil;
  10. class AddressValidationSubscriber implements EventSubscriberInterface
  11. {
  12.     private PluginConfigUtil $pluginConfig;
  13.     private RequestStack $requestStack;
  14.     public function __construct(PluginConfigUtil $pluginConfigRequestStack $requestStack)
  15.     {
  16.         $this->pluginConfig $pluginConfig;
  17.         $this->requestStack $requestStack;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             'framework.validation.address.update' => 'onBuildValidation',
  23.             'framework.validation.address.create' => 'onBuildValidation'
  24.         ];
  25.     }
  26.     public function onBuildValidation(BuildValidationEvent $event)
  27.     {
  28.         if (!$this->isPluginActive()) {
  29.             return;
  30.         }
  31.         $this->addPostNumberBuildValidation($event);
  32.     }
  33.     private function isPluginActive(?SalesChannelContext $salesChannelContext null)
  34.     {
  35.         return $this->pluginConfig->isPluginActive($salesChannelContext);
  36.     }
  37.     private function addPostNumberBuildValidation(BuildValidationEvent $event)
  38.     {
  39.         if (!$this->pluginConfig->validatePostNumber()) {
  40.             return;
  41.         }
  42.         $postNumberKey 'repertusPackstationPostNumber';
  43.         $data $event->getData();
  44.         $definition $event->getDefinition();
  45.         if (!$data->has($postNumberKey)) {
  46.             $currentRequest $this->requestStack->getCurrentRequest();
  47.             if ($currentRequest && $postNumberValue $currentRequest->get($postNumberKey)) {
  48.                 $data->set($postNumberKey$postNumberValue);
  49.             }
  50.         }
  51.         if ($data->has($postNumberKey)) {
  52.             $shippingAddress =  $data->get('shippingAddress');
  53.             if ($shippingAddress && !$shippingAddress->has($postNumberKey)) {
  54.                 $shippingAddress->set($postNumberKey$data->get($postNumberKey));
  55.             }
  56.             $definition->add($postNumberKey, new PostNumber('/^[\d]*$/'),  new Length(null612));
  57.         }
  58.     }
  59. }