PageRenderTime 93ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-validator/src/NotEmpty.php

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