/library/Zend/Validator/NotEmpty.php

https://github.com/Exercise/zf2 · PHP · 281 lines · 221 code · 16 blank · 44 comment · 39 complexity · ddfd65f2400228277687c29a20a5fe48 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$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Validator;
  25. /**
  26. * @uses \Zend\Validator\AbstractValidator
  27. * @uses \Zend\Validator\Exception
  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. */
  33. class NotEmpty extends AbstractValidator
  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. const INVALID = 'notEmptyInvalid';
  49. const IS_EMPTY = 'isEmpty';
  50. protected $_constants = array(
  51. self::BOOLEAN => 'boolean',
  52. self::INTEGER => 'integer',
  53. self::FLOAT => 'float',
  54. self::STRING => 'string',
  55. self::ZERO => 'zero',
  56. self::EMPTY_ARRAY => 'array',
  57. self::NULL => 'null',
  58. self::PHP => 'php',
  59. self::SPACE => 'space',
  60. self::OBJECT => 'object',
  61. self::OBJECT_STRING => 'objectstring',
  62. self::OBJECT_COUNT => 'objectcount',
  63. self::ALL => 'all',
  64. );
  65. /**
  66. * @var array
  67. */
  68. protected $_messageTemplates = array(
  69. self::IS_EMPTY => "Value is required and can't be empty",
  70. self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
  71. );
  72. /**
  73. * Internal type to detect
  74. *
  75. * @var integer
  76. */
  77. protected $_type = 493;
  78. /**
  79. * Constructor
  80. *
  81. * @param string|array|\Zend\Config\Config $options OPTIONAL
  82. */
  83. public function __construct($options = null)
  84. {
  85. if ($options instanceof \Zend\Config\Config) {
  86. $options = $options->toArray();
  87. } else if (!is_array($options)) {
  88. $options = func_get_args();
  89. $temp = array();
  90. if (!empty($options)) {
  91. $temp['type'] = array_shift($options);
  92. }
  93. $options = $temp;
  94. }
  95. if (is_array($options) && array_key_exists('type', $options)) {
  96. $this->setType($options['type']);
  97. }
  98. }
  99. /**
  100. * Returns the set types
  101. *
  102. * @return array
  103. */
  104. public function getType()
  105. {
  106. return $this->_type;
  107. }
  108. /**
  109. * Set the types
  110. *
  111. * @param integer|array $type
  112. * @throws \Zend\Validator\Exception
  113. * @return \Zend\Validator\NotEmpty
  114. */
  115. public function setType($type = null)
  116. {
  117. if (is_array($type)) {
  118. $detected = 0;
  119. foreach($type as $value) {
  120. if (is_int($value)) {
  121. $detected += $value;
  122. } else if (in_array($value, $this->_constants)) {
  123. $detected += array_search($value, $this->_constants);
  124. }
  125. }
  126. $type = $detected;
  127. } else if (is_string($type) && in_array($type, $this->_constants)) {
  128. $type = array_search($type, $this->_constants);
  129. }
  130. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  131. throw new Exception('Unknown type');
  132. }
  133. $this->_type = $type;
  134. return $this;
  135. }
  136. /**
  137. * Defined by Zend_Validate_Interface
  138. *
  139. * Returns true if and only if $value is not an empty value.
  140. *
  141. * @param string $value
  142. * @return boolean
  143. */
  144. public function isValid($value)
  145. {
  146. if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
  147. !is_bool($value) && !is_array($value) && !is_object($value)
  148. ) {
  149. $this->_error(self::INVALID);
  150. return false;
  151. }
  152. $type = $this->getType();
  153. $this->_setValue($value);
  154. $object = false;
  155. // OBJECT_COUNT (countable object)
  156. if ($type >= self::OBJECT_COUNT) {
  157. $type -= self::OBJECT_COUNT;
  158. $object = true;
  159. if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
  160. $this->_error(self::IS_EMPTY);
  161. return false;
  162. }
  163. }
  164. // OBJECT_STRING (object's toString)
  165. if ($type >= self::OBJECT_STRING) {
  166. $type -= self::OBJECT_STRING;
  167. $object = true;
  168. if ((is_object($value) && (!method_exists($value, '__toString'))) ||
  169. (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
  170. $this->_error(self::IS_EMPTY);
  171. return false;
  172. }
  173. }
  174. // OBJECT (object)
  175. if ($type >= self::OBJECT) {
  176. $type -= self::OBJECT;
  177. // fall trough, objects are always not empty
  178. } else if ($object === false) {
  179. // object not allowed but object given -> return false
  180. if (is_object($value)) {
  181. $this->_error(self::IS_EMPTY);
  182. return false;
  183. }
  184. }
  185. // SPACE (' ')
  186. if ($type >= self::SPACE) {
  187. $type -= self::SPACE;
  188. if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
  189. $this->_error(self::IS_EMPTY);
  190. return false;
  191. }
  192. }
  193. // NULL (null)
  194. if ($type >= self::NULL) {
  195. $type -= self::NULL;
  196. if ($value === null) {
  197. $this->_error(self::IS_EMPTY);
  198. return false;
  199. }
  200. }
  201. // EMPTY_ARRAY (array())
  202. if ($type >= self::EMPTY_ARRAY) {
  203. $type -= self::EMPTY_ARRAY;
  204. if (is_array($value) && ($value == array())) {
  205. $this->_error(self::IS_EMPTY);
  206. return false;
  207. }
  208. }
  209. // ZERO ('0')
  210. if ($type >= self::ZERO) {
  211. $type -= self::ZERO;
  212. if (is_string($value) && ($value == '0')) {
  213. $this->_error(self::IS_EMPTY);
  214. return false;
  215. }
  216. }
  217. // STRING ('')
  218. if ($type >= self::STRING) {
  219. $type -= self::STRING;
  220. if (is_string($value) && ($value == '')) {
  221. $this->_error(self::IS_EMPTY);
  222. return false;
  223. }
  224. }
  225. // FLOAT (0.0)
  226. if ($type >= self::FLOAT) {
  227. $type -= self::FLOAT;
  228. if (is_float($value) && ($value == 0.0)) {
  229. $this->_error(self::IS_EMPTY);
  230. return false;
  231. }
  232. }
  233. // INTEGER (0)
  234. if ($type >= self::INTEGER) {
  235. $type -= self::INTEGER;
  236. if (is_int($value) && ($value == 0)) {
  237. $this->_error(self::IS_EMPTY);
  238. return false;
  239. }
  240. }
  241. // BOOLEAN (false)
  242. if ($type >= self::BOOLEAN) {
  243. $type -= self::BOOLEAN;
  244. if (is_bool($value) && ($value == false)) {
  245. $this->_error(self::IS_EMPTY);
  246. return false;
  247. }
  248. }
  249. return true;
  250. }
  251. }