PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/ZendFramework-2.2.7/library/Zend/Validator/NotEmpty.php

https://gitlab.com/jeann2015/nexus
PHP | 267 lines | 229 code | 17 blank | 21 comment | 37 complexity | 910f06463a15cc8dc3a6ca48b545c049 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-2014 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. * @var array
  46. */
  47. protected $messageTemplates = array(
  48. self::IS_EMPTY => "Value is required and can't be empty",
  49. self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
  50. );
  51. /**
  52. * Options for this validator
  53. *
  54. * @var array
  55. */
  56. protected $options = array(
  57. 'type' => 493, // Internal type to detect
  58. );
  59. /**
  60. * Constructor
  61. *
  62. * @param array|Traversable|int $options OPTIONAL
  63. */
  64. public function __construct($options = null)
  65. {
  66. if ($options instanceof Traversable) {
  67. $options = ArrayUtils::iteratorToArray($options);
  68. }
  69. if (!is_array($options)) {
  70. $options = func_get_args();
  71. $temp = array();
  72. if (!empty($options)) {
  73. $temp['type'] = array_shift($options);
  74. }
  75. $options = $temp;
  76. }
  77. if (is_array($options)) {
  78. if (!array_key_exists('type', $options)) {
  79. $detected = 0;
  80. $found = false;
  81. foreach ($options as $option) {
  82. if (in_array($option, $this->constants, true)) {
  83. $found = true;
  84. $detected += array_search($option, $this->constants);
  85. }
  86. }
  87. if ($found) {
  88. $options['type'] = $detected;
  89. }
  90. }
  91. }
  92. parent::__construct($options);
  93. }
  94. /**
  95. * Returns the set types
  96. *
  97. * @return array
  98. */
  99. public function getType()
  100. {
  101. return $this->options['type'];
  102. }
  103. /**
  104. * Set the types
  105. *
  106. * @param int|array $type
  107. * @throws Exception\InvalidArgumentException
  108. * @return NotEmpty
  109. */
  110. public function setType($type = null)
  111. {
  112. if (is_array($type)) {
  113. $detected = 0;
  114. foreach ($type as $value) {
  115. if (is_int($value)) {
  116. $detected |= $value;
  117. } elseif (in_array($value, $this->constants)) {
  118. $detected |= array_search($value, $this->constants);
  119. }
  120. }
  121. $type = $detected;
  122. } elseif (is_string($type) && in_array($type, $this->constants)) {
  123. $type = array_search($type, $this->constants);
  124. }
  125. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  126. throw new Exception\InvalidArgumentException('Unknown type');
  127. }
  128. $this->options['type'] = $type;
  129. return $this;
  130. }
  131. /**
  132. * Returns true if and only if $value is not an empty value.
  133. *
  134. * @param string $value
  135. * @return bool
  136. */
  137. public function isValid($value)
  138. {
  139. if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
  140. !is_bool($value) && !is_array($value) && !is_object($value)
  141. ) {
  142. $this->error(self::INVALID);
  143. return false;
  144. }
  145. $type = $this->getType();
  146. $this->setValue($value);
  147. $object = false;
  148. // OBJECT_COUNT (countable object)
  149. if ($type & self::OBJECT_COUNT) {
  150. $object = true;
  151. if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
  152. $this->error(self::IS_EMPTY);
  153. return false;
  154. }
  155. }
  156. // OBJECT_STRING (object's toString)
  157. if ($type & self::OBJECT_STRING) {
  158. $object = true;
  159. if ((is_object($value) && (!method_exists($value, '__toString'))) ||
  160. (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
  161. $this->error(self::IS_EMPTY);
  162. return false;
  163. }
  164. }
  165. // OBJECT (object)
  166. if ($type & self::OBJECT) {
  167. // fall trough, objects are always not empty
  168. } elseif ($object === false) {
  169. // object not allowed but object given -> return false
  170. if (is_object($value)) {
  171. $this->error(self::IS_EMPTY);
  172. return false;
  173. }
  174. }
  175. // SPACE (' ')
  176. if ($type & self::SPACE) {
  177. if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
  178. $this->error(self::IS_EMPTY);
  179. return false;
  180. }
  181. }
  182. // NULL (null)
  183. if ($type & self::NULL) {
  184. if ($value === null) {
  185. $this->error(self::IS_EMPTY);
  186. return false;
  187. }
  188. }
  189. // EMPTY_ARRAY (array())
  190. if ($type & self::EMPTY_ARRAY) {
  191. if (is_array($value) && ($value == array())) {
  192. $this->error(self::IS_EMPTY);
  193. return false;
  194. }
  195. }
  196. // ZERO ('0')
  197. if ($type & self::ZERO) {
  198. if (is_string($value) && ($value == '0')) {
  199. $this->error(self::IS_EMPTY);
  200. return false;
  201. }
  202. }
  203. // STRING ('')
  204. if ($type & self::STRING) {
  205. if (is_string($value) && ($value == '')) {
  206. $this->error(self::IS_EMPTY);
  207. return false;
  208. }
  209. }
  210. // FLOAT (0.0)
  211. if ($type & self::FLOAT) {
  212. if (is_float($value) && ($value == 0.0)) {
  213. $this->error(self::IS_EMPTY);
  214. return false;
  215. }
  216. }
  217. // INTEGER (0)
  218. if ($type & self::INTEGER) {
  219. if (is_int($value) && ($value == 0)) {
  220. $this->error(self::IS_EMPTY);
  221. return false;
  222. }
  223. }
  224. // BOOLEAN (false)
  225. if ($type & self::BOOLEAN) {
  226. if (is_bool($value) && ($value == false)) {
  227. $this->error(self::IS_EMPTY);
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. }