vendor/store.shopware.com/rhiemextendedregistration/src/Subscriber/Frontend/CustomerValidation.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace RhiemExtendedRegistration\Subscriber\Frontend;
  3. use RhiemExtendedRegistration\Components\Services\RegistrationGroupService;
  4. use RhiemExtendedRegistration\Entities\Attribute\AttributeEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\Event\DataMappingEvent;
  7. use Shopware\Core\Framework\Validation\DataValidator;
  8. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Validator\Constraints\Length;
  12. use Symfony\Component\Validator\Constraints\LessThanOrEqual;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  15. class CustomerValidation implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var ContainerInterface
  19.      */
  20.     private ContainerInterface $container;
  21.     /**
  22.      * @var RegistrationGroupService
  23.      */
  24.     private RegistrationGroupService $registrationGroupService;
  25.     /**
  26.      * @var DataValidator $dataValidator
  27.      */
  28.     private DataValidator $dataValidator;
  29.     /**
  30.      * CustomerValidation constructor.
  31.      *
  32.      * @param ContainerInterface $container
  33.      * @param RegistrationGroupService $registrationGroupService
  34.      * @param DataValidator $dataValidator
  35.      */
  36.     public function __construct(
  37.         ContainerInterface $container,
  38.         RegistrationGroupService $registrationGroupService,
  39.         DataValidator $dataValidator
  40.     )
  41.     {
  42.         $this->container $container;
  43.         $this->registrationGroupService $registrationGroupService;
  44.         $this->dataValidator $dataValidator;
  45.     }
  46.     /**
  47.      * @return string[]
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onValidateCustomer'
  53.         ];
  54.     }
  55.     /**
  56.      * @param DataMappingEvent $event
  57.      */
  58.     public function onValidateCustomer(DataMappingEvent $event): void
  59.     {
  60.         $data $event->getInput()->all();
  61.         $definition = new DataValidationDefinition;
  62.         $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  63.         try {
  64.             $registrationAttributes $this->registrationGroupService->getRegistrationAttributes($salesChannelId);
  65.         } catch (\Exception $e) {
  66.             $registrationAttributes = [];
  67.         }
  68.         if (count($registrationAttributes) > 0) {
  69.             $additionalValidation = new DataValidationDefinition('rhiem.additional.registration.fields');
  70.             foreach ($registrationAttributes as $attribute) {
  71.                 call_user_func_array(
  72.                     [$additionalValidation'add'],
  73.                     $this->createValidationParameters($attribute)
  74.                 );
  75.             }
  76.             $definition->addSub('Rhiem_Additional_Registration_Fields_Personal'$additionalValidation);
  77.         }
  78.         try {
  79.             $this->dataValidator->validate($data$definition);
  80.         } catch (ConstraintViolationException $formViolations) {
  81.             throw $formViolations;
  82.         }
  83.     }
  84.     /**
  85.      * @param AttributeEntity $attribute
  86.      *
  87.      * @return array
  88.      */
  89.     private function createValidationParameters(AttributeEntity $attribute): array
  90.     {
  91.         $return = [];
  92.         $return[] = $attribute->getId();
  93.         if ($attribute->getRequired()) {
  94.             $return[] = new NotBlank();
  95.         }
  96.         if ($attribute->getMinMax()) {
  97.             $lengthOptions = ["min" => $attribute->getValueMin(), "max" => $attribute->getValueMax() ?: null];
  98.             if (!$attribute->getRequired()) {
  99.                 $lengthOptions['allowEmptyString'] = true;
  100.             }
  101.             $return[] = new Length($lengthOptions);
  102.         }
  103.         return $return;
  104.     }
  105. }