PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 239 lines | 99 code | 20 blank | 120 comment | 11 complexity | 4722a34657b896e66d7d819e0d907a19 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: Filter.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 implements Zend_Filter_Interface
  32. {
  33. const CHAIN_APPEND = 'append';
  34. const CHAIN_PREPEND = 'prepend';
  35. /**
  36. * Filter chain
  37. *
  38. * @var array
  39. */
  40. protected $_filters = array();
  41. /**
  42. * Default Namespaces
  43. *
  44. * @var array
  45. */
  46. protected static $_defaultNamespaces = array();
  47. /**
  48. * Adds a filter to the chain
  49. *
  50. * @param Zend_Filter_Interface $filter
  51. * @param string $placement
  52. * @return Zend_Filter Provides a fluent interface
  53. */
  54. public function addFilter(Zend_Filter_Interface $filter, $placement = self::CHAIN_APPEND)
  55. {
  56. if ($placement == self::CHAIN_PREPEND) {
  57. array_unshift($this->_filters, $filter);
  58. } else {
  59. $this->_filters[] = $filter;
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Add a filter to the end of the chain
  65. *
  66. * @param Zend_Filter_Interface $filter
  67. * @return Zend_Filter Provides a fluent interface
  68. */
  69. public function appendFilter(Zend_Filter_Interface $filter)
  70. {
  71. return $this->addFilter($filter, self::CHAIN_APPEND);
  72. }
  73. /**
  74. * Add a filter to the start of the chain
  75. *
  76. * @param Zend_Filter_Interface $filter
  77. * @return Zend_Filter Provides a fluent interface
  78. */
  79. public function prependFilter(Zend_Filter_Interface $filter)
  80. {
  81. return $this->addFilter($filter, self::CHAIN_PREPEND);
  82. }
  83. /**
  84. * Get all the filters
  85. *
  86. * @return array
  87. */
  88. public function getFilters()
  89. {
  90. return $this->_filters;
  91. }
  92. /**
  93. * Returns $value filtered through each filter in the chain
  94. *
  95. * Filters are run in the order in which they were added to the chain (FIFO)
  96. *
  97. * @param mixed $value
  98. * @return mixed
  99. */
  100. public function filter($value)
  101. {
  102. $valueFiltered = $value;
  103. foreach ($this->_filters as $filter) {
  104. $valueFiltered = $filter->filter($valueFiltered);
  105. }
  106. return $valueFiltered;
  107. }
  108. /**
  109. * Returns the set default namespaces
  110. *
  111. * @return array
  112. */
  113. public static function getDefaultNamespaces()
  114. {
  115. return self::$_defaultNamespaces;
  116. }
  117. /**
  118. * Sets new default namespaces
  119. *
  120. * @param array|string $namespace
  121. * @return null
  122. */
  123. public static function setDefaultNamespaces($namespace)
  124. {
  125. if (!is_array($namespace)) {
  126. $namespace = array((string) $namespace);
  127. }
  128. self::$_defaultNamespaces = $namespace;
  129. }
  130. /**
  131. * Adds a new default namespace
  132. *
  133. * @param array|string $namespace
  134. * @return null
  135. */
  136. public static function addDefaultNamespaces($namespace)
  137. {
  138. if (!is_array($namespace)) {
  139. $namespace = array((string) $namespace);
  140. }
  141. self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
  142. }
  143. /**
  144. * Returns true when defaultNamespaces are set
  145. *
  146. * @return boolean
  147. */
  148. public static function hasDefaultNamespaces()
  149. {
  150. return (!empty(self::$_defaultNamespaces));
  151. }
  152. /**
  153. * @deprecated
  154. * @see Zend_Filter::filterStatic()
  155. *
  156. * @param mixed $value
  157. * @param string $classBaseName
  158. * @param array $args OPTIONAL
  159. * @param array|string $namespaces OPTIONAL
  160. * @return mixed
  161. * @throws Zend_Filter_Exception
  162. */
  163. public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
  164. {
  165. trigger_error(
  166. 'Zend_Filter::get() is deprecated as of 1.9.0; please update your code to utilize Zend_Filter::filterStatic()',
  167. E_USER_NOTICE
  168. );
  169. return self::filterStatic($value, $classBaseName, $args, $namespaces);
  170. }
  171. /**
  172. * Returns a value filtered through a specified filter class, without requiring separate
  173. * instantiation of the filter object.
  174. *
  175. * The first argument of this method is a data input value, that you would have filtered.
  176. * The second argument is a string, which corresponds to the basename of the filter class,
  177. * relative to the Zend_Filter namespace. This method automatically loads the class,
  178. * creates an instance, and applies the filter() method to the data input. You can also pass
  179. * an array of constructor arguments, if they are needed for the filter class.
  180. *
  181. * @param mixed $value
  182. * @param string $classBaseName
  183. * @param array $args OPTIONAL
  184. * @param array|string $namespaces OPTIONAL
  185. * @return mixed
  186. * @throws Zend_Filter_Exception
  187. */
  188. public static function filterStatic($value, $classBaseName, array $args = array(), $namespaces = array())
  189. {
  190. require_once 'Zend/Loader.php';
  191. $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Filter'));
  192. foreach ($namespaces as $namespace) {
  193. $className = $namespace . '_' . ucfirst($classBaseName);
  194. if (!class_exists($className, false)) {
  195. try {
  196. $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  197. if (Zend_Loader::isReadable($file)) {
  198. Zend_Loader::loadClass($className);
  199. } else {
  200. continue;
  201. }
  202. } catch (Zend_Exception $ze) {
  203. continue;
  204. }
  205. }
  206. $class = new ReflectionClass($className);
  207. if ($class->implementsInterface('Zend_Filter_Interface')) {
  208. if ($class->hasMethod('__construct')) {
  209. $object = $class->newInstanceArgs($args);
  210. } else {
  211. $object = $class->newInstance();
  212. }
  213. return $object->filter($value);
  214. }
  215. }
  216. require_once 'Zend/Filter/Exception.php';
  217. throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'");
  218. }
  219. }