PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/Validate/NotEmpty.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 237 lines | 201 code | 5 blank | 31 comment | 0 complexity | edd9e35070441657cbd335408a5618a3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. * @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-2010 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 ALL = 255;
  43. const INVALID = 'notEmptyInvalid';
  44. const IS_EMPTY = 'isEmpty';
  45. protected $_constants = array(
  46. self::BOOLEAN => 'boolean',
  47. self::INTEGER => 'integer',
  48. self::FLOAT => 'float',
  49. self::STRING => 'string',
  50. self::ZERO => 'zero',
  51. self::EMPTY_ARRAY => 'array',
  52. self::NULL => 'null',
  53. self::PHP => 'php',
  54. self::SPACE => 'space',
  55. self::ALL => 'all'
  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, value should be float, string, array, boolean or integer",
  63. );
  64. /**
  65. * Internal type to detect
  66. *
  67. * @var integer
  68. */
  69. protected $_type = 237;
  70. /**
  71. * Constructor
  72. *
  73. * @param string|array|Zend_Config $options OPTIONAL
  74. */
  75. public function __construct($options = null)
  76. {
  77. if ($options instanceof Zend_Config) {
  78. $options = $options->toArray();
  79. } else if (!is_array($options)) {
  80. $options = func_get_args();
  81. $temp = array();
  82. if (!empty($options)) {
  83. $temp['type'] = array_shift($options);
  84. }
  85. $options = $temp;
  86. }
  87. if (is_array($options) && array_key_exists('type', $options)) {
  88. $this->setType($options['type']);
  89. }
  90. }
  91. /**
  92. * Returns the set types
  93. *
  94. * @return array
  95. */
  96. public function getType()
  97. {
  98. return $this->_type;
  99. }
  100. /**
  101. * Set the types
  102. *
  103. * @param integer|array $type
  104. * @throws Zend_Validate_Exception
  105. * @return Zend_Validate_NotEmpty
  106. */
  107. public function setType($type = null)
  108. {
  109. if (is_array($type)) {
  110. $detected = 0;
  111. foreach($type as $value) {
  112. if (is_int($value)) {
  113. $detected += $value;
  114. } else if (in_array($value, $this->_constants)) {
  115. $detected += array_search($value, $this->_constants);
  116. }
  117. }
  118. $type = $detected;
  119. } else if (is_string($type) && in_array($type, $this->_constants)) {
  120. $type = array_search($type, $this->_constants);
  121. }
  122. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  123. require_once 'Zend/Validate/Exception.php';
  124. throw new Zend_Validate_Exception('Unknown type');
  125. }
  126. $this->_type = $type;
  127. return $this;
  128. }
  129. /**
  130. * Defined by Zend_Validate_Interface
  131. *
  132. * Returns true if and only if $value is not an empty value.
  133. *
  134. * @param string $value
  135. * @return boolean
  136. */
  137. public function isValid($value)
  138. {
  139. if (!is_null($value) && !is_string($value) && !is_int($value) && !is_float($value) &&
  140. !is_bool($value) && !is_array($value)) {
  141. $this->_error(self::INVALID);
  142. return false;
  143. }
  144. $type = $this->getType();
  145. $this->_setValue($value);
  146. // SPACE (' ')
  147. if ($type >= self::SPACE) {
  148. $type -= self::SPACE;
  149. if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
  150. $this->_error(self::IS_EMPTY);
  151. return false;
  152. }
  153. }
  154. // NULL (null)
  155. if ($type >= self::NULL) {
  156. $type -= self::NULL;
  157. if (is_null($value)) {
  158. $this->_error(self::IS_EMPTY);
  159. return false;
  160. }
  161. }
  162. // EMPTY_ARRAY (array())
  163. if ($type >= self::EMPTY_ARRAY) {
  164. $type -= self::EMPTY_ARRAY;
  165. if (is_array($value) && ($value == array())) {
  166. $this->_error(self::IS_EMPTY);
  167. return false;
  168. }
  169. }
  170. // ZERO ('0')
  171. if ($type >= self::ZERO) {
  172. $type -= self::ZERO;
  173. if (is_string($value) && ($value == '0')) {
  174. $this->_error(self::IS_EMPTY);
  175. return false;
  176. }
  177. }
  178. // STRING ('')
  179. if ($type >= self::STRING) {
  180. $type -= self::STRING;
  181. if (is_string($value) && ($value == '')) {
  182. $this->_error(self::IS_EMPTY);
  183. return false;
  184. }
  185. }
  186. // FLOAT (0.0)
  187. if ($type >= self::FLOAT) {
  188. $type -= self::FLOAT;
  189. if (is_float($value) && ($value == 0.0)) {
  190. $this->_error(self::IS_EMPTY);
  191. return false;
  192. }
  193. }
  194. // INTEGER (0)
  195. if ($type >= self::INTEGER) {
  196. $type -= self::INTEGER;
  197. if (is_int($value) && ($value == 0)) {
  198. $this->_error(self::IS_EMPTY);
  199. return false;
  200. }
  201. }
  202. // BOOLEAN (false)
  203. if ($type >= self::BOOLEAN) {
  204. $type -= self::BOOLEAN;
  205. if (is_bool($value) && ($value == false)) {
  206. $this->_error(self::IS_EMPTY);
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. }