Validator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model\Country\Postcode;
  7. class Validator implements ValidatorInterface
  8. {
  9. /**
  10. * @var ConfigInterface
  11. */
  12. protected $postCodesConfig;
  13. /**
  14. * @param ConfigInterface $postCodesConfig
  15. */
  16. public function __construct(\Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig)
  17. {
  18. $this->postCodesConfig = $postCodesConfig;
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function validate($postcode, $countryId)
  24. {
  25. $postCodes = $this->postCodesConfig->getPostCodes();
  26. if (isset($postCodes[$countryId]) && is_array($postCodes[$countryId])) {
  27. $patterns = $postCodes[$countryId];
  28. foreach ($patterns as $pattern) {
  29. preg_match('/' . $pattern['pattern'] . '/', $postcode, $matches);
  30. if (count($matches)) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. throw new \InvalidArgumentException('Provided countryId does not exist.');
  37. }
  38. }