Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Allow.php
Go to the documentation of this file.
1 <?php
12 
17 class Allow extends AbstractCurrency
18 {
22  protected $_localeCurrency;
23 
34  public function __construct(
35  \Magento\Framework\Model\Context $context,
36  \Magento\Framework\Registry $registry,
38  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
39  \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
40  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
41  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
42  array $data = []
43  ) {
44  $this->_localeCurrency = $localeCurrency;
45  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
46  }
47 
55  public function afterSave()
56  {
57  $exceptions = [];
58  $allowedCurrencies = $this->_getAllowedCurrencies();
59  foreach ($allowedCurrencies as $currencyCode) {
60  if (!in_array($currencyCode, $this->_getInstalledCurrencies())) {
61  $exceptions[] = __(
62  'Selected allowed currency "%1" is not available in installed currencies.',
63  $this->_localeCurrency->getCurrency($currencyCode)->getName()
64  );
65  }
66  }
67 
68  if (!in_array($this->_getCurrencyDefault(), $allowedCurrencies)) {
69  $exceptions[] = __(
70  'Default display currency "%1" is not available in allowed currencies.',
71  $this->_localeCurrency->getCurrency($this->_getCurrencyDefault())->getName()
72  );
73  }
74 
75  if ($exceptions) {
76  throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions)));
77  }
78 
79  return parent::afterSave();
80  }
81 
86  protected function _getAllowedCurrencies()
87  {
88  $value = $this->getValue();
89  $isFormData = $this->getData('groups/options/fields') !== null;
90  if ($isFormData && $this->getData('groups/options/fields/allow/inherit')) {
91  $value = (string)$this->_config->getValue(
92  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
93  $this->getScope(),
94  $this->getScopeId()
95  );
96  }
97 
98  return explode(',', $value);
99  }
100 }
getData($key='', $index=null)
Definition: DataObject.php:119
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Allow.php:34
$value
Definition: gender.phtml:16