General.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Address\Validator;
  7. use Magento\Customer\Model\Address\AbstractAddress;
  8. use Magento\Customer\Model\Address\ValidatorInterface;
  9. /**
  10. * Address general fields validator.
  11. */
  12. class General implements ValidatorInterface
  13. {
  14. /**
  15. * @var \Magento\Eav\Model\Config
  16. */
  17. private $eavConfig;
  18. /**
  19. * @var \Magento\Directory\Helper\Data
  20. */
  21. private $directoryData;
  22. /**
  23. * @param \Magento\Eav\Model\Config $eavConfig
  24. * @param \Magento\Directory\Helper\Data $directoryData
  25. */
  26. public function __construct(
  27. \Magento\Eav\Model\Config $eavConfig,
  28. \Magento\Directory\Helper\Data $directoryData
  29. ) {
  30. $this->eavConfig = $eavConfig;
  31. $this->directoryData = $directoryData;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function validate(AbstractAddress $address)
  37. {
  38. $errors = array_merge(
  39. $this->checkRequredFields($address),
  40. $this->checkOptionalFields($address)
  41. );
  42. return $errors;
  43. }
  44. /**
  45. * Check fields that are generally required.
  46. *
  47. * @param AbstractAddress $address
  48. * @return array
  49. * @throws \Zend_Validate_Exception
  50. */
  51. private function checkRequredFields(AbstractAddress $address)
  52. {
  53. $errors = [];
  54. if (!\Zend_Validate::is($address->getFirstname(), 'NotEmpty')) {
  55. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'firstname']);
  56. }
  57. if (!\Zend_Validate::is($address->getLastname(), 'NotEmpty')) {
  58. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'lastname']);
  59. }
  60. if (!\Zend_Validate::is($address->getStreetLine(1), 'NotEmpty')) {
  61. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
  62. }
  63. if (!\Zend_Validate::is($address->getCity(), 'NotEmpty')) {
  64. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']);
  65. }
  66. return $errors;
  67. }
  68. /**
  69. * Check fields that are conditionally required.
  70. *
  71. * @param AbstractAddress $address
  72. * @return array
  73. * @throws \Magento\Framework\Exception\LocalizedException
  74. * @throws \Zend_Validate_Exception
  75. */
  76. private function checkOptionalFields(AbstractAddress $address)
  77. {
  78. $errors = [];
  79. if ($this->isTelephoneRequired()
  80. && !\Zend_Validate::is($address->getTelephone(), 'NotEmpty')
  81. ) {
  82. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'telephone']);
  83. }
  84. if ($this->isFaxRequired()
  85. && !\Zend_Validate::is($address->getFax(), 'NotEmpty')
  86. ) {
  87. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'fax']);
  88. }
  89. if ($this->isCompanyRequired()
  90. && !\Zend_Validate::is($address->getCompany(), 'NotEmpty')
  91. ) {
  92. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
  93. }
  94. $havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip();
  95. if (!in_array($address->getCountryId(), $havingOptionalZip)
  96. && !\Zend_Validate::is($address->getPostcode(), 'NotEmpty')
  97. ) {
  98. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'postcode']);
  99. }
  100. return $errors;
  101. }
  102. /**
  103. * Check if company field required in configuration.
  104. *
  105. * @return bool
  106. * @throws \Magento\Framework\Exception\LocalizedException
  107. */
  108. private function isCompanyRequired()
  109. {
  110. return $this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired();
  111. }
  112. /**
  113. * Check if telephone field required in configuration.
  114. *
  115. * @return bool
  116. * @throws \Magento\Framework\Exception\LocalizedException
  117. */
  118. private function isTelephoneRequired()
  119. {
  120. return $this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired();
  121. }
  122. /**
  123. * Check if fax field required in configuration.
  124. *
  125. * @return bool
  126. * @throws \Magento\Framework\Exception\LocalizedException
  127. */
  128. private function isFaxRequired()
  129. {
  130. return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired();
  131. }
  132. }