Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Inflector.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Filter.php';
27 
31 #require_once 'Zend/Loader/PluginLoader.php';
32 
42 {
46  protected $_pluginLoader = null;
47 
51  protected $_target = null;
52 
56  protected $_throwTargetExceptionsOn = true;
57 
62 
66  protected $_rules = array();
67 
73  public function __construct($options = null)
74  {
75  if ($options instanceof Zend_Config) {
76  $options = $options->toArray();
77  } else if (!is_array($options)) {
78  $options = func_get_args();
79  $temp = array();
80 
81  if (!empty($options)) {
82  $temp['target'] = array_shift($options);
83  }
84 
85  if (!empty($options)) {
86  $temp['rules'] = array_shift($options);
87  }
88 
89  if (!empty($options)) {
90  $temp['throwTargetExceptionsOn'] = array_shift($options);
91  }
92 
93  if (!empty($options)) {
94  $temp['targetReplacementIdentifier'] = array_shift($options);
95  }
96 
97  $options = $temp;
98  }
99 
100  $this->setOptions($options);
101  }
102 
108  public function getPluginLoader()
109  {
110  if (!$this->_pluginLoader instanceof Zend_Loader_PluginLoader_Interface) {
111  $this->_pluginLoader = new Zend_Loader_PluginLoader(array('Zend_Filter_' => 'Zend/Filter/'), __CLASS__);
112  }
113 
114  return $this->_pluginLoader;
115  }
116 
124  {
125  $this->_pluginLoader = $pluginLoader;
126  return $this;
127  }
128 
136  public function setConfig(Zend_Config $config)
137  {
138  return $this->setOptions($config);
139  }
140 
147  public function setOptions($options) {
148  if ($options instanceof Zend_Config) {
149  $options = $options->toArray();
150  }
151 
152  // Set Präfix Path
153  if (array_key_exists('filterPrefixPath', $options)) {
154  if (!is_scalar($options['filterPrefixPath'])) {
155  foreach ($options['filterPrefixPath'] as $prefix => $path) {
157  }
158  }
159  }
160 
161  if (array_key_exists('throwTargetExceptionsOn', $options)) {
162  $this->setThrowTargetExceptionsOn($options['throwTargetExceptionsOn']);
163  }
164 
165  if (array_key_exists('targetReplacementIdentifier', $options)) {
166  $this->setTargetReplacementIdentifier($options['targetReplacementIdentifier']);
167  }
168 
169  if (array_key_exists('target', $options)) {
170  $this->setTarget($options['target']);
171  }
172 
173  if (array_key_exists('rules', $options)) {
174  $this->addRules($options['rules']);
175  }
176 
177  return $this;
178  }
179 
188  {
189  $this->getPluginLoader()->addPrefixPath($prefix, $path);
190  return $this;
191  }
192 
200  public function setThrowTargetExceptionsOn($throwTargetExceptionsOn)
201  {
202  $this->_throwTargetExceptionsOn = ($throwTargetExceptionsOn == true) ? true : false;
203  return $this;
204  }
205 
211  public function isThrowTargetExceptionsOn()
212  {
214  }
215 
222  public function setTargetReplacementIdentifier($targetReplacementIdentifier)
223  {
224  if ($targetReplacementIdentifier) {
225  $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier;
226  }
227 
228  return $this;
229  }
230 
237  {
239  }
240 
248  public function setTarget($target)
249  {
250  $this->_target = (string) $target;
251  return $this;
252  }
253 
259  public function getTarget()
260  {
261  return $this->_target;
262  }
263 
270  public function setTargetReference(&$target)
271  {
272  $this->_target =& $target;
273  return $this;
274  }
275 
283  public function setRules(Array $rules)
284  {
285  $this->clearRules();
286  $this->addRules($rules);
287  return $this;
288  }
289 
306  public function addRules(Array $rules)
307  {
308  $keys = array_keys($rules);
309  foreach ($keys as $spec) {
310  if ($spec[0] == ':') {
311  $this->addFilterRule($spec, $rules[$spec]);
312  } else {
313  $this->setStaticRule($spec, $rules[$spec]);
314  }
315  }
316 
317  return $this;
318  }
319 
329  public function getRules($spec = null)
330  {
331  if (null !== $spec) {
332  $spec = $this->_normalizeSpec($spec);
333  if (isset($this->_rules[$spec])) {
334  return $this->_rules[$spec];
335  }
336  return false;
337  }
338 
339  return $this->_rules;
340  }
341 
349  public function getRule($spec, $index)
350  {
351  $spec = $this->_normalizeSpec($spec);
352  if (isset($this->_rules[$spec]) && is_array($this->_rules[$spec])) {
353  if (isset($this->_rules[$spec][$index])) {
354  return $this->_rules[$spec][$index];
355  }
356  }
357  return false;
358  }
359 
365  public function clearRules()
366  {
367  $this->_rules = array();
368  return $this;
369  }
370 
379  public function setFilterRule($spec, $ruleSet)
380  {
381  $spec = $this->_normalizeSpec($spec);
382  $this->_rules[$spec] = array();
383  return $this->addFilterRule($spec, $ruleSet);
384  }
385 
393  public function addFilterRule($spec, $ruleSet)
394  {
395  $spec = $this->_normalizeSpec($spec);
396  if (!isset($this->_rules[$spec])) {
397  $this->_rules[$spec] = array();
398  }
399 
400  if (!is_array($ruleSet)) {
401  $ruleSet = array($ruleSet);
402  }
403 
404  if (is_string($this->_rules[$spec])) {
405  $temp = $this->_rules[$spec];
406  $this->_rules[$spec] = array();
407  $this->_rules[$spec][] = $temp;
408  }
409 
410  foreach ($ruleSet as $rule) {
411  $this->_rules[$spec][] = $this->_getRule($rule);
412  }
413 
414  return $this;
415  }
416 
424  public function setStaticRule($name, $value)
425  {
426  $name = $this->_normalizeSpec($name);
427  $this->_rules[$name] = (string) $value;
428  return $this;
429  }
430 
442  public function setStaticRuleReference($name, &$reference)
443  {
444  $name = $this->_normalizeSpec($name);
445  $this->_rules[$name] =& $reference;
446  return $this;
447  }
448 
455  public function filter($source)
456  {
457  // clean source
458  foreach ( (array) $source as $sourceName => $sourceValue) {
459  $source[ltrim($sourceName, ':')] = $sourceValue;
460  }
461 
462  $pregQuotedTargetReplacementIdentifier = preg_quote($this->_targetReplacementIdentifier, '#');
463  $processedParts = array();
464 
465  foreach ($this->_rules as $ruleName => $ruleValue) {
466  if (isset($source[$ruleName])) {
467  if (is_string($ruleValue)) {
468  // overriding the set rule
469  $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $source[$ruleName]);
470  } elseif (is_array($ruleValue)) {
471  $processedPart = $source[$ruleName];
472  foreach ($ruleValue as $ruleFilter) {
473  $processedPart = $ruleFilter->filter($processedPart);
474  }
475  $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $processedPart);
476  }
477  } elseif (is_string($ruleValue)) {
478  $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $ruleValue);
479  }
480  }
481 
482  // all of the values of processedParts would have been str_replace('\\', '\\\\', ..)'d to disable preg_replace backreferences
483  $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->_target);
484 
485  if ($this->_throwTargetExceptionsOn && (preg_match('#(?='.$pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true)) {
486  #require_once 'Zend/Filter/Exception.php';
487  throw new Zend_Filter_Exception('A replacement identifier ' . $this->_targetReplacementIdentifier . ' was found inside the inflected target, perhaps a rule was not satisfied with a target source? Unsatisfied inflected target: ' . $inflectedTarget);
488  }
489 
490  return $inflectedTarget;
491  }
492 
499  protected function _normalizeSpec($spec)
500  {
501  return ltrim((string) $spec, ':&');
502  }
503 
510  protected function _getRule($rule)
511  {
512  if ($rule instanceof Zend_Filter_Interface) {
513  return $rule;
514  }
515 
516  $rule = (string) $rule;
517 
518  $className = $this->getPluginLoader()->load($rule);
519  $ruleObject = new $className();
520  if (!$ruleObject instanceof Zend_Filter_Interface) {
521  #require_once 'Zend/Filter/Exception.php';
522  throw new Zend_Filter_Exception('No class named ' . $rule . ' implementing Zend_Filter_Interface could be found');
523  }
524 
525  return $ruleObject;
526  }
527 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
setConfig(Zend_Config $config)
Definition: Inflector.php:136
setStaticRuleReference($name, &$reference)
Definition: Inflector.php:442
addRules(Array $rules)
Definition: Inflector.php:306
addFilterPrefixPath($prefix, $path)
Definition: Inflector.php:187
getRule($spec, $index)
Definition: Inflector.php:349
setFilterRule($spec, $ruleSet)
Definition: Inflector.php:379
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
getRules($spec=null)
Definition: Inflector.php:329
setStaticRule($name, $value)
Definition: Inflector.php:424
setRules(Array $rules)
Definition: Inflector.php:283
addFilterRule($spec, $ruleSet)
Definition: Inflector.php:393
__construct($options=null)
Definition: Inflector.php:73
setPluginLoader(Zend_Loader_PluginLoader_Interface $pluginLoader)
Definition: Inflector.php:123
setTargetReference(&$target)
Definition: Inflector.php:270
setTargetReplacementIdentifier($targetReplacementIdentifier)
Definition: Inflector.php:222
$index
Definition: list.phtml:44
setThrowTargetExceptionsOn($throwTargetExceptionsOn)
Definition: Inflector.php:200
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14