PageRenderTime 134ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 527 lines | 242 code | 60 blank | 225 comment | 35 complexity | 1c4b360d6995f3657a336e819d62e4da 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: Inflector.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Filter
  23. * @see Zend_Filter_Interface
  24. */
  25. require_once 'Zend/Filter.php';
  26. /**
  27. * @see Zend_Loader_PluginLoader
  28. */
  29. require_once 'Zend/Loader/PluginLoader.php';
  30. /**
  31. * Filter chain for string inflection
  32. *
  33. * @category Zend
  34. * @package Zend_Filter
  35. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Filter_Inflector implements Zend_Filter_Interface
  39. {
  40. /**
  41. * @var Zend_Loader_PluginLoader_Interface
  42. */
  43. protected $_pluginLoader = null;
  44. /**
  45. * @var string
  46. */
  47. protected $_target = null;
  48. /**
  49. * @var bool
  50. */
  51. protected $_throwTargetExceptionsOn = true;
  52. /**
  53. * @var string
  54. */
  55. protected $_targetReplacementIdentifier = ':';
  56. /**
  57. * @var array
  58. */
  59. protected $_rules = array();
  60. /**
  61. * Constructor
  62. *
  63. * @param string|array $options Options to set
  64. */
  65. public function __construct($options = null)
  66. {
  67. if ($options instanceof Zend_Config) {
  68. $options = $options->toArray();
  69. } else if (!is_array($options)) {
  70. $options = func_get_args();
  71. $temp = array();
  72. if (!empty($options)) {
  73. $temp['target'] = array_shift($options);
  74. }
  75. if (!empty($options)) {
  76. $temp['rules'] = array_shift($options);
  77. }
  78. if (!empty($options)) {
  79. $temp['throwTargetExceptionsOn'] = array_shift($options);
  80. }
  81. if (!empty($options)) {
  82. $temp['targetReplacementIdentifier'] = array_shift($options);
  83. }
  84. $options = $temp;
  85. }
  86. $this->setOptions($options);
  87. }
  88. /**
  89. * Retreive PluginLoader
  90. *
  91. * @return Zend_Loader_PluginLoader_Interface
  92. */
  93. public function getPluginLoader()
  94. {
  95. if (!$this->_pluginLoader instanceof Zend_Loader_PluginLoader_Interface) {
  96. $this->_pluginLoader = new Zend_Loader_PluginLoader(array('Zend_Filter_' => 'Zend/Filter/'), __CLASS__);
  97. }
  98. return $this->_pluginLoader;
  99. }
  100. /**
  101. * Set PluginLoader
  102. *
  103. * @param Zend_Loader_PluginLoader_Interface $pluginLoader
  104. * @return Zend_Filter_Inflector
  105. */
  106. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $pluginLoader)
  107. {
  108. $this->_pluginLoader = $pluginLoader;
  109. return $this;
  110. }
  111. /**
  112. * Use Zend_Config object to set object state
  113. *
  114. * @deprecated Use setOptions() instead
  115. * @param Zend_Config $config
  116. * @return Zend_Filter_Inflector
  117. */
  118. public function setConfig(Zend_Config $config)
  119. {
  120. return $this->setOptions($config);
  121. }
  122. /**
  123. * Set options
  124. *
  125. * @param array $options
  126. * @return Zend_Filter_Inflector
  127. */
  128. public function setOptions($options) {
  129. if ($options instanceof Zend_Config) {
  130. $options = $options->toArray();
  131. }
  132. // Set Präfix Path
  133. if (array_key_exists('filterPrefixPath', $options)) {
  134. if (!is_scalar($options['filterPrefixPath'])) {
  135. foreach ($options['filterPrefixPath'] as $prefix => $path) {
  136. $this->addFilterPrefixPath($prefix, $path);
  137. }
  138. }
  139. }
  140. if (array_key_exists('throwTargetExceptionsOn', $options)) {
  141. $this->setThrowTargetExceptionsOn($options['throwTargetExceptionsOn']);
  142. }
  143. if (array_key_exists('targetReplacementIdentifier', $options)) {
  144. $this->setTargetReplacementIdentifier($options['targetReplacementIdentifier']);
  145. }
  146. if (array_key_exists('target', $options)) {
  147. $this->setTarget($options['target']);
  148. }
  149. if (array_key_exists('rules', $options)) {
  150. $this->addRules($options['rules']);
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Convienence method to add prefix and path to PluginLoader
  156. *
  157. * @param string $prefix
  158. * @param string $path
  159. * @return Zend_Filter_Inflector
  160. */
  161. public function addFilterPrefixPath($prefix, $path)
  162. {
  163. $this->getPluginLoader()->addPrefixPath($prefix, $path);
  164. return $this;
  165. }
  166. /**
  167. * Set Whether or not the inflector should throw an exception when a replacement
  168. * identifier is still found within an inflected target.
  169. *
  170. * @param bool $throwTargetExceptions
  171. * @return Zend_Filter_Inflector
  172. */
  173. public function setThrowTargetExceptionsOn($throwTargetExceptionsOn)
  174. {
  175. $this->_throwTargetExceptionsOn = ($throwTargetExceptionsOn == true) ? true : false;
  176. return $this;
  177. }
  178. /**
  179. * Will exceptions be thrown?
  180. *
  181. * @return bool
  182. */
  183. public function isThrowTargetExceptionsOn()
  184. {
  185. return $this->_throwTargetExceptionsOn;
  186. }
  187. /**
  188. * Set the Target Replacement Identifier, by default ':'
  189. *
  190. * @param string $targetReplacementIdentifier
  191. * @return Zend_Filter_Inflector
  192. */
  193. public function setTargetReplacementIdentifier($targetReplacementIdentifier)
  194. {
  195. if ($targetReplacementIdentifier) {
  196. $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier;
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Get Target Replacement Identifier
  202. *
  203. * @return string
  204. */
  205. public function getTargetReplacementIdentifier()
  206. {
  207. return $this->_targetReplacementIdentifier;
  208. }
  209. /**
  210. * Set a Target
  211. * ex: 'scripts/:controller/:action.:suffix'
  212. *
  213. * @param string
  214. * @return Zend_Filter_Inflector
  215. */
  216. public function setTarget($target)
  217. {
  218. $this->_target = (string) $target;
  219. return $this;
  220. }
  221. /**
  222. * Retrieve target
  223. *
  224. * @return string
  225. */
  226. public function getTarget()
  227. {
  228. return $this->_target;
  229. }
  230. /**
  231. * Set Target Reference
  232. *
  233. * @param reference $target
  234. * @return Zend_Filter_Inflector
  235. */
  236. public function setTargetReference(&$target)
  237. {
  238. $this->_target =& $target;
  239. return $this;
  240. }
  241. /**
  242. * SetRules() is the same as calling addRules() with the exception that it
  243. * clears the rules before adding them.
  244. *
  245. * @param array $rules
  246. * @return Zend_Filter_Inflector
  247. */
  248. public function setRules(Array $rules)
  249. {
  250. $this->clearRules();
  251. $this->addRules($rules);
  252. return $this;
  253. }
  254. /**
  255. * AddRules(): multi-call to setting filter rules.
  256. *
  257. * If prefixed with a ":" (colon), a filter rule will be added. If not
  258. * prefixed, a static replacement will be added.
  259. *
  260. * ex:
  261. * array(
  262. * ':controller' => array('CamelCaseToUnderscore','StringToLower'),
  263. * ':action' => array('CamelCaseToUnderscore','StringToLower'),
  264. * 'suffix' => 'phtml'
  265. * );
  266. *
  267. * @param array
  268. * @return Zend_Filter_Inflector
  269. */
  270. public function addRules(Array $rules)
  271. {
  272. $keys = array_keys($rules);
  273. foreach ($keys as $spec) {
  274. if ($spec[0] == ':') {
  275. $this->addFilterRule($spec, $rules[$spec]);
  276. } else {
  277. $this->setStaticRule($spec, $rules[$spec]);
  278. }
  279. }
  280. return $this;
  281. }
  282. /**
  283. * Get rules
  284. *
  285. * By default, returns all rules. If a $spec is provided, will return those
  286. * rules if found, false otherwise.
  287. *
  288. * @param string $spec
  289. * @return array|false
  290. */
  291. public function getRules($spec = null)
  292. {
  293. if (null !== $spec) {
  294. $spec = $this->_normalizeSpec($spec);
  295. if (isset($this->_rules[$spec])) {
  296. return $this->_rules[$spec];
  297. }
  298. return false;
  299. }
  300. return $this->_rules;
  301. }
  302. /**
  303. * getRule() returns a rule set by setFilterRule(), a numeric index must be provided
  304. *
  305. * @param string $spec
  306. * @param int $index
  307. * @return Zend_Filter_Interface|false
  308. */
  309. public function getRule($spec, $index)
  310. {
  311. $spec = $this->_normalizeSpec($spec);
  312. if (isset($this->_rules[$spec]) && is_array($this->_rules[$spec])) {
  313. if (isset($this->_rules[$spec][$index])) {
  314. return $this->_rules[$spec][$index];
  315. }
  316. }
  317. return false;
  318. }
  319. /**
  320. * ClearRules() clears the rules currently in the inflector
  321. *
  322. * @return Zend_Filter_Inflector
  323. */
  324. public function clearRules()
  325. {
  326. $this->_rules = array();
  327. return $this;
  328. }
  329. /**
  330. * Set a filtering rule for a spec. $ruleSet can be a string, Filter object
  331. * or an array of strings or filter objects.
  332. *
  333. * @param string $spec
  334. * @param array|string|Zend_Filter_Interface $ruleSet
  335. * @return Zend_Filter_Inflector
  336. */
  337. public function setFilterRule($spec, $ruleSet)
  338. {
  339. $spec = $this->_normalizeSpec($spec);
  340. $this->_rules[$spec] = array();
  341. return $this->addFilterRule($spec, $ruleSet);
  342. }
  343. /**
  344. * Add a filter rule for a spec
  345. *
  346. * @param mixed $spec
  347. * @param mixed $ruleSet
  348. * @return void
  349. */
  350. public function addFilterRule($spec, $ruleSet)
  351. {
  352. $spec = $this->_normalizeSpec($spec);
  353. if (!isset($this->_rules[$spec])) {
  354. $this->_rules[$spec] = array();
  355. }
  356. if (!is_array($ruleSet)) {
  357. $ruleSet = array($ruleSet);
  358. }
  359. if (is_string($this->_rules[$spec])) {
  360. $temp = $this->_rules[$spec];
  361. $this->_rules[$spec] = array();
  362. $this->_rules[$spec][] = $temp;
  363. }
  364. foreach ($ruleSet as $rule) {
  365. $this->_rules[$spec][] = $this->_getRule($rule);
  366. }
  367. return $this;
  368. }
  369. /**
  370. * Set a static rule for a spec. This is a single string value
  371. *
  372. * @param string $name
  373. * @param string $value
  374. * @return Zend_Filter_Inflector
  375. */
  376. public function setStaticRule($name, $value)
  377. {
  378. $name = $this->_normalizeSpec($name);
  379. $this->_rules[$name] = (string) $value;
  380. return $this;
  381. }
  382. /**
  383. * Set Static Rule Reference.
  384. *
  385. * This allows a consuming class to pass a property or variable
  386. * in to be referenced when its time to build the output string from the
  387. * target.
  388. *
  389. * @param string $name
  390. * @param mixed $reference
  391. * @return Zend_Filter_Inflector
  392. */
  393. public function setStaticRuleReference($name, &$reference)
  394. {
  395. $name = $this->_normalizeSpec($name);
  396. $this->_rules[$name] =& $reference;
  397. return $this;
  398. }
  399. /**
  400. * Inflect
  401. *
  402. * @param string|array $source
  403. * @return string
  404. */
  405. public function filter($source)
  406. {
  407. // clean source
  408. foreach ( (array) $source as $sourceName => $sourceValue) {
  409. $source[ltrim($sourceName, ':')] = $sourceValue;
  410. }
  411. $pregQuotedTargetReplacementIdentifier = preg_quote($this->_targetReplacementIdentifier, '#');
  412. $processedParts = array();
  413. foreach ($this->_rules as $ruleName => $ruleValue) {
  414. if (isset($source[$ruleName])) {
  415. if (is_string($ruleValue)) {
  416. // overriding the set rule
  417. $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $source[$ruleName]);
  418. } elseif (is_array($ruleValue)) {
  419. $processedPart = $source[$ruleName];
  420. foreach ($ruleValue as $ruleFilter) {
  421. $processedPart = $ruleFilter->filter($processedPart);
  422. }
  423. $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $processedPart);
  424. }
  425. } elseif (is_string($ruleValue)) {
  426. $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $ruleValue);
  427. }
  428. }
  429. // all of the values of processedParts would have been str_replace('\\', '\\\\', ..)'d to disable preg_replace backreferences
  430. $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->_target);
  431. if ($this->_throwTargetExceptionsOn && (preg_match('#(?='.$pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true)) {
  432. require_once 'Zend/Filter/Exception.php';
  433. 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);
  434. }
  435. return $inflectedTarget;
  436. }
  437. /**
  438. * Normalize spec string
  439. *
  440. * @param string $spec
  441. * @return string
  442. */
  443. protected function _normalizeSpec($spec)
  444. {
  445. return ltrim((string) $spec, ':&');
  446. }
  447. /**
  448. * Resolve named filters and convert them to filter objects.
  449. *
  450. * @param string $rule
  451. * @return Zend_Filter_Interface
  452. */
  453. protected function _getRule($rule)
  454. {
  455. if ($rule instanceof Zend_Filter_Interface) {
  456. return $rule;
  457. }
  458. $rule = (string) $rule;
  459. $className = $this->getPluginLoader()->load($rule);
  460. $ruleObject = new $className();
  461. if (!$ruleObject instanceof Zend_Filter_Interface) {
  462. require_once 'Zend/Filter/Exception.php';
  463. throw new Zend_Filter_Exception('No class named ' . $rule . ' implementing Zend_Filter_Interface could be found');
  464. }
  465. return $ruleObject;
  466. }
  467. }