source: trunk/library/Zend/Validate/NotEmpty.php @ 5146

Revision 5146, 7.9 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Library: adicionando arquivos.

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: NotEmpty.php 22691 2010-07-26 19:29:14Z 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_NotEmpty extends Zend_Validate_Abstract
34{
35    const BOOLEAN       = 1;
36    const INTEGER       = 2;
37    const FLOAT         = 4;
38    const STRING        = 8;
39    const ZERO          = 16;
40    const EMPTY_ARRAY   = 32;
41    const NULL          = 64;
42    const PHP           = 127;
43    const SPACE         = 128;
44    const OBJECT        = 256;
45    const OBJECT_STRING = 512;
46    const OBJECT_COUNT  = 1024;
47    const ALL           = 2047;
48
49    const INVALID  = 'notEmptyInvalid';
50    const IS_EMPTY = 'isEmpty';
51
52    protected $_constants = array(
53        self::BOOLEAN       => 'boolean',
54        self::INTEGER       => 'integer',
55        self::FLOAT         => 'float',
56        self::STRING        => 'string',
57        self::ZERO          => 'zero',
58        self::EMPTY_ARRAY   => 'array',
59        self::NULL          => 'null',
60        self::PHP           => 'php',
61        self::SPACE         => 'space',
62        self::OBJECT        => 'object',
63        self::OBJECT_STRING => 'objectstring',
64        self::OBJECT_COUNT  => 'objectcount',
65        self::ALL           => 'all',
66    );
67
68    /**
69     * @var array
70     */
71    protected $_messageTemplates = array(
72        self::IS_EMPTY => "Value is required and can't be empty",
73        self::INVALID  => "Invalid type given. String, integer, float, boolean or array expected",
74    );
75
76    /**
77     * Internal type to detect
78     *
79     * @var integer
80     */
81    protected $_type = 493;
82
83    /**
84     * Constructor
85     *
86     * @param string|array|Zend_Config $options OPTIONAL
87     */
88    public function __construct($options = null)
89    {
90        if ($options instanceof Zend_Config) {
91            $options = $options->toArray();
92        } else if (!is_array($options)) {
93            $options = func_get_args();
94            $temp    = array();
95            if (!empty($options)) {
96                $temp['type'] = array_shift($options);
97            }
98
99            $options = $temp;
100        }
101
102        if (is_array($options) && array_key_exists('type', $options)) {
103            $this->setType($options['type']);
104        }
105    }
106
107    /**
108     * Returns the set types
109     *
110     * @return array
111     */
112    public function getType()
113    {
114        return $this->_type;
115    }
116
117    /**
118     * Set the types
119     *
120     * @param  integer|array $type
121     * @throws Zend_Validate_Exception
122     * @return Zend_Validate_NotEmpty
123     */
124    public function setType($type = null)
125    {
126        if (is_array($type)) {
127            $detected = 0;
128            foreach($type as $value) {
129                if (is_int($value)) {
130                    $detected += $value;
131                } else if (in_array($value, $this->_constants)) {
132                    $detected += array_search($value, $this->_constants);
133                }
134            }
135
136            $type = $detected;
137        } else if (is_string($type) && in_array($type, $this->_constants)) {
138            $type = array_search($type, $this->_constants);
139        }
140
141        if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
142            require_once 'Zend/Validate/Exception.php';
143            throw new Zend_Validate_Exception('Unknown type');
144        }
145
146        $this->_type = $type;
147        return $this;
148    }
149
150    /**
151     * Defined by Zend_Validate_Interface
152     *
153     * Returns true if and only if $value is not an empty value.
154     *
155     * @param  string $value
156     * @return boolean
157     */
158    public function isValid($value)
159    {
160        if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
161            !is_bool($value) && !is_array($value) && !is_object($value)) {
162            $this->_error(self::INVALID);
163            return false;
164        }
165
166        $type    = $this->getType();
167        $this->_setValue($value);
168        $object  = false;
169
170        // OBJECT_COUNT (countable object)
171        if ($type >= self::OBJECT_COUNT) {
172            $type -= self::OBJECT_COUNT;
173            $object = true;
174
175            if (is_object($value) && ($value instanceof Countable) && (count($value) == 0)) {
176                $this->_error(self::IS_EMPTY);
177                return false;
178            }
179        }
180
181        // OBJECT_STRING (object's toString)
182        if ($type >= self::OBJECT_STRING) {
183            $type -= self::OBJECT_STRING;
184            $object = true;
185
186            if ((is_object($value) && (!method_exists($value, '__toString'))) ||
187                (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
188                $this->_error(self::IS_EMPTY);
189                return false;
190            }
191        }
192
193        // OBJECT (object)
194        if ($type >= self::OBJECT) {
195            $type -= self::OBJECT;
196            // fall trough, objects are always not empty
197        } else if ($object === false) {
198            // object not allowed but object given -> return false
199            if (is_object($value)) {
200                $this->_error(self::IS_EMPTY);
201                return false;
202            }
203        }
204
205        // SPACE ('   ')
206        if ($type >= self::SPACE) {
207            $type -= self::SPACE;
208            if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
209                $this->_error(self::IS_EMPTY);
210                return false;
211            }
212        }
213
214        // NULL (null)
215        if ($type >= self::NULL) {
216            $type -= self::NULL;
217            if ($value === null) {
218                $this->_error(self::IS_EMPTY);
219                return false;
220            }
221        }
222
223        // EMPTY_ARRAY (array())
224        if ($type >= self::EMPTY_ARRAY) {
225            $type -= self::EMPTY_ARRAY;
226            if (is_array($value) && ($value == array())) {
227                $this->_error(self::IS_EMPTY);
228                return false;
229            }
230        }
231
232        // ZERO ('0')
233        if ($type >= self::ZERO) {
234            $type -= self::ZERO;
235            if (is_string($value) && ($value == '0')) {
236                $this->_error(self::IS_EMPTY);
237                return false;
238            }
239        }
240
241        // STRING ('')
242        if ($type >= self::STRING) {
243            $type -= self::STRING;
244            if (is_string($value) && ($value == '')) {
245                $this->_error(self::IS_EMPTY);
246                return false;
247            }
248        }
249
250        // FLOAT (0.0)
251        if ($type >= self::FLOAT) {
252            $type -= self::FLOAT;
253            if (is_float($value) && ($value == 0.0)) {
254                $this->_error(self::IS_EMPTY);
255                return false;
256            }
257        }
258
259        // INTEGER (0)
260        if ($type >= self::INTEGER) {
261            $type -= self::INTEGER;
262            if (is_int($value) && ($value == 0)) {
263                $this->_error(self::IS_EMPTY);
264                return false;
265            }
266        }
267
268        // BOOLEAN (false)
269        if ($type >= self::BOOLEAN) {
270            $type -= self::BOOLEAN;
271            if (is_bool($value) && ($value == false)) {
272                $this->_error(self::IS_EMPTY);
273                return false;
274            }
275        }
276
277        return true;
278    }
279}
Note: See TracBrowser for help on using the repository browser.