Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AllowedCountryValidationRule.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\Framework\Validation\ValidationResultFactory;
14 
19 {
23  private $generalMessage;
24 
28  private $allowedCountryReader;
29 
33  private $validationResultFactory;
34 
40  public function __construct(
41  AllowedCountries $allowedCountryReader,
42  ValidationResultFactory $validationResultFactory,
43  string $generalMessage = ''
44  ) {
45  $this->allowedCountryReader = $allowedCountryReader;
46  $this->validationResultFactory = $validationResultFactory;
47  $this->generalMessage = $generalMessage;
48  }
49 
53  public function validate(Quote $quote): array
54  {
55  $validationErrors = [];
56 
57  if (!$quote->isVirtual()) {
58  $shippingAddress = $quote->getShippingAddress();
59  $shippingAddress->setStoreId($quote->getStoreId());
60  $validationResult =
61  in_array(
62  $shippingAddress->getCountryId(),
63  $this->allowedCountryReader->getAllowedCountries(
65  $quote->getStoreId()
66  )
67  );
68  if (!$validationResult) {
69  $validationErrors = [__($this->generalMessage)];
70  }
71  }
72 
73  return [$this->validationResultFactory->create(['errors' => $validationErrors])];
74  }
75 }
$quote
$shippingAddress
Definition: order.php:40
__()
Definition: __.php:13
__construct(AllowedCountries $allowedCountryReader, ValidationResultFactory $validationResultFactory, string $generalMessage='')