Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validator.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Directory\Helper\Data as DirectoryHelper;
10 use Magento\Directory\Model\CountryFactory;
11 use Magento\Eav\Model\Config as EavConfig;
14 
18 class Validator
19 {
23  protected $required = [
24  'parent_id' => 'Parent Order Id',
25  'postcode' => 'Zip code',
26  'lastname' => 'Last name',
27  'street' => 'Street',
28  'city' => 'City',
29  'email' => 'Email',
30  'country_id' => 'Country',
31  'firstname' => 'First Name',
32  'address_type' => 'Address Type',
33  ];
34 
38  protected $directoryHelper;
39 
43  protected $countryFactory;
44 
48  protected $eavConfig;
49 
55  public function __construct(
56  DirectoryHelper $directoryHelper,
57  CountryFactory $countryFactory,
58  EavConfig $eavConfig = null
59  ) {
60  $this->directoryHelper = $directoryHelper;
61  $this->countryFactory = $countryFactory;
62  $this->eavConfig = $eavConfig ?: ObjectManager::getInstance()
63  ->get(EavConfig::class);
64 
65  if ($this->isTelephoneRequired()) {
66  $this->required['telephone'] = 'Phone Number';
67  }
68 
69  if ($this->isCompanyRequired()) {
70  $this->required['company'] = 'Company';
71  }
72 
73  if ($this->isFaxRequired()) {
74  $this->required['fax'] = 'Fax';
75  }
76  }
77 
83  public function validate(Address $address)
84  {
85  $warnings = [];
86  foreach ($this->required as $code => $label) {
87  if (!$address->hasData($code)) {
88  $warnings[] = sprintf('"%s" is required. Enter and try again.', $label);
89  }
90  }
91  if (!filter_var($address->getEmail(), FILTER_VALIDATE_EMAIL)) {
92  $warnings[] = 'Email has a wrong format';
93  }
94  if (!filter_var(in_array($address->getAddressType(), [Address::TYPE_BILLING, Address::TYPE_SHIPPING]))) {
95  $warnings[] = 'Address type doesn\'t match required options';
96  }
97  return $warnings;
98  }
99 
110  {
111  if ($address->getShouldIgnoreValidation()) {
112  return true;
113  }
114 
115  $errors = [];
116 
117  if ($this->isEmpty($address->getFirstname())) {
118  $errors[] = __('Please enter the first name.');
119  }
120  if ($this->isEmpty($address->getLastname())) {
121  $errors[] = __('Please enter the last name.');
122  }
123  if ($this->isEmpty($address->getStreetLine(1))) {
124  $errors[] = __('Please enter the street.');
125  }
126  if ($this->isEmpty($address->getCity())) {
127  $errors[] = __('Please enter the city.');
128  }
129 
130  if ($this->isTelephoneRequired()) {
131  if ($this->isEmpty($address->getTelephone())) {
132  $errors[] = __('Please enter the phone number.');
133  }
134  }
135 
136  if ($this->isCompanyRequired()) {
137  if ($this->isEmpty($address->getCompany())) {
138  $errors[] = __('Please enter the company.');
139  }
140  }
141 
142  if ($this->isFaxRequired()) {
143  if ($this->isEmpty($address->getFax())) {
144  $errors[] = __('Please enter the fax number.');
145  }
146  }
147 
148  $countryId = $address->getCountryId();
149 
150  if ($this->isZipRequired($countryId) && $this->isEmpty($address->getPostcode())) {
151  $errors[] = __('Please enter the zip/postal code.');
152  }
153  if ($this->isEmpty($countryId)) {
154  $errors[] = __('Please enter the country.');
155  }
156  if ($this->isStateRequired($countryId) && $this->isEmpty($address->getRegionId())) {
157  $errors[] = __('Please enter the state/province.');
158  }
159 
160  return empty($errors) ? true : $errors;
161  }
162 
169  protected function isEmpty($value)
170  {
171  return empty($value);
172  }
173 
180  protected function isZipRequired($countryId)
181  {
182  return !in_array($countryId, $this->directoryHelper->getCountriesWithOptionalZip());
183  }
184 
191  protected function isStateRequired($countryId)
192  {
193  $country = $this->countryFactory->create()->load($countryId);
194  return $this->directoryHelper->isRegionRequired($countryId) && $country->getRegionCollection()->getSize();
195  }
196 
200  protected function isTelephoneRequired()
201  {
202  return ($this->eavConfig->getAttribute('customer_address', 'telephone')->getIsRequired());
203  }
204 
208  protected function isCompanyRequired()
209  {
210  return ($this->eavConfig->getAttribute('customer_address', 'company')->getIsRequired());
211  }
212 
216  protected function isFaxRequired()
217  {
218  return ($this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired());
219  }
220 }
__()
Definition: __.php:13
$address
Definition: customer.php:38
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
__construct(DirectoryHelper $directoryHelper, CountryFactory $countryFactory, EavConfig $eavConfig=null)
Definition: Validator.php:55
$errors
Definition: overview.phtml:9
$code
Definition: info.phtml:12