diff options
Diffstat (limited to 'libs/Zend/Validate/NotEmpty.php')
-rw-r--r-- | libs/Zend/Validate/NotEmpty.php | 190 |
1 files changed, 174 insertions, 16 deletions
diff --git a/libs/Zend/Validate/NotEmpty.php b/libs/Zend/Validate/NotEmpty.php index 304cf4fe70..e370459cf9 100644 --- a/libs/Zend/Validate/NotEmpty.php +++ b/libs/Zend/Validate/NotEmpty.php @@ -14,27 +14,51 @@ * * @category Zend * @package Zend_Validate - * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @version $Id: NotEmpty.php 18186 2009-09-17 18:57:00Z matthew $ + * @version $Id: NotEmpty.php 20193 2010-01-11 10:40:43Z thomas $ */ /** * @see Zend_Validate_Abstract */ -require_once 'Zend/Validate/Abstract.php'; +// require_once 'Zend/Validate/Abstract.php'; /** * @category Zend * @package Zend_Validate - * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Zend_Validate_NotEmpty extends Zend_Validate_Abstract { + const BOOLEAN = 1; + const INTEGER = 2; + const FLOAT = 4; + const STRING = 8; + const ZERO = 16; + const EMPTY_ARRAY = 32; + const NULL = 64; + const PHP = 127; + const SPACE = 128; + const ALL = 255; + const INVALID = 'notEmptyInvalid'; const IS_EMPTY = 'isEmpty'; + protected $_constants = array( + self::BOOLEAN => 'boolean', + self::INTEGER => 'integer', + self::FLOAT => 'float', + self::STRING => 'string', + self::ZERO => 'zero', + self::EMPTY_ARRAY => 'array', + self::NULL => 'null', + self::PHP => 'php', + self::SPACE => 'space', + self::ALL => 'all' + ); + /** * @var array */ @@ -44,6 +68,80 @@ class Zend_Validate_NotEmpty extends Zend_Validate_Abstract ); /** + * Internal type to detect + * + * @var integer + */ + protected $_type = 237; + + /** + * Constructor + * + * @param string|array|Zend_Config $options OPTIONAL + */ + public function __construct($options = null) + { + if ($options instanceof Zend_Config) { + $options = $options->toArray(); + } else if (!is_array($options)) { + $options = func_get_args(); + $temp = array(); + if (!empty($options)) { + $temp['type'] = array_shift($options); + } + + $options = $temp; + } + + if (is_array($options) && array_key_exists('type', $options)) { + $this->setType($options['type']); + } + } + + /** + * Returns the set types + * + * @return array + */ + public function getType() + { + return $this->_type; + } + + /** + * Set the types + * + * @param integer|array $type + * @throws Zend_Validate_Exception + * @return Zend_Validate_NotEmpty + */ + public function setType($type = null) + { + if (is_array($type)) { + $detected = 0; + foreach($type as $value) { + if (is_int($value)) { + $detected += $value; + } else if (in_array($value, $this->_constants)) { + $detected += array_search($value, $this->_constants); + } + } + + $type = $detected; + } else if (is_string($type) && in_array($type, $this->_constants)) { + $type = array_search($type, $this->_constants); + } + + if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { + // require_once 'Zend/Validate/Exception.php'; + throw new Zend_Validate_Exception('Unknown type'); + } + + $this->_type = $type; + return $this; + } + + /** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is not an empty value. @@ -59,21 +157,81 @@ class Zend_Validate_NotEmpty extends Zend_Validate_Abstract return false; } + $type = $this->getType(); $this->_setValue($value); - if (is_string($value) - && (('' === $value) - || preg_match('/^\s+$/s', $value)) - ) { - $this->_error(self::IS_EMPTY); - return false; - } elseif (is_int($value) && (0 === $value)) { - return true; - } elseif (!is_string($value) && empty($value)) { - $this->_error(self::IS_EMPTY); - return false; + + // SPACE (' ') + if ($type >= self::SPACE) { + $type -= self::SPACE; + if (is_string($value) && (preg_match('/^\s+$/s', $value))) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // NULL (null) + if ($type >= self::NULL) { + $type -= self::NULL; + if (is_null($value)) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // EMPTY_ARRAY (array()) + if ($type >= self::EMPTY_ARRAY) { + $type -= self::EMPTY_ARRAY; + if (is_array($value) && ($value == array())) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // ZERO ('0') + if ($type >= self::ZERO) { + $type -= self::ZERO; + if (is_string($value) && ($value == '0')) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // STRING ('') + if ($type >= self::STRING) { + $type -= self::STRING; + if (is_string($value) && ($value == '')) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // FLOAT (0.0) + if ($type >= self::FLOAT) { + $type -= self::FLOAT; + if (is_float($value) && ($value == 0.0)) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // INTEGER (0) + if ($type >= self::INTEGER) { + $type -= self::INTEGER; + if (is_int($value) && ($value == 0)) { + $this->_error(self::IS_EMPTY); + return false; + } + } + + // BOOLEAN (false) + if ($type >= self::BOOLEAN) { + $type -= self::BOOLEAN; + if (is_bool($value) && ($value == false)) { + $this->_error(self::IS_EMPTY); + return false; + } } return true; } - } |