Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
General.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class General implements ValidatorInterface
15 {
19  private $eavConfig;
20 
24  private $directoryData;
25 
30  public function __construct(
31  \Magento\Eav\Model\Config $eavConfig,
32  \Magento\Directory\Helper\Data $directoryData
33  ) {
34  $this->eavConfig = $eavConfig;
35  $this->directoryData = $directoryData;
36  }
37 
42  {
43  $errors = array_merge(
44  $this->checkRequredFields($address),
45  $this->checkOptionalFields($address)
46  );
47 
48  return $errors;
49  }
50 
58  private function checkRequredFields(AbstractAddress $address)
59  {
60  $errors = [];
61  if (!\Zend_Validate::is($address->getFirstname(), 'NotEmpty')) {
62  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'firstname']);
63  }
64 
65  if (!\Zend_Validate::is($address->getLastname(), 'NotEmpty')) {
66  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'lastname']);
67  }
68 
69  if (!\Zend_Validate::is($address->getStreetLine(1), 'NotEmpty')) {
70  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
71  }
72 
73  if (!\Zend_Validate::is($address->getCity(), 'NotEmpty')) {
74  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'city']);
75  }
76 
77  return $errors;
78  }
79 
88  private function checkOptionalFields(AbstractAddress $address)
89  {
90  $errors = [];
91  if ($this->isTelephoneRequired()
92  && !\Zend_Validate::is($address->getTelephone(), 'NotEmpty')
93  ) {
94  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'telephone']);
95  }
96 
97  if ($this->isFaxRequired()
98  && !\Zend_Validate::is($address->getFax(), 'NotEmpty')
99  ) {
100  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'fax']);
101  }
102 
103  if ($this->isCompanyRequired()
104  && !\Zend_Validate::is($address->getCompany(), 'NotEmpty')
105  ) {
106  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
107  }
108 
109  $havingOptionalZip = $this->directoryData->getCountriesWithOptionalZip();
110  if (!in_array($address->getCountryId(), $havingOptionalZip)
111  && !\Zend_Validate::is($address->getPostcode(), 'NotEmpty')
112  ) {
113  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'postcode']);
114  }
115 
116  return $errors;
117  }
118 
125  private function isCompanyRequired()
126  {
127  return $this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired();
128  }
129 
136  private function isTelephoneRequired()
137  {
138  return $this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired();
139  }
140 
147  private function isFaxRequired()
148  {
149  return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired();
150  }
151 }
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(\Magento\Eav\Model\Config $eavConfig, \Magento\Directory\Helper\Data $directoryData)
Definition: General.php:30
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
$errors
Definition: overview.phtml:9