|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CustomerGraphQl\Model\Customer; |
| 9 | + |
| 10 | +use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 13 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 14 | +use Magento\Framework\Validator\EmailAddress as EmailAddressValidator; |
| 15 | + |
| 16 | +/** |
| 17 | + * Customer data validation used during customer account creation and updating |
| 18 | + */ |
| 19 | +class ValidateCustomerData |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Get allowed/required customer attributes |
| 23 | + * |
| 24 | + * @var GetAllowedCustomerAttributes |
| 25 | + */ |
| 26 | + private $getAllowedCustomerAttributes; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var EmailAddressValidator |
| 30 | + */ |
| 31 | + private $emailAddressValidator; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ValidateCustomerDataInterface[] |
| 35 | + */ |
| 36 | + private $validators = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * ValidateCustomerData constructor. |
| 40 | + * |
| 41 | + * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes |
| 42 | + * @param EmailAddressValidator $emailAddressValidator |
| 43 | + * @param array $validators |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + GetAllowedCustomerAttributes $getAllowedCustomerAttributes, |
| 47 | + EmailAddressValidator $emailAddressValidator, |
| 48 | + $validators = [] |
| 49 | + ) { |
| 50 | + $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; |
| 51 | + $this->emailAddressValidator = $emailAddressValidator; |
| 52 | + $this->validators = $validators; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Validate customer data |
| 57 | + * |
| 58 | + * @param array $customerData |
| 59 | + * @throws GraphQlInputException |
| 60 | + * @throws LocalizedException |
| 61 | + * @throws NoSuchEntityException |
| 62 | + */ |
| 63 | + public function execute(array $customerData) |
| 64 | + { |
| 65 | + /** @var ValidateCustomerDataInterface $validator */ |
| 66 | + foreach ($this->validators as $validator) { |
| 67 | + $validator->execute($customerData); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments