Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Country.php
Go to the documentation of this file.
1 <?php
7 
13 
17 class Country implements ValidatorInterface
18 {
22  private $directoryData;
23 
27  private $allowedCountriesReader;
28 
33  public function __construct(
34  Data $directoryData,
35  AllowedCountries $allowedCountriesReader
36  ) {
37  $this->directoryData = $directoryData;
38  $this->allowedCountriesReader = $allowedCountriesReader;
39  }
40 
45  {
46  $errors = $this->validateCountry($address);
47  if (empty($errors)) {
48  $errors = $this->validateRegion($address);
49  }
50 
51  return $errors;
52  }
53 
60  private function validateCountry(AbstractAddress $address)
61  {
62  $countryId = $address->getCountryId();
63  $errors = [];
64  if (!\Zend_Validate::is($countryId, 'NotEmpty')) {
65  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'countryId']);
66  } elseif (!in_array($countryId, $this->getWebsiteAllowedCountries($address), true)) {
67  //Checking if such country exists.
68  $errors[] = __(
69  'Invalid value of "%value" provided for the %fieldName field.',
70  ['fieldName' => 'countryId', 'value' => htmlspecialchars($countryId)]
71  );
72  }
73 
74  return $errors;
75  }
76 
85  private function validateRegion(AbstractAddress $address)
86  {
87  $errors = [];
88  $countryId = $address->getCountryId();
89  $countryModel = $address->getCountryModel();
90  $regionCollection = $countryModel->getRegionCollection();
91  $region = $address->getRegion();
92  $regionId = (string)$address->getRegionId();
93  $allowedRegions = $regionCollection->getAllIds();
94  $isRegionRequired = $this->directoryData->isRegionRequired($countryId);
95  if ($isRegionRequired && empty($allowedRegions) && !\Zend_Validate::is($region, 'NotEmpty')) {
96  //If region is required for country and country doesn't provide regions list
97  //region must be provided.
98  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'region']);
99  } elseif ($allowedRegions && !\Zend_Validate::is($regionId, 'NotEmpty') && $isRegionRequired) {
100  //If country actually has regions and requires you to
101  //select one then it must be selected.
102  $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'regionId']);
103  } elseif ($allowedRegions && $regionId && !in_array($regionId, $allowedRegions, true)) {
104  //If a region is selected then checking if it exists.
105  $errors[] = __(
106  'Invalid value of "%value" provided for the %fieldName field.',
107  ['fieldName' => 'regionId', 'value' => htmlspecialchars($regionId)]
108  );
109  }
110 
111  return $errors;
112  }
113 
120  private function getWebsiteAllowedCountries(AbstractAddress $address): array
121  {
122  $storeId = $address->getData('store_id');
123  return $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $storeId);
124  }
125 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$address
Definition: customer.php:38
__construct(Data $directoryData, AllowedCountries $allowedCountriesReader)
Definition: Country.php:33
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
$errors
Definition: overview.phtml:9