Validator.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Address;
  7. use Zend_Validate_Exception;
  8. class Validator extends \Magento\Framework\Validator\AbstractValidator
  9. {
  10. /**
  11. * @var \Magento\Directory\Model\CountryFactory
  12. */
  13. protected $countryFactory;
  14. /**
  15. * @param \Magento\Directory\Model\CountryFactory $countryFactory
  16. */
  17. public function __construct(
  18. \Magento\Directory\Model\CountryFactory $countryFactory
  19. ) {
  20. $this->countryFactory = $countryFactory;
  21. }
  22. /**
  23. * Returns true if and only if $value meets the validation requirements
  24. *
  25. * If $value fails validation, then this method returns false, and
  26. * getMessages() will return an array of messages that explain why the
  27. * validation failed.
  28. *
  29. * @param \Magento\Quote\Model\Quote\Address $value
  30. * @return boolean
  31. * @throws Zend_Validate_Exception If validation of $value is impossible
  32. */
  33. public function isValid($value)
  34. {
  35. $messages = [];
  36. $email = $value->getEmail();
  37. if (!empty($email) && !\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
  38. $messages['invalid_email_format'] = 'Invalid email format';
  39. }
  40. $countryId = $value->getCountryId();
  41. if (!empty($countryId)) {
  42. $country = $this->countryFactory->create();
  43. $country->load($countryId);
  44. if (!$country->getId()) {
  45. $messages['invalid_country_code'] = 'Invalid country code';
  46. }
  47. }
  48. $this->_addMessages($messages);
  49. return empty($messages);
  50. }
  51. }