Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AllowedIps.php
Go to the documentation of this file.
1 <?php
7 
11 class AllowedIps extends \Magento\Framework\App\Config\Value
12 {
16  private $messageManager;
17 
23  protected $escaper;
24 
38  public function __construct(
39  \Magento\Framework\Model\Context $context,
40  \Magento\Framework\Registry $registry,
41  \Magento\Framework\App\Config\ScopeConfigInterface $config,
43  \Magento\Framework\Message\ManagerInterface $messageManager,
44  \Magento\Framework\Escaper $escaper,
45  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
46  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
47  array $data = []
48  ) {
49  $this->messageManager = $messageManager;
50  $this->escaper = $escaper;
51  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
52  }
53 
59  public function beforeSave()
60  {
61  $allowedIpsRaw = $this->escaper->escapeHtml($this->getValue());
62  $noticeMsgArray = [];
63  $allowedIpsArray = [];
64 
65  if (empty($allowedIpsRaw)) {
66  return parent::beforeSave();
67  }
68 
69  $dataArray = explode(',', $allowedIpsRaw);
70  foreach ($dataArray as $data) {
71  if (filter_var(trim($data), FILTER_VALIDATE_IP)) {
72  $allowedIpsArray[] = $data;
73  } else {
74  $noticeMsgArray[] = $data;
75  }
76  }
77 
78  $noticeMsg = implode(',', $noticeMsgArray);
79  if (!empty($noticeMsgArray)) {
80  $this->messageManager->addNotice(
81  __(
82  __('The following invalid values cannot be saved: %values', ['values' => $noticeMsg])
83  )
84  );
85  }
86 
87  $this->setValue(implode(',', $allowedIpsArray));
88  return parent::beforeSave();
89  }
90 }
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\Escaper $escaper, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: AllowedIps.php:38
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12