Regex.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Regex extends Zend_Validate_Abstract
  32. {
  33. const NOT_MATCH = 'regexNotMatch';
  34. /**
  35. * @var array
  36. */
  37. protected $_messageTemplates = array(
  38. self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'"
  39. );
  40. /**
  41. * @var array
  42. */
  43. protected $_messageVariables = array(
  44. 'pattern' => '_pattern'
  45. );
  46. /**
  47. * Regular expression pattern
  48. *
  49. * @var string
  50. */
  51. protected $_pattern;
  52. /**
  53. * Sets validator options
  54. *
  55. * @param string $pattern
  56. * @return void
  57. */
  58. public function __construct($pattern)
  59. {
  60. $this->setPattern($pattern);
  61. }
  62. /**
  63. * Returns the pattern option
  64. *
  65. * @return string
  66. */
  67. public function getPattern()
  68. {
  69. return $this->_pattern;
  70. }
  71. /**
  72. * Sets the pattern option
  73. *
  74. * @param string $pattern
  75. * @return Zend_Validate_Regex Provides a fluent interface
  76. */
  77. public function setPattern($pattern)
  78. {
  79. $this->_pattern = (string) $pattern;
  80. return $this;
  81. }
  82. /**
  83. * Defined by Zend_Validate_Interface
  84. *
  85. * Returns true if and only if $value matches against the pattern option
  86. *
  87. * @param string $value
  88. * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
  89. * @return boolean
  90. */
  91. public function isValid($value)
  92. {
  93. $valueString = (string) $value;
  94. $this->_setValue($valueString);
  95. $status = @preg_match($this->_pattern, $valueString);
  96. if (false === $status) {
  97. /**
  98. * @see Zend_Validate_Exception
  99. */
  100. require_once 'Zend/Validate/Exception.php';
  101. throw new Zend_Validate_Exception("Internal error matching pattern '$this->_pattern' against value '$valueString'");
  102. }
  103. if (!$status) {
  104. $this->_error();
  105. return false;
  106. }
  107. return true;
  108. }
  109. }