Callback.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-2012 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-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_Validate_Callback extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Invalid callback
  35. */
  36. const INVALID_CALLBACK = 'callbackInvalid';
  37. /**
  38. * Invalid value
  39. */
  40. const INVALID_VALUE = 'callbackValue';
  41. /**
  42. * Validation failure message template definitions
  43. *
  44. * @var array
  45. */
  46. protected $_messageTemplates = array(
  47. self::INVALID_VALUE => "'%value%' is not valid",
  48. self::INVALID_CALLBACK => "An exception has been raised within the callback",
  49. );
  50. /**
  51. * Callback in a call_user_func format
  52. *
  53. * @var string|array
  54. */
  55. protected $_callback = null;
  56. /**
  57. * Default options to set for the filter
  58. *
  59. * @var mixed
  60. */
  61. protected $_options = array();
  62. /**
  63. * Sets validator options
  64. *
  65. * @param string|array $callback
  66. * @param mixed $max
  67. * @param boolean $inclusive
  68. * @return void
  69. */
  70. public function __construct($callback = null)
  71. {
  72. if (is_callable($callback)) {
  73. $this->setCallback($callback);
  74. } elseif (is_array($callback)) {
  75. if (isset($callback['callback'])) {
  76. $this->setCallback($callback['callback']);
  77. }
  78. if (isset($callback['options'])) {
  79. $this->setOptions($callback['options']);
  80. }
  81. }
  82. if (null === ($initializedCallack = $this->getCallback())) {
  83. require_once 'Zend/Validate/Exception.php';
  84. throw new Zend_Validate_Exception('No callback registered');
  85. }
  86. }
  87. /**
  88. * Returns the set callback
  89. *
  90. * @return mixed
  91. */
  92. public function getCallback()
  93. {
  94. return $this->_callback;
  95. }
  96. /**
  97. * Sets the callback
  98. *
  99. * @param string|array $callback
  100. * @return Zend_Validate_Callback Provides a fluent interface
  101. */
  102. public function setCallback($callback)
  103. {
  104. if (!is_callable($callback)) {
  105. require_once 'Zend/Validate/Exception.php';
  106. throw new Zend_Validate_Exception('Invalid callback given');
  107. }
  108. $this->_callback = $callback;
  109. return $this;
  110. }
  111. /**
  112. * Returns the set options for the callback
  113. *
  114. * @return mixed
  115. */
  116. public function getOptions()
  117. {
  118. return $this->_options;
  119. }
  120. /**
  121. * Sets options for the callback
  122. *
  123. * @param mixed $max
  124. * @return Zend_Validate_Callback Provides a fluent interface
  125. */
  126. public function setOptions($options)
  127. {
  128. $this->_options = (array) $options;
  129. return $this;
  130. }
  131. /**
  132. * Defined by Zend_Validate_Interface
  133. *
  134. * Returns true if and only if the set callback returns
  135. * for the provided $value
  136. *
  137. * @param mixed $value
  138. * @return boolean
  139. */
  140. public function isValid($value)
  141. {
  142. $this->_setValue($value);
  143. $options = $this->getOptions();
  144. $callback = $this->getCallback();
  145. $args = func_get_args();
  146. $options = array_merge($args, $options);
  147. try {
  148. if (!call_user_func_array($callback, $options)) {
  149. $this->_error(self::INVALID_VALUE);
  150. return false;
  151. }
  152. } catch (Exception $e) {
  153. $this->_error(self::INVALID_CALLBACK);
  154. return false;
  155. }
  156. return true;
  157. }
  158. }