Validator.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Address;
  7. use Magento\Directory\Helper\Data as DirectoryHelper;
  8. use Magento\Directory\Model\CountryFactory;
  9. use Magento\Eav\Model\Config as EavConfig;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Sales\Model\Order\Address;
  12. /**
  13. * Class Validator
  14. */
  15. class Validator
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $required = [
  21. 'parent_id' => 'Parent Order Id',
  22. 'postcode' => 'Zip code',
  23. 'lastname' => 'Last name',
  24. 'street' => 'Street',
  25. 'city' => 'City',
  26. 'email' => 'Email',
  27. 'country_id' => 'Country',
  28. 'firstname' => 'First Name',
  29. 'address_type' => 'Address Type',
  30. ];
  31. /**
  32. * @var DirectoryHelper
  33. */
  34. protected $directoryHelper;
  35. /**
  36. * @var CountryFactory
  37. */
  38. protected $countryFactory;
  39. /**
  40. * @var EavConfig
  41. */
  42. protected $eavConfig;
  43. /**
  44. * @param DirectoryHelper $directoryHelper
  45. * @param CountryFactory $countryFactory
  46. * @param EavConfig $eavConfig
  47. */
  48. public function __construct(
  49. DirectoryHelper $directoryHelper,
  50. CountryFactory $countryFactory,
  51. EavConfig $eavConfig = null
  52. ) {
  53. $this->directoryHelper = $directoryHelper;
  54. $this->countryFactory = $countryFactory;
  55. $this->eavConfig = $eavConfig ?: ObjectManager::getInstance()
  56. ->get(EavConfig::class);
  57. if ($this->isTelephoneRequired()) {
  58. $this->required['telephone'] = 'Phone Number';
  59. }
  60. if ($this->isCompanyRequired()) {
  61. $this->required['company'] = 'Company';
  62. }
  63. if ($this->isFaxRequired()) {
  64. $this->required['fax'] = 'Fax';
  65. }
  66. }
  67. /**
  68. *
  69. * @param \Magento\Sales\Model\Order\Address $address
  70. * @return array
  71. */
  72. public function validate(Address $address)
  73. {
  74. $warnings = [];
  75. foreach ($this->required as $code => $label) {
  76. if (!$address->hasData($code)) {
  77. $warnings[] = sprintf('"%s" is required. Enter and try again.', $label);
  78. }
  79. }
  80. if (!filter_var($address->getEmail(), FILTER_VALIDATE_EMAIL)) {
  81. $warnings[] = 'Email has a wrong format';
  82. }
  83. if (!filter_var(in_array($address->getAddressType(), [Address::TYPE_BILLING, Address::TYPE_SHIPPING]))) {
  84. $warnings[] = 'Address type doesn\'t match required options';
  85. }
  86. return $warnings;
  87. }
  88. /**
  89. * Validate address attribute for customer creation
  90. *
  91. * @return bool|array
  92. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  93. * @SuppressWarnings(PHPMD.NPathComplexity)
  94. *
  95. * @param Address $address
  96. */
  97. public function validateForCustomer(Address $address)
  98. {
  99. if ($address->getShouldIgnoreValidation()) {
  100. return true;
  101. }
  102. $errors = [];
  103. if ($this->isEmpty($address->getFirstname())) {
  104. $errors[] = __('Please enter the first name.');
  105. }
  106. if ($this->isEmpty($address->getLastname())) {
  107. $errors[] = __('Please enter the last name.');
  108. }
  109. if ($this->isEmpty($address->getStreetLine(1))) {
  110. $errors[] = __('Please enter the street.');
  111. }
  112. if ($this->isEmpty($address->getCity())) {
  113. $errors[] = __('Please enter the city.');
  114. }
  115. if ($this->isTelephoneRequired()) {
  116. if ($this->isEmpty($address->getTelephone())) {
  117. $errors[] = __('Please enter the phone number.');
  118. }
  119. }
  120. if ($this->isCompanyRequired()) {
  121. if ($this->isEmpty($address->getCompany())) {
  122. $errors[] = __('Please enter the company.');
  123. }
  124. }
  125. if ($this->isFaxRequired()) {
  126. if ($this->isEmpty($address->getFax())) {
  127. $errors[] = __('Please enter the fax number.');
  128. }
  129. }
  130. $countryId = $address->getCountryId();
  131. if ($this->isZipRequired($countryId) && $this->isEmpty($address->getPostcode())) {
  132. $errors[] = __('Please enter the zip/postal code.');
  133. }
  134. if ($this->isEmpty($countryId)) {
  135. $errors[] = __('Please enter the country.');
  136. }
  137. if ($this->isStateRequired($countryId) && $this->isEmpty($address->getRegionId())) {
  138. $errors[] = __('Please enter the state/province.');
  139. }
  140. return empty($errors) ? true : $errors;
  141. }
  142. /**
  143. * Check if value is empty
  144. *
  145. * @param mixed $value
  146. * @return bool
  147. */
  148. protected function isEmpty($value)
  149. {
  150. return empty($value);
  151. }
  152. /**
  153. * Checks if zip for current country id is required
  154. *
  155. * @param string $countryId
  156. * @return bool
  157. */
  158. protected function isZipRequired($countryId)
  159. {
  160. return !in_array($countryId, $this->directoryHelper->getCountriesWithOptionalZip());
  161. }
  162. /**
  163. * Checks if state for current country id is required
  164. *
  165. * @param string $countryId
  166. * @return bool
  167. */
  168. protected function isStateRequired($countryId)
  169. {
  170. $country = $this->countryFactory->create()->load($countryId);
  171. return $this->directoryHelper->isRegionRequired($countryId) && $country->getRegionCollection()->getSize();
  172. }
  173. /**
  174. * @return bool
  175. */
  176. protected function isTelephoneRequired()
  177. {
  178. return ($this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
  179. }
  180. /**
  181. * @return bool
  182. */
  183. protected function isCompanyRequired()
  184. {
  185. return ($this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
  186. }
  187. /**
  188. * @return bool
  189. */
  190. protected function isFaxRequired()
  191. {
  192. return ($this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
  193. }
  194. }