Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Validator / NotEmpty.php
blob99da975c8a1c6ca7a61c0d49b55aea5193e35e97
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Validator;
12 use Traversable;
13 use Zend\Stdlib\ArrayUtils;
15 class NotEmpty extends AbstractValidator
17 const BOOLEAN = 1;
18 const INTEGER = 2;
19 const FLOAT = 4;
20 const STRING = 8;
21 const ZERO = 16;
22 const EMPTY_ARRAY = 32;
23 const NULL = 64;
24 const PHP = 127;
25 const SPACE = 128;
26 const OBJECT = 256;
27 const OBJECT_STRING = 512;
28 const OBJECT_COUNT = 1024;
29 const ALL = 2047;
31 const INVALID = 'notEmptyInvalid';
32 const IS_EMPTY = 'isEmpty';
34 protected $constants = array(
35 self::BOOLEAN => 'boolean',
36 self::INTEGER => 'integer',
37 self::FLOAT => 'float',
38 self::STRING => 'string',
39 self::ZERO => 'zero',
40 self::EMPTY_ARRAY => 'array',
41 self::NULL => 'null',
42 self::PHP => 'php',
43 self::SPACE => 'space',
44 self::OBJECT => 'object',
45 self::OBJECT_STRING => 'objectstring',
46 self::OBJECT_COUNT => 'objectcount',
47 self::ALL => 'all',
50 /**
51 * @var array
53 protected $messageTemplates = array(
54 self::IS_EMPTY => "Value is required and can't be empty",
55 self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
58 /**
59 * Options for this validator
61 * @var array
63 protected $options = array(
64 'type' => 493, // Internal type to detect
67 /**
68 * Constructor
70 * @param array|Traversable|int $options OPTIONAL
72 public function __construct($options = null)
74 if ($options instanceof Traversable) {
75 $options = ArrayUtils::iteratorToArray($options);
78 if (!is_array($options)) {
79 $options = func_get_args();
80 $temp = array();
81 if (!empty($options)) {
82 $temp['type'] = array_shift($options);
85 $options = $temp;
88 if (is_array($options)) {
89 if (!array_key_exists('type', $options)) {
90 $detected = 0;
91 $found = false;
92 foreach ($options as $option) {
93 if (in_array($option, $this->constants, true)) {
94 $found = true;
95 $detected += array_search($option, $this->constants);
99 if ($found) {
100 $options['type'] = $detected;
105 parent::__construct($options);
109 * Returns the set types
111 * @return array
113 public function getType()
115 return $this->options['type'];
119 * Set the types
121 * @param int|array $type
122 * @throws Exception\InvalidArgumentException
123 * @return NotEmpty
125 public function setType($type = null)
127 if (is_array($type)) {
128 $detected = 0;
129 foreach ($type as $value) {
130 if (is_int($value)) {
131 $detected += $value;
132 } elseif (in_array($value, $this->constants)) {
133 $detected += array_search($value, $this->constants);
137 $type = $detected;
138 } elseif (is_string($type) && in_array($type, $this->constants)) {
139 $type = array_search($type, $this->constants);
142 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
143 throw new Exception\InvalidArgumentException('Unknown type');
146 $this->options['type'] = $type;
147 return $this;
151 * Returns true if and only if $value is not an empty value.
153 * @param string $value
154 * @return bool
156 public function isValid($value)
158 if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
159 !is_bool($value) && !is_array($value) && !is_object($value)
161 $this->error(self::INVALID);
162 return false;
165 $type = $this->getType();
166 $this->setValue($value);
167 $object = false;
169 // OBJECT_COUNT (countable object)
170 if ($type >= self::OBJECT_COUNT) {
171 $type -= self::OBJECT_COUNT;
172 $object = true;
174 if (is_object($value) && ($value instanceof \Countable) && (count($value) == 0)) {
175 $this->error(self::IS_EMPTY);
176 return false;
180 // OBJECT_STRING (object's toString)
181 if ($type >= self::OBJECT_STRING) {
182 $type -= self::OBJECT_STRING;
183 $object = true;
185 if ((is_object($value) && (!method_exists($value, '__toString'))) ||
186 (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
187 $this->error(self::IS_EMPTY);
188 return false;
192 // OBJECT (object)
193 if ($type >= self::OBJECT) {
194 $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;
204 // SPACE (' ')
205 if ($type >= self::SPACE) {
206 $type -= self::SPACE;
207 if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
208 $this->error(self::IS_EMPTY);
209 return false;
213 // NULL (null)
214 if ($type >= self::NULL) {
215 $type -= self::NULL;
216 if ($value === null) {
217 $this->error(self::IS_EMPTY);
218 return false;
222 // EMPTY_ARRAY (array())
223 if ($type >= self::EMPTY_ARRAY) {
224 $type -= self::EMPTY_ARRAY;
225 if (is_array($value) && ($value == array())) {
226 $this->error(self::IS_EMPTY);
227 return false;
231 // ZERO ('0')
232 if ($type >= self::ZERO) {
233 $type -= self::ZERO;
234 if (is_string($value) && ($value == '0')) {
235 $this->error(self::IS_EMPTY);
236 return false;
240 // STRING ('')
241 if ($type >= self::STRING) {
242 $type -= self::STRING;
243 if (is_string($value) && ($value == '')) {
244 $this->error(self::IS_EMPTY);
245 return false;
249 // FLOAT (0.0)
250 if ($type >= self::FLOAT) {
251 $type -= self::FLOAT;
252 if (is_float($value) && ($value == 0.0)) {
253 $this->error(self::IS_EMPTY);
254 return false;
258 // INTEGER (0)
259 if ($type >= self::INTEGER) {
260 $type -= self::INTEGER;
261 if (is_int($value) && ($value == 0)) {
262 $this->error(self::IS_EMPTY);
263 return false;
267 // BOOLEAN (false)
268 if ($type >= self::BOOLEAN) {
269 $type -= self::BOOLEAN;
270 if (is_bool($value) && ($value == false)) {
271 $this->error(self::IS_EMPTY);
272 return false;
276 return true;