source: sandbox/expressoAgenda/library/Zend/Validate/Callback.php @ 4456

Revision 4456, 4.4 KB checked in by airton, 13 years ago (diff)

Ticket #1991 - Parametrizacao das buscas LDAP no Expresso Mail - Adicionando arquivos e bibliotecas

  • Property svn:executable set to *
Line 
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-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19 * @version    $Id: Callback.php 22668 2010-07-25 14:50:46Z thomas $
20 */
21
22/**
23 * @see Zend_Validate_Abstract
24 */
25require_once 'Zend/Validate/Abstract.php';
26
27/**
28 * @category   Zend
29 * @package    Zend_Validate
30 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
32 */
33class Zend_Validate_Callback extends Zend_Validate_Abstract
34{
35    /**
36     * Invalid callback
37     */
38    const INVALID_CALLBACK = 'callbackInvalid';
39
40    /**
41     * Invalid value
42     */
43    const INVALID_VALUE = 'callbackValue';
44
45    /**
46     * Validation failure message template definitions
47     *
48     * @var array
49     */
50    protected $_messageTemplates = array(
51        self::INVALID_VALUE    => "'%value%' is not valid",
52        self::INVALID_CALLBACK => "An exception has been raised within the callback",
53    );
54
55    /**
56     * Callback in a call_user_func format
57     *
58     * @var string|array
59     */
60    protected $_callback = null;
61
62    /**
63     * Default options to set for the filter
64     *
65     * @var mixed
66     */
67    protected $_options = array();
68
69    /**
70     * Sets validator options
71     *
72     * @param  string|array $callback
73     * @param  mixed   $max
74     * @param  boolean $inclusive
75     * @return void
76     */
77    public function __construct($callback = null)
78    {
79        if (is_callable($callback)) {
80            $this->setCallback($callback);
81        } elseif (is_array($callback)) {
82            if (isset($callback['callback'])) {
83                $this->setCallback($callback['callback']);
84            }
85            if (isset($callback['options'])) {
86                $this->setOptions($callback['options']);
87            }
88        }
89
90        if (null === ($initializedCallack = $this->getCallback())) {
91            require_once 'Zend/Validate/Exception.php';
92            throw new Zend_Validate_Exception('No callback registered');
93        }
94    }
95
96    /**
97     * Returns the set callback
98     *
99     * @return mixed
100     */
101    public function getCallback()
102    {
103        return $this->_callback;
104    }
105
106    /**
107     * Sets the callback
108     *
109     * @param  string|array $callback
110     * @return Zend_Validate_Callback Provides a fluent interface
111     */
112    public function setCallback($callback)
113    {
114        if (!is_callable($callback)) {
115            require_once 'Zend/Validate/Exception.php';
116            throw new Zend_Validate_Exception('Invalid callback given');
117        }
118        $this->_callback = $callback;
119        return $this;
120    }
121
122    /**
123     * Returns the set options for the callback
124     *
125     * @return mixed
126     */
127    public function getOptions()
128    {
129        return $this->_options;
130    }
131
132    /**
133     * Sets options for the callback
134     *
135     * @param  mixed $max
136     * @return Zend_Validate_Callback Provides a fluent interface
137     */
138    public function setOptions($options)
139    {
140        $this->_options = (array) $options;
141        return $this;
142    }
143
144    /**
145     * Defined by Zend_Validate_Interface
146     *
147     * Returns true if and only if the set callback returns
148     * for the provided $value
149     *
150     * @param  mixed $value
151     * @return boolean
152     */
153    public function isValid($value)
154    {
155        $this->_setValue($value);
156
157        $options  = $this->getOptions();
158        $callback = $this->getCallback();
159        $args     = func_get_args();
160        $options  = array_merge($args, $options);
161
162        try {
163            if (!call_user_func_array($callback, $options)) {
164                $this->_error(self::INVALID_VALUE);
165                return false;
166            }
167        } catch (Exception $e) {
168            $this->_error(self::INVALID_CALLBACK);
169            return false;
170        }
171
172        return true;
173    }
174}
Note: See TracBrowser for help on using the repository browser.