PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Validator/NotEmpty.php

http://github.com/zendframework/zf2
PHP | 302 lines | 258 code | 18 blank | 26 comment | 37 complexity | 4d20089715ce6345785160afc9c06aaa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Validator;
  10. use Traversable;
  11. use Zend\Stdlib\ArrayUtils;
  12. class NotEmpty extends AbstractValidator
  13. {
  14. const BOOLEAN = 0x001;
  15. const INTEGER = 0x002;
  16. const FLOAT = 0x004;
  17. const STRING = 0x008;
  18. const ZERO = 0x010;
  19. const EMPTY_ARRAY = 0x020;
  20. const NULL = 0x040;
  21. const PHP = 0x07F;
  22. const SPACE = 0x080;
  23. const OBJECT = 0x100;
  24. const OBJECT_STRING = 0x200;
  25. const OBJECT_COUNT = 0x400;
  26. const ALL = 0x7FF;
  27. const INVALID = 'notEmptyInvalid';
  28. const IS_EMPTY = 'isEmpty';
  29. protected $constants = array(
  30. self::BOOLEAN => 'boolean',
  31. self::INTEGER => 'integer',
  32. self::FLOAT => 'float',
  33. self::STRING => 'string',
  34. self::ZERO => 'zero',
  35. self::EMPTY_ARRAY => 'array',
  36. self::NULL => 'null',
  37. self::PHP => 'php',
  38. self::SPACE => 'space',
  39. self::OBJECT => 'object',
  40. self::OBJECT_STRING => 'objectstring',
  41. self::OBJECT_COUNT => 'objectcount',
  42. self::ALL => 'all',
  43. );
  44. /**
  45. * Default value for types; value = 493
  46. *
  47. * @var array
  48. */
  49. protected $defaultType = array(
  50. self::OBJECT,
  51. self::SPACE,
  52. self::NULL,
  53. self::EMPTY_ARRAY,
  54. self::STRING,
  55. self::FLOAT,
  56. self::BOOLEAN
  57. );
  58. /**
  59. * @var array
  60. */
  61. protected $messageTemplates = array(
  62. self::IS_EMPTY => "Value is required and can't be empty",
  63. self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
  64. );
  65. /**
  66. * Options for this validator
  67. *
  68. * @var array
  69. */
  70. protected $options = array();
  71. /**
  72. * Constructor
  73. *
  74. * @param array|Traversable|int $options OPTIONAL
  75. */
  76. public function __construct($options = null)
  77. {
  78. $this->setType($this->defaultType);
  79. if ($options instanceof Traversable) {
  80. $options = ArrayUtils::iteratorToArray($options);
  81. }
  82. if (!is_array($options)) {
  83. $options = func_get_args();
  84. $temp = array();
  85. if (!empty($options)) {
  86. $temp['type'] = array_shift($options);
  87. }
  88. $options = $temp;
  89. }
  90. if (is_array($options)) {
  91. if (!array_key_exists('type', $options)) {
  92. $detected = 0;
  93. $found = false;
  94. foreach ($options as $option) {
  95. if (in_array($option, $this->constants, true)) {
  96. $found = true;
  97. $detected += array_search($option, $this->constants);
  98. }
  99. }
  100. if ($found) {
  101. $options['type'] = $detected;
  102. }
  103. }
  104. }
  105. parent::__construct($options);
  106. }
  107. /**
  108. * Returns the set types
  109. *
  110. * @return array
  111. */
  112. public function getType()
  113. {
  114. return $this->options['type'];
  115. }
  116. /**
  117. * @return int
  118. */
  119. public function getDefaultType()
  120. {
  121. return $this->calculateTypeValue($this->defaultType);
  122. }
  123. /**
  124. * @param array|int|string $type
  125. * @return int
  126. */
  127. protected function calculateTypeValue($type)
  128. {
  129. if (is_array($type)) {
  130. $detected = 0;
  131. foreach ($type as $value) {
  132. if (is_int($value)) {
  133. $detected |= $value;
  134. } elseif (in_array($value, $this->constants)) {
  135. $detected |= array_search($value, $this->constants);
  136. }
  137. }
  138. $type = $detected;
  139. } elseif (is_string($type) && in_array($type, $this->constants)) {
  140. $type = array_search($type, $this->constants);
  141. }
  142. return $type;
  143. }
  144. /**
  145. * Set the types
  146. *
  147. * @param int|array $type
  148. * @throws Exception\InvalidArgumentException
  149. * @return NotEmpty
  150. */
  151. public function setType($type = null)
  152. {
  153. $type = $this->calculateTypeValue($type);
  154. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  155. throw new Exception\InvalidArgumentException('Unknown type');
  156. }
  157. $this->options['type'] = $type;
  158. return $this;
  159. }
  160. /**
  161. * Returns true if and only if $value is not an empty value.
  162. *
  163. * @param string $value
  164. * @return bool
  165. */
  166. public function isValid($value)
  167. {
  168. if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
  169. !is_bool($value) && !is_array($value) && !is_object($value)
  170. ) {
  171. $this->error(self::INVALID);
  172. return false;
  173. }
  174. $type = $this->getType();
  175. $this->setValue($value);
  176. $object = false;
  177. // OBJECT_COUNT (countable object)
  178. if ($type & self::OBJECT_COUNT) {
  179. $object = true;
  180. if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
  181. $this->error(self::IS_EMPTY);
  182. return false;
  183. }
  184. }
  185. // OBJECT_STRING (object's toString)
  186. if ($type & self::OBJECT_STRING) {
  187. $object = true;
  188. if ((is_object($value) && (!method_exists($value, '__toString'))) ||
  189. (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
  190. $this->error(self::IS_EMPTY);
  191. return false;
  192. }
  193. }
  194. // OBJECT (object)
  195. if ($type & self::OBJECT) {
  196. // fall trough, objects are always not empty
  197. } elseif ($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. // SPACE (' ')
  205. if ($type & self::SPACE) {
  206. if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
  207. $this->error(self::IS_EMPTY);
  208. return false;
  209. }
  210. }
  211. // NULL (null)
  212. if ($type & self::NULL) {
  213. if ($value === null) {
  214. $this->error(self::IS_EMPTY);
  215. return false;
  216. }
  217. }
  218. // EMPTY_ARRAY (array())
  219. if ($type & self::EMPTY_ARRAY) {
  220. if (is_array($value) && ($value == array())) {
  221. $this->error(self::IS_EMPTY);
  222. return false;
  223. }
  224. }
  225. // ZERO ('0')
  226. if ($type & self::ZERO) {
  227. if (is_string($value) && ($value == '0')) {
  228. $this->error(self::IS_EMPTY);
  229. return false;
  230. }
  231. }
  232. // STRING ('')
  233. if ($type & self::STRING) {
  234. if (is_string($value) && ($value == '')) {
  235. $this->error(self::IS_EMPTY);
  236. return false;
  237. }
  238. }
  239. // FLOAT (0.0)
  240. if ($type & self::FLOAT) {
  241. if (is_float($value) && ($value == 0.0)) {
  242. $this->error(self::IS_EMPTY);
  243. return false;
  244. }
  245. }
  246. // INTEGER (0)
  247. if ($type & self::INTEGER) {
  248. if (is_int($value) && ($value == 0)) {
  249. $this->error(self::IS_EMPTY);
  250. return false;
  251. }
  252. }
  253. // BOOLEAN (false)
  254. if ($type & self::BOOLEAN) {
  255. if (is_bool($value) && ($value == false)) {
  256. $this->error(self::IS_EMPTY);
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. }