custom/plugins/CoeAccountOrtPlzSw6/src/Subscriber/ValidationSubscriber.php line 64

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeAccountOrtPlzSw6\Subscriber;
  3. use CoeAccountOrtPlzSw6\Core\System\Country\CountryZipValidationEntity;
  4. use CoeAccountOrtPlzSw6\Validator\Constraints\CityNameLength;
  5. use CoeAccountOrtPlzSw6\Validator\Constraints\ZipCode;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  11. use Shopware\Core\System\SystemConfig\Service\ConfigurationService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Validator\Constraints\Blank;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Validator\Constraints\NotBlank;
  19. Class ValidationSubscriber implements EventSubscriberInterface {
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     private $requestStack;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $countryZipValidationRepository;
  28.     /**
  29.      * @var SystemConfigService
  30.      */
  31.     private $systemConfigService;
  32.     /**
  33.      * ValidationSubscriber constructor.
  34.      * @param RequestStack $requestStack
  35.      * @param EntityRepositoryInterface $countryZipValidationRepository
  36.      * @param SystemConfigService $systemConfigService
  37.      */
  38.     public function __construct(RequestStack $requestStackEntityRepositoryInterface $countryZipValidationRepositorySystemConfigService $systemConfigService) {
  39.         $this->requestStack $requestStack;
  40.         $this->countryZipValidationRepository $countryZipValidationRepository;
  41.         $this->systemConfigService $systemConfigService;
  42.     }
  43.     public static function getSubscribedEvents() {
  44.         return [
  45.             'framework.validation.address.create' => 'onBeforeValidateAddress',
  46.             'framework.validation.address.update' => 'onBeforeValidateAddress'
  47.         ];
  48.     }
  49.     /**
  50.      * Validate some Front-End (address) input fields like taxNumber if it's set and required.
  51.      * @param BuildValidationEvent $event
  52.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  53.      */
  54.     public function onBeforeValidateAddress(BuildValidationEvent $event)
  55.     {
  56.         /** @var Request $request */
  57.         $request $this -> requestStack->getCurrentRequest();
  58.         /** @var Array|null $billingAddress (You can choose between billing and shipping address during registration) */
  59.         $billingAddress $request->get("billingAddress");
  60.         /** @var Array|null $shippingAddress (You can choose between billing and shipping address during registration) */
  61.         $shippingAddress $request->get("shippingAddress");
  62.         /** @var Array|null $address (It's neither shipping, nor billing address if you add a new address in profile) */
  63.         $address $request->get("address");
  64.         $countryId null;
  65.         $city null;
  66.         if($billingAddress){
  67.             $countryId $billingAddress["countryId"];
  68.             $city $billingAddress["city"];
  69.         }else if($shippingAddress){
  70.             $countryId $shippingAddress["countryId"];
  71.             $city $shippingAddress["city"];
  72.         }else if($address){
  73.             $countryId $address["countryId"];
  74.             $city $address["city"];
  75.         }
  76.         if($countryId != null){
  77.             /** @var CountryZipValidationEntity|null $entity */
  78.             $entity $this->countryZipValidationRepository->search(
  79.                 (new Criteria())
  80.                     ->setLimit(1)
  81.                     ->addFilter(
  82.                         new EqualsFilter("countryId"$countryId)
  83.                     ),
  84.                 $event->getContext()
  85.             )->first();
  86.             if($entity === null || !$entity->isActive()){
  87.                 return;
  88.             }
  89.             $event->getDefinition()->add("zipcode", new ZipCode([
  90.                 "min" => $entity->getMinLength(),
  91.                 "max" => $entity->getMaxLength(),
  92.                 "lettersAllowed" => $entity->getLettersAllowed(),
  93.                 "pattern" => $entity->getPattern()
  94.             ]));
  95.         }
  96.         /** @var int|null $cityMaxLength */
  97.         $cityMaxLength $this->systemConfigService->get("CoeAccountOrtPlzSw6.config.cityMaxiumChars"$request->get("sw-sales-channel-id"));
  98.         if($city != null && $cityMaxLength != null){
  99.             $event->getDefinition()->add("city", new CityNameLength([
  100.                 "max" => $cityMaxLength
  101.             ]));
  102.         }
  103.     }
  104. }