CustomerAddressCreateDataValidator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CustomerGraphQl\Model\Customer\Address;
  8. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  9. /**
  10. * Customer address create data validator
  11. */
  12. class CustomerAddressCreateDataValidator
  13. {
  14. /**
  15. * @var GetAllowedAddressAttributes
  16. */
  17. private $getAllowedAddressAttributes;
  18. /**
  19. * @param GetAllowedAddressAttributes $getAllowedAddressAttributes
  20. */
  21. public function __construct(GetAllowedAddressAttributes $getAllowedAddressAttributes)
  22. {
  23. $this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
  24. }
  25. /**
  26. * Validate customer address create data
  27. *
  28. * @param array $addressData
  29. * @return void
  30. * @throws GraphQlInputException
  31. */
  32. public function validate(array $addressData): void
  33. {
  34. $attributes = $this->getAllowedAddressAttributes->execute();
  35. $errorInput = [];
  36. foreach ($attributes as $attributeName => $attributeInfo) {
  37. if ($attributeInfo->getIsRequired()
  38. && (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
  39. ) {
  40. $errorInput[] = $attributeName;
  41. }
  42. }
  43. if ($errorInput) {
  44. throw new GraphQlInputException(
  45. __('Required parameters are missing: %1', [implode(', ', $errorInput)])
  46. );
  47. }
  48. }
  49. }