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
7 
8 class Validator implements ValidatorInterface
9 {
13  protected $postCodesConfig;
14 
18  public function __construct(\Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig)
19  {
20  $this->postCodesConfig = $postCodesConfig;
21  }
22 
26  public function validate($postcode, $countryId)
27  {
28  $postCodes = $this->postCodesConfig->getPostCodes();
29  if (isset($postCodes[$countryId]) && is_array($postCodes[$countryId])) {
30  $patterns = $postCodes[$countryId];
31  foreach ($patterns as $pattern) {
32  preg_match('/' . $pattern['pattern'] . '/', $postcode, $matches);
33  if (count($matches)) {
34  return true;
35  }
36  }
37  return false;
38  }
39  throw new \InvalidArgumentException('Provided countryId does not exist.');
40  }
41 }
$pattern
Definition: website.php:22
__construct(\Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig)
Definition: Validator.php:18