Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Exceptions.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
18  protected $_design = null;
19 
33  public function __construct(
34  \Magento\Framework\Model\Context $context,
35  \Magento\Framework\Registry $registry,
38  \Magento\Framework\View\DesignInterface $design,
39  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
40  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
41  array $data = [],
42  Json $serializer = null
43  ) {
44  $this->_design = $design;
45  parent::__construct(
46  $context,
47  $registry,
48  $config,
50  $resource,
51  $resourceCollection,
52  $data,
54  );
55  }
56 
64  public function beforeSave()
65  {
66  $design = clone $this->_design;
67  // For value validations
68  $exceptions = $this->getValue();
69 
70  foreach ($exceptions as $rowKey => &$row) {
71  unset($row['record_id']);
72  // Validate that all values have come
73  foreach (['search', 'value'] as $fieldName) {
74  if (!isset($row[$fieldName])) {
75  throw new \Magento\Framework\Exception\LocalizedException(
76  __('%1 does not contain field \'%2\'', $this->getData('field_config/fieldset'), $fieldName)
77  );
78  }
79  }
80 
81  // Empty string (match all) is not supported, because it means setting a default theme. Remove such entries.
82  if (!strlen($row['search'])) {
83  unset($exceptions[$rowKey]);
84  continue;
85  }
86 
87  // Validate the theme value
88  $design->setDesignTheme($row['value'], \Magento\Framework\App\Area::AREA_FRONTEND);
89 
90  // Compose regular exception pattern
91  $exceptions[$rowKey]['regexp'] = $this->_composeRegexp($row['search']);
92  }
93  $this->setValue($exceptions);
94 
95  return parent::beforeSave();
96  }
97 
105  protected function _composeRegexp($search)
106  {
107  // If valid regexp entered - do nothing
108  if (@preg_match($search, '') !== false) {
109  return $search;
110  }
111 
112  // Find out - whether user wanted to enter regexp or normal string.
113  if ($this->_isRegexp($search)) {
114  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid regular expression: "%1".', $search));
115  }
116 
117  return '/' . preg_quote($search, '/') . '/i';
118  }
119 
126  protected function _isRegexp($search)
127  {
128  if (strlen($search) < 3) {
129  return false;
130  }
131 
132  $possibleDelimiters = '/#~%';
133  // Limit delimiters to reduce possibility, that we miss string with regexp.
134 
135  // Starts with a delimiter
136  if (strpos($possibleDelimiters, $search[0]) !== false) {
137  return true;
138  }
139 
140  // Ends with a delimiter and (possible) modifiers
141  $pattern = '/[' . preg_quote($possibleDelimiters, '/') . '][imsxeADSUXJu]*$/';
142  if (preg_match($pattern, $search)) {
143  return true;
144  }
145 
146  return false;
147  }
148 
152  public function afterLoad()
153  {
154  parent::afterLoad();
155  $values = $this->getValue();
156  foreach ($values as &$value) {
157  if (isset($value['record_id'])) {
158  unset($value['record_id']);
159  }
160  }
161  $this->setValue($values);
162  return $this;
163  }
164 
168  public function getValue()
169  {
170  return $this->getData('value') ?: [];
171  }
172 }
getData($key='', $index=null)
Definition: DataObject.php:119
$pattern
Definition: website.php:22
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\View\DesignInterface $design, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], Json $serializer=null)
Definition: Exceptions.php:33