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
10 namespace Zend\Validator
;
13 use Zend\Stdlib\ArrayUtils
;
15 class NotEmpty
extends AbstractValidator
22 const EMPTY_ARRAY
= 32;
27 const OBJECT_STRING
= 512;
28 const OBJECT_COUNT
= 1024;
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',
40 self
::EMPTY_ARRAY
=> 'array',
43 self
::SPACE
=> 'space',
44 self
::OBJECT => 'object',
45 self
::OBJECT_STRING
=> 'objectstring',
46 self
::OBJECT_COUNT
=> 'objectcount',
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",
59 * Options for this validator
63 protected $options = array(
64 'type' => 493, // Internal type to detect
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();
81 if (!empty($options)) {
82 $temp['type'] = array_shift($options);
88 if (is_array($options)) {
89 if (!array_key_exists('type', $options)) {
92 foreach ($options as $option) {
93 if (in_array($option, $this->constants
, true)) {
95 $detected +
= array_search($option, $this->constants
);
100 $options['type'] = $detected;
105 parent
::__construct($options);
109 * Returns the set types
113 public function getType()
115 return $this->options
['type'];
121 * @param int|array $type
122 * @throws Exception\InvalidArgumentException
125 public function setType($type = null)
127 if (is_array($type)) {
129 foreach ($type as $value) {
130 if (is_int($value)) {
132 } elseif (in_array($value, $this->constants
)) {
133 $detected +
= array_search($value, $this->constants
);
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;
151 * Returns true if and only if $value is not an empty value.
153 * @param string $value
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
);
165 $type = $this->getType();
166 $this->setValue($value);
169 // OBJECT_COUNT (countable object)
170 if ($type >= self
::OBJECT_COUNT
) {
171 $type -= self
::OBJECT_COUNT
;
174 if (is_object($value) && ($value instanceof \Countable
) && (count($value) == 0)) {
175 $this->error(self
::IS_EMPTY
);
180 // OBJECT_STRING (object's toString)
181 if ($type >= self
::OBJECT_STRING
) {
182 $type -= self
::OBJECT_STRING
;
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
);
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
);
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
);
214 if ($type >= self
::NULL) {
216 if ($value === null) {
217 $this->error(self
::IS_EMPTY
);
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
);
232 if ($type >= self
::ZERO
) {
234 if (is_string($value) && ($value == '0')) {
235 $this->error(self
::IS_EMPTY
);
241 if ($type >= self
::STRING) {
242 $type -= self
::STRING;
243 if (is_string($value) && ($value == '')) {
244 $this->error(self
::IS_EMPTY
);
250 if ($type >= self
::FLOAT) {
251 $type -= self
::FLOAT;
252 if (is_float($value) && ($value == 0.0)) {
253 $this->error(self
::IS_EMPTY
);
259 if ($type >= self
::INTEGER) {
260 $type -= self
::INTEGER;
261 if (is_int($value) && ($value == 0)) {
262 $this->error(self
::IS_EMPTY
);
268 if ($type >= self
::BOOLEAN
) {
269 $type -= self
::BOOLEAN
;
270 if (is_bool($value) && ($value == false)) {
271 $this->error(self
::IS_EMPTY
);