/Zend/Validate/NotEmpty.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 284 lines · 222 code · 17 blank · 45 comment · 39 complexity · 60884e3f462475c96eeacf8ce261f75c MD5 · raw file

  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. * @namespace
  23. */
  24. namespace Zend\Validate;
  25. /**
  26. * @see Zend_Validate_Abstract
  27. */
  28. require_once 'Zend/Validate/Abstract.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class NotEmpty extends AbstractValidate
  36. {
  37. const BOOLEAN = 1;
  38. const INTEGER = 2;
  39. const FLOAT = 4;
  40. const STRING = 8;
  41. const ZERO = 16;
  42. const EMPTY_ARRAY = 32;
  43. const NULL = 64;
  44. const PHP = 127;
  45. const SPACE = 128;
  46. const OBJECT = 256;
  47. const OBJECT_STRING = 512;
  48. const OBJECT_COUNT = 1024;
  49. const ALL = 2047;
  50. const INVALID = 'notEmptyInvalid';
  51. const IS_EMPTY = 'isEmpty';
  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. * @var array
  69. */
  70. protected $_messageTemplates = array(
  71. self::IS_EMPTY => "Value is required and can't be empty",
  72. self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
  73. );
  74. /**
  75. * Internal type to detect
  76. *
  77. * @var integer
  78. */
  79. protected $_type = 493;
  80. /**
  81. * Constructor
  82. *
  83. * @param string|array|Zend_Config $options OPTIONAL
  84. */
  85. public function __construct($options = null)
  86. {
  87. if ($options instanceof \Zend\Config\Config) {
  88. $options = $options->toArray();
  89. } else if (!is_array($options)) {
  90. $options = func_get_args();
  91. $temp = array();
  92. if (!empty($options)) {
  93. $temp['type'] = array_shift($options);
  94. }
  95. $options = $temp;
  96. }
  97. if (is_array($options) && array_key_exists('type', $options)) {
  98. $this->setType($options['type']);
  99. }
  100. }
  101. /**
  102. * Returns the set types
  103. *
  104. * @return array
  105. */
  106. public function getType()
  107. {
  108. return $this->_type;
  109. }
  110. /**
  111. * Set the types
  112. *
  113. * @param integer|array $type
  114. * @throws Zend_Validate_Exception
  115. * @return Zend_Validate_NotEmpty
  116. */
  117. public function setType($type = null)
  118. {
  119. if (is_array($type)) {
  120. $detected = 0;
  121. foreach($type as $value) {
  122. if (is_int($value)) {
  123. $detected += $value;
  124. } else if (in_array($value, $this->_constants)) {
  125. $detected += array_search($value, $this->_constants);
  126. }
  127. }
  128. $type = $detected;
  129. } else if (is_string($type) && in_array($type, $this->_constants)) {
  130. $type = array_search($type, $this->_constants);
  131. }
  132. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  133. require_once 'Zend/Validate/Exception.php';
  134. throw new Exception('Unknown type');
  135. }
  136. $this->_type = $type;
  137. return $this;
  138. }
  139. /**
  140. * Defined by Zend_Validate_Interface
  141. *
  142. * Returns true if and only if $value is not an empty value.
  143. *
  144. * @param string $value
  145. * @return boolean
  146. */
  147. public function isValid($value)
  148. {
  149. if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
  150. !is_bool($value) && !is_array($value) && !is_object($value)) {
  151. $this->_error(self::INVALID);
  152. return false;
  153. }
  154. $type = $this->getType();
  155. $this->_setValue($value);
  156. $object = false;
  157. // OBJECT_COUNT (countable object)
  158. if ($type >= self::OBJECT_COUNT) {
  159. $type -= self::OBJECT_COUNT;
  160. $object = true;
  161. if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
  162. $this->_error(self::IS_EMPTY);
  163. return false;
  164. }
  165. }
  166. // OBJECT_STRING (object's toString)
  167. if ($type >= self::OBJECT_STRING) {
  168. $type -= self::OBJECT_STRING;
  169. $object = true;
  170. if ((is_object($value) && (!method_exists($value, '__toString'))) ||
  171. (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
  172. $this->_error(self::IS_EMPTY);
  173. return false;
  174. }
  175. }
  176. // OBJECT (object)
  177. if ($type >= self::OBJECT) {
  178. $type -= self::OBJECT;
  179. // fall trough, objects are always not empty
  180. } else if ($object === false) {
  181. // object not allowed but object given -> return false
  182. if (is_object($value)) {
  183. $this->_error(self::IS_EMPTY);
  184. return false;
  185. }
  186. }
  187. // SPACE (' ')
  188. if ($type >= self::SPACE) {
  189. $type -= self::SPACE;
  190. if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
  191. $this->_error(self::IS_EMPTY);
  192. return false;
  193. }
  194. }
  195. // NULL (null)
  196. if ($type >= self::NULL) {
  197. $type -= self::NULL;
  198. if ($value === null) {
  199. $this->_error(self::IS_EMPTY);
  200. return false;
  201. }
  202. }
  203. // EMPTY_ARRAY (array())
  204. if ($type >= self::EMPTY_ARRAY) {
  205. $type -= self::EMPTY_ARRAY;
  206. if (is_array($value) && ($value == array())) {
  207. $this->_error(self::IS_EMPTY);
  208. return false;
  209. }
  210. }
  211. // ZERO ('0')
  212. if ($type >= self::ZERO) {
  213. $type -= self::ZERO;
  214. if (is_string($value) && ($value == '0')) {
  215. $this->_error(self::IS_EMPTY);
  216. return false;
  217. }
  218. }
  219. // STRING ('')
  220. if ($type >= self::STRING) {
  221. $type -= self::STRING;
  222. if (is_string($value) && ($value == '')) {
  223. $this->_error(self::IS_EMPTY);
  224. return false;
  225. }
  226. }
  227. // FLOAT (0.0)
  228. if ($type >= self::FLOAT) {
  229. $type -= self::FLOAT;
  230. if (is_float($value) && ($value == 0.0)) {
  231. $this->_error(self::IS_EMPTY);
  232. return false;
  233. }
  234. }
  235. // INTEGER (0)
  236. if ($type >= self::INTEGER) {
  237. $type -= self::INTEGER;
  238. if (is_int($value) && ($value == 0)) {
  239. $this->_error(self::IS_EMPTY);
  240. return false;
  241. }
  242. }
  243. // BOOLEAN (false)
  244. if ($type >= self::BOOLEAN) {
  245. $type -= self::BOOLEAN;
  246. if (is_bool($value) && ($value == false)) {
  247. $this->_error(self::IS_EMPTY);
  248. return false;
  249. }
  250. }
  251. return true;
  252. }
  253. }