<?php declare(strict_types=1);
namespace CoeAccountOrtPlzSw6\Subscriber;
use CoeAccountOrtPlzSw6\Core\System\Country\CountryZipValidationEntity;
use CoeAccountOrtPlzSw6\Validator\Constraints\CityNameLength;
use CoeAccountOrtPlzSw6\Validator\Constraints\ZipCode;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Validation\BuildValidationEvent;
use Shopware\Core\System\SystemConfig\Service\ConfigurationService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
Class ValidationSubscriber implements EventSubscriberInterface {
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var EntityRepositoryInterface
*/
private $countryZipValidationRepository;
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* ValidationSubscriber constructor.
* @param RequestStack $requestStack
* @param EntityRepositoryInterface $countryZipValidationRepository
* @param SystemConfigService $systemConfigService
*/
public function __construct(RequestStack $requestStack, EntityRepositoryInterface $countryZipValidationRepository, SystemConfigService $systemConfigService) {
$this->requestStack = $requestStack;
$this->countryZipValidationRepository = $countryZipValidationRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents() {
return [
'framework.validation.address.create' => 'onBeforeValidateAddress',
'framework.validation.address.update' => 'onBeforeValidateAddress'
];
}
/**
* Validate some Front-End (address) input fields like taxNumber if it's set and required.
* @param BuildValidationEvent $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function onBeforeValidateAddress(BuildValidationEvent $event)
{
/** @var Request $request */
$request = $this -> requestStack->getCurrentRequest();
/** @var Array|null $billingAddress (You can choose between billing and shipping address during registration) */
$billingAddress = $request->get("billingAddress");
/** @var Array|null $shippingAddress (You can choose between billing and shipping address during registration) */
$shippingAddress = $request->get("shippingAddress");
/** @var Array|null $address (It's neither shipping, nor billing address if you add a new address in profile) */
$address = $request->get("address");
$countryId = null;
$city = null;
if($billingAddress){
$countryId = $billingAddress["countryId"];
$city = $billingAddress["city"];
}else if($shippingAddress){
$countryId = $shippingAddress["countryId"];
$city = $shippingAddress["city"];
}else if($address){
$countryId = $address["countryId"];
$city = $address["city"];
}
if($countryId != null){
/** @var CountryZipValidationEntity|null $entity */
$entity = $this->countryZipValidationRepository->search(
(new Criteria())
->setLimit(1)
->addFilter(
new EqualsFilter("countryId", $countryId)
),
$event->getContext()
)->first();
if($entity === null || !$entity->isActive()){
return;
}
$event->getDefinition()->add("zipcode", new ZipCode([
"min" => $entity->getMinLength(),
"max" => $entity->getMaxLength(),
"lettersAllowed" => $entity->getLettersAllowed(),
"pattern" => $entity->getPattern()
]));
}
/** @var int|null $cityMaxLength */
$cityMaxLength = $this->systemConfigService->get("CoeAccountOrtPlzSw6.config.cityMaxiumChars", $request->get("sw-sales-channel-id"));
if($city != null && $cityMaxLength != null){
$event->getDefinition()->add("city", new CityNameLength([
"max" => $cityMaxLength
]));
}
}
}