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

/concreteOLD/libraries/3rdparty/Zend/Validate/NotEmpty.php

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