PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Filter/PregReplace.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 174 lines | 67 code | 19 blank | 88 comment | 10 complexity | 68cbac600ef57293af1de0fcd0d1772a MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: PregReplace.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_PregReplace implements Zend_Filter_Interface
  32. {
  33. /**
  34. * Pattern to match
  35. * @var mixed
  36. */
  37. protected $_matchPattern = null;
  38. /**
  39. * Replacement pattern
  40. * @var mixed
  41. */
  42. protected $_replacement = '';
  43. /**
  44. * Is unicode enabled?
  45. *
  46. * @var bool
  47. */
  48. static protected $_unicodeSupportEnabled = null;
  49. /**
  50. * Is Unicode Support Enabled Utility function
  51. *
  52. * @return bool
  53. */
  54. static public function isUnicodeSupportEnabled()
  55. {
  56. if (self::$_unicodeSupportEnabled === null) {
  57. self::_determineUnicodeSupport();
  58. }
  59. return self::$_unicodeSupportEnabled;
  60. }
  61. /**
  62. * Method to cache the regex needed to determine if unicode support is available
  63. *
  64. * @return bool
  65. */
  66. static protected function _determineUnicodeSupport()
  67. {
  68. self::$_unicodeSupportEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
  69. }
  70. /**
  71. * Constructor
  72. * Supported options are
  73. * 'match' => matching pattern
  74. * 'replace' => replace with this
  75. *
  76. * @param string|array $options
  77. * @return void
  78. */
  79. public function __construct($options = null)
  80. {
  81. if ($options instanceof Zend_Config) {
  82. $options = $options->toArray();
  83. } else if (!is_array($options)) {
  84. $options = func_get_args();
  85. $temp = array();
  86. if (!empty($options)) {
  87. $temp['match'] = array_shift($options);
  88. }
  89. if (!empty($options)) {
  90. $temp['replace'] = array_shift($options);
  91. }
  92. $options = $temp;
  93. }
  94. if (array_key_exists('match', $options)) {
  95. $this->setMatchPattern($options['match']);
  96. }
  97. if (array_key_exists('replace', $options)) {
  98. $this->setReplacement($options['replace']);
  99. }
  100. }
  101. /**
  102. * Set the match pattern for the regex being called within filter()
  103. *
  104. * @param mixed $match - same as the first argument of preg_replace
  105. * @return Zend_Filter_PregReplace
  106. */
  107. public function setMatchPattern($match)
  108. {
  109. $this->_matchPattern = $match;
  110. return $this;
  111. }
  112. /**
  113. * Get currently set match pattern
  114. *
  115. * @return string
  116. */
  117. public function getMatchPattern()
  118. {
  119. return $this->_matchPattern;
  120. }
  121. /**
  122. * Set the Replacement pattern/string for the preg_replace called in filter
  123. *
  124. * @param mixed $replacement - same as the second argument of preg_replace
  125. * @return Zend_Filter_PregReplace
  126. */
  127. public function setReplacement($replacement)
  128. {
  129. $this->_replacement = $replacement;
  130. return $this;
  131. }
  132. /**
  133. * Get currently set replacement value
  134. *
  135. * @return string
  136. */
  137. public function getReplacement()
  138. {
  139. return $this->_replacement;
  140. }
  141. /**
  142. * Perform regexp replacement as filter
  143. *
  144. * @param string $value
  145. * @return string
  146. */
  147. public function filter($value)
  148. {
  149. if ($this->_matchPattern == null) {
  150. require_once 'Zend/Filter/Exception.php';
  151. throw new Zend_Filter_Exception(get_class($this) . ' does not have a valid MatchPattern set.');
  152. }
  153. return preg_replace($this->_matchPattern, $this->_replacement, $value);
  154. }
  155. }