File indexing completed on 2024-05-12 06:03:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_NotEmpty extends Zend_Validate_Abstract
0034 {
0035     const BOOLEAN       = 1;
0036     const INTEGER       = 2;
0037     const FLOAT         = 4;
0038     const STRING        = 8;
0039     const ZERO          = 16;
0040     const EMPTY_ARRAY   = 32;
0041     const NULL          = 64;
0042     const PHP           = 127;
0043     const SPACE         = 128;
0044     const OBJECT        = 256;
0045     const OBJECT_STRING = 512;
0046     const OBJECT_COUNT  = 1024;
0047     const ALL           = 2047;
0048 
0049     const INVALID  = 'notEmptyInvalid';
0050     const IS_EMPTY = 'isEmpty';
0051 
0052     protected $_constants = array(
0053         self::BOOLEAN       => 'boolean',
0054         self::INTEGER       => 'integer',
0055         self::FLOAT         => 'float',
0056         self::STRING        => 'string',
0057         self::ZERO          => 'zero',
0058         self::EMPTY_ARRAY   => 'array',
0059         self::NULL          => 'null',
0060         self::PHP           => 'php',
0061         self::SPACE         => 'space',
0062         self::OBJECT        => 'object',
0063         self::OBJECT_STRING => 'objectstring',
0064         self::OBJECT_COUNT  => 'objectcount',
0065         self::ALL           => 'all',
0066     );
0067 
0068     /**
0069      * @var array
0070      */
0071     protected $_messageTemplates = array(
0072         self::IS_EMPTY => "Value is required and can't be empty",
0073         self::INVALID  => "Invalid type given. String, integer, float, boolean or array expected",
0074     );
0075 
0076     /**
0077      * Internal type to detect
0078      *
0079      * @var integer
0080      */
0081     protected $_type = 493;
0082 
0083     /**
0084      * Constructor
0085      *
0086      * @param string|array|Zend_Config $options OPTIONAL
0087      */
0088     public function __construct($options = null)
0089     {
0090         if ($options instanceof Zend_Config) {
0091             $options = $options->toArray();
0092         } else if (!is_array($options)) {
0093             $options = func_get_args();
0094             $temp    = array();
0095             if (!empty($options)) {
0096                 $temp['type'] = array_shift($options);
0097             }
0098 
0099             $options = $temp;
0100         }
0101 
0102         if (is_array($options) && array_key_exists('type', $options)) {
0103             $this->setType($options['type']);
0104         }
0105     }
0106 
0107     /**
0108      * Returns the set types
0109      *
0110      * @return array
0111      */
0112     public function getType()
0113     {
0114         return $this->_type;
0115     }
0116 
0117     /**
0118      * Set the types
0119      *
0120      * @param  integer|array $type
0121      * @throws Zend_Validate_Exception
0122      * @return Zend_Validate_NotEmpty
0123      */
0124     public function setType($type = null)
0125     {
0126         if (is_array($type)) {
0127             $detected = 0;
0128             foreach($type as $value) {
0129                 if (is_int($value)) {
0130                     $detected += $value;
0131                 } else if (in_array($value, $this->_constants)) {
0132                     $detected += array_search($value, $this->_constants);
0133                 }
0134             }
0135 
0136             $type = $detected;
0137         } else if (is_string($type) && in_array($type, $this->_constants)) {
0138             $type = array_search($type, $this->_constants);
0139         }
0140 
0141         if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
0142             // require_once 'Zend/Validate/Exception.php';
0143             throw new Zend_Validate_Exception('Unknown type');
0144         }
0145 
0146         $this->_type = $type;
0147         return $this;
0148     }
0149 
0150     /**
0151      * Defined by Zend_Validate_Interface
0152      *
0153      * Returns true if and only if $value is not an empty value.
0154      *
0155      * @param  string $value
0156      * @return boolean
0157      */
0158     public function isValid($value)
0159     {
0160         if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
0161             !is_bool($value) && !is_array($value) && !is_object($value)) {
0162             $this->_error(self::INVALID);
0163             return false;
0164         }
0165 
0166         $type    = $this->getType();
0167         $this->_setValue($value);
0168         $object  = false;
0169 
0170         // OBJECT_COUNT (countable object)
0171         if ($type >= self::OBJECT_COUNT) {
0172             $type -= self::OBJECT_COUNT;
0173             $object = true;
0174 
0175             if (is_object($value) && ($value instanceof Countable) && (count($value) == 0)) {
0176                 $this->_error(self::IS_EMPTY);
0177                 return false;
0178             }
0179         }
0180 
0181         // OBJECT_STRING (object's toString)
0182         if ($type >= self::OBJECT_STRING) {
0183             $type -= self::OBJECT_STRING;
0184             $object = true;
0185 
0186             if ((is_object($value) && (!method_exists($value, '__toString'))) ||
0187                 (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
0188                 $this->_error(self::IS_EMPTY);
0189                 return false;
0190             }
0191         }
0192 
0193         // OBJECT (object)
0194         if ($type >= self::OBJECT) {
0195             $type -= self::OBJECT;
0196             // fall trough, objects are always not empty
0197         } else if ($object === false) {
0198             // object not allowed but object given -> return false
0199             if (is_object($value)) {
0200                 $this->_error(self::IS_EMPTY);
0201                 return false;
0202             }
0203         }
0204 
0205         // SPACE ('   ')
0206         if ($type >= self::SPACE) {
0207             $type -= self::SPACE;
0208             if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
0209                 $this->_error(self::IS_EMPTY);
0210                 return false;
0211             }
0212         }
0213 
0214         // NULL (null)
0215         if ($type >= self::NULL) {
0216             $type -= self::NULL;
0217             if ($value === null) {
0218                 $this->_error(self::IS_EMPTY);
0219                 return false;
0220             }
0221         }
0222 
0223         // EMPTY_ARRAY (array())
0224         if ($type >= self::EMPTY_ARRAY) {
0225             $type -= self::EMPTY_ARRAY;
0226             if (is_array($value) && ($value == array())) {
0227                 $this->_error(self::IS_EMPTY);
0228                 return false;
0229             }
0230         }
0231 
0232         // ZERO ('0')
0233         if ($type >= self::ZERO) {
0234             $type -= self::ZERO;
0235             if (is_string($value) && ($value == '0')) {
0236                 $this->_error(self::IS_EMPTY);
0237                 return false;
0238             }
0239         }
0240 
0241         // STRING ('')
0242         if ($type >= self::STRING) {
0243             $type -= self::STRING;
0244             if (is_string($value) && ($value == '')) {
0245                 $this->_error(self::IS_EMPTY);
0246                 return false;
0247             }
0248         }
0249 
0250         // FLOAT (0.0)
0251         if ($type >= self::FLOAT) {
0252             $type -= self::FLOAT;
0253             if (is_float($value) && ($value == 0.0)) {
0254                 $this->_error(self::IS_EMPTY);
0255                 return false;
0256             }
0257         }
0258 
0259         // INTEGER (0)
0260         if ($type >= self::INTEGER) {
0261             $type -= self::INTEGER;
0262             if (is_int($value) && ($value == 0)) {
0263                 $this->_error(self::IS_EMPTY);
0264                 return false;
0265             }
0266         }
0267 
0268         // BOOLEAN (false)
0269         if ($type >= self::BOOLEAN) {
0270             $type -= self::BOOLEAN;
0271             if (is_bool($value) && ($value == false)) {
0272                 $this->_error(self::IS_EMPTY);
0273                 return false;
0274             }
0275         }
0276 
0277         return true;
0278     }
0279 }