/library/Zend/Filter/Word/SeparatorToSeparator.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 137 lines · 44 code · 15 blank · 78 comment · 2 complexity · 1e3640918a30f2c05ad7d64eac8fb8fe 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: SeparatorToSeparator.php 24593 2012-01-05 20:35:02Z matthew $
  20. */
  21. /**
  22. * @see Zend_Filter_PregReplace
  23. */
  24. require_once 'Zend/Filter/PregReplace.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2012 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_Word_SeparatorToSeparator extends Zend_Filter_PregReplace
  32. {
  33. protected $_searchSeparator = null;
  34. protected $_replacementSeparator = null;
  35. /**
  36. * Constructor
  37. *
  38. * @param string $searchSeparator Seperator to search for
  39. * @param string $replacementSeperator Seperator to replace with
  40. *
  41. * @return void
  42. */
  43. public function __construct ($searchSeparator = ' ', $replacementSeparator = '-')
  44. {
  45. $this->setSearchSeparator($searchSeparator);
  46. $this->setReplacementSeparator($replacementSeparator);
  47. }
  48. /**
  49. * Sets a new seperator to search for
  50. *
  51. * @param string $separator Seperator to search for
  52. *
  53. * @return $this
  54. */
  55. public function setSearchSeparator ($separator)
  56. {
  57. $this->_searchSeparator = $separator;
  58. return $this;
  59. }
  60. /**
  61. * Returns the actual set seperator to search for
  62. *
  63. * @return string
  64. */
  65. public function getSearchSeparator ()
  66. {
  67. return $this->_searchSeparator;
  68. }
  69. /**
  70. * Sets a new seperator which replaces the searched one
  71. *
  72. * @param string $separator Seperator which replaces the searched one
  73. *
  74. * @return $this
  75. */
  76. public function setReplacementSeparator ($separator)
  77. {
  78. $this->_replacementSeparator = $separator;
  79. return $this;
  80. }
  81. /**
  82. * Returns the actual set seperator which replaces the searched one
  83. *
  84. * @return string
  85. */
  86. public function getReplacementSeparator ()
  87. {
  88. return $this->_replacementSeparator;
  89. }
  90. /**
  91. * Defined by Zend_Filter_Interface
  92. *
  93. * Returns the string $value, replacing the searched seperators with the defined ones
  94. *
  95. * @param string $value
  96. *
  97. * @return string
  98. */
  99. public function filter ($value)
  100. {
  101. return $this->_separatorToSeparatorFilter($value);
  102. }
  103. /**
  104. * Do the real work, replaces the seperator to search for with the replacement seperator
  105. *
  106. * Returns the replaced string
  107. *
  108. * @param string $value
  109. *
  110. * @return string
  111. */
  112. protected function _separatorToSeparatorFilter ($value)
  113. {
  114. if ($this->_searchSeparator == null) {
  115. require_once 'Zend/Filter/Exception.php';
  116. throw new Zend_Filter_Exception( 'You must provide a search separator for this filter to work.' );
  117. }
  118. $this->setMatchPattern('#' . preg_quote($this->_searchSeparator, '#') . '#');
  119. $this->setReplacement($this->_replacementSeparator);
  120. return parent::filter($value);
  121. }
  122. }