/vendor/zendframework/zendframework/library/Zend/Filter/PregReplace.php

https://bitbucket.org/zbahij/eprojets_app · PHP · 162 lines · 91 code · 16 blank · 55 comment · 16 complexity · 99bd7008f268cc86e11d3aa3df5d0f75 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Filter;
  10. use Traversable;
  11. class PregReplace extends AbstractFilter
  12. {
  13. protected $options = array(
  14. 'pattern' => null,
  15. 'replacement' => '',
  16. );
  17. /**
  18. * Constructor
  19. * Supported options are
  20. * 'pattern' => matching pattern
  21. * 'replacement' => replace with this
  22. *
  23. * @param array|Traversable|string|null $options
  24. */
  25. public function __construct($options = null)
  26. {
  27. if ($options instanceof Traversable) {
  28. $options = iterator_to_array($options);
  29. }
  30. if (!is_array($options)
  31. || (!isset($options['pattern']) && !isset($options['replacement'])))
  32. {
  33. $args = func_get_args();
  34. if (isset($args[0])) {
  35. $this->setPattern($args[0]);
  36. }
  37. if (isset($args[1])) {
  38. $this->setReplacement($args[1]);
  39. }
  40. } else {
  41. $this->setOptions($options);
  42. }
  43. }
  44. /**
  45. * Set the regex pattern to search for
  46. * @see preg_replace()
  47. *
  48. * @param string|array $pattern - same as the first argument of preg_replace
  49. * @return PregReplace
  50. * @throws Exception\InvalidArgumentException
  51. */
  52. public function setPattern($pattern)
  53. {
  54. if (!is_array($pattern) && !is_string($pattern)) {
  55. throw new Exception\InvalidArgumentException(sprintf(
  56. '%s expects pattern to be array or string; received "%s"',
  57. __METHOD__,
  58. (is_object($pattern) ? get_class($pattern) : gettype($pattern))
  59. ));
  60. }
  61. if (is_array($pattern)) {
  62. foreach ($pattern as $p) {
  63. $this->validatePattern($p);
  64. }
  65. }
  66. if (is_string($pattern)) {
  67. $this->validatePattern($pattern);
  68. }
  69. $this->options['pattern'] = $pattern;
  70. return $this;
  71. }
  72. /**
  73. * Get currently set match pattern
  74. *
  75. * @return string|array
  76. */
  77. public function getPattern()
  78. {
  79. return $this->options['pattern'];
  80. }
  81. /**
  82. * Set the replacement array/string
  83. * @see preg_replace()
  84. *
  85. * @param array|string $replacement - same as the second argument of preg_replace
  86. * @return PregReplace
  87. * @throws Exception\InvalidArgumentException
  88. */
  89. public function setReplacement($replacement)
  90. {
  91. if (!is_array($replacement) && !is_string($replacement)) {
  92. throw new Exception\InvalidArgumentException(sprintf(
  93. '%s expects replacement to be array or string; received "%s"',
  94. __METHOD__,
  95. (is_object($replacement) ? get_class($replacement) : gettype($replacement))
  96. ));
  97. }
  98. $this->options['replacement'] = $replacement;
  99. return $this;
  100. }
  101. /**
  102. * Get currently set replacement value
  103. *
  104. * @return string|array
  105. */
  106. public function getReplacement()
  107. {
  108. return $this->options['replacement'];
  109. }
  110. /**
  111. * Perform regexp replacement as filter
  112. *
  113. * @param mixed $value
  114. * @return mixed
  115. * @throws Exception\RuntimeException
  116. */
  117. public function filter($value)
  118. {
  119. if ($this->options['pattern'] === null) {
  120. throw new Exception\RuntimeException(sprintf(
  121. 'Filter %s does not have a valid pattern set',
  122. get_class($this)
  123. ));
  124. }
  125. return preg_replace($this->options['pattern'], $this->options['replacement'], $value);
  126. }
  127. /**
  128. * Validate a pattern and ensure it does not contain the "e" modifier
  129. *
  130. * @param string $pattern
  131. * @return bool
  132. * @throws Exception\InvalidArgumentException
  133. */
  134. protected function validatePattern($pattern)
  135. {
  136. if (!preg_match('/(?<modifier>[imsxeADSUXJu]+)$/', $pattern, $matches)) {
  137. return true;
  138. }
  139. if (false !== strstr($matches['modifier'], 'e')) {
  140. throw new Exception\InvalidArgumentException(sprintf(
  141. 'Pattern for a PregReplace filter may not contain the "e" pattern modifier; received "%s"',
  142. $pattern
  143. ));
  144. }
  145. }
  146. }