CountryValidator.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Validator;
  7. use Magento\Framework\Exception\NotFoundException;
  8. use Magento\Payment\Gateway\ConfigInterface;
  9. use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
  10. /**
  11. * Class CountryValidator
  12. * @package Magento\Payment\Gateway\Validator
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class CountryValidator extends AbstractValidator
  17. {
  18. /**
  19. * @var \Magento\Payment\Gateway\ConfigInterface
  20. */
  21. private $config;
  22. /**
  23. * @param ResultInterfaceFactory $resultFactory
  24. * @param \Magento\Payment\Gateway\ConfigInterface $config
  25. */
  26. public function __construct(
  27. ResultInterfaceFactory $resultFactory,
  28. ConfigInterface $config
  29. ) {
  30. $this->config = $config;
  31. parent::__construct($resultFactory);
  32. }
  33. /**
  34. * @param array $validationSubject
  35. * @return bool
  36. * @throws NotFoundException
  37. * @throws \Exception
  38. */
  39. public function validate(array $validationSubject)
  40. {
  41. $isValid = true;
  42. $storeId = $validationSubject['storeId'];
  43. if ((int)$this->config->getValue('allowspecific', $storeId) === 1) {
  44. $availableCountries = explode(
  45. ',',
  46. $this->config->getValue('specificcountry', $storeId)
  47. );
  48. if (!in_array($validationSubject['country'], $availableCountries)) {
  49. $isValid = false;
  50. }
  51. }
  52. return $this->createResult($isValid);
  53. }
  54. }