-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNotEmptyValidator.php
61 lines (54 loc) · 1.47 KB
/
NotEmptyValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php declare(strict_types=1);
namespace MyENA\RGW\Validator;
use MyENA\RGW\Validator;
/**
* NOTE: this validator should probably only be used on strings and MAYBE arrays. you've been warned.
*
* Class NotEmptyValidator
* @package MyENA\RGW\Validator
*/
class NotEmptyValidator implements Validator
{
public const NAME = 'not-empty';
public const EXPECTS = 'type-specific non-empty value';
/**
* @return string
*/
public function name(): string
{
return self::NAME;
}
/**
* @param mixed $value
* @return bool
*/
public function test($value): bool
{
switch ($type = gettype($value)) {
case 'string':
return '' !== $value;
case 'integer':
return 0 !== $value;
case 'double':
return 0 > $value || $value > 0;
case 'boolean':
// TODO: why are you setting a not empty validator on a bool value...?
return !$value;
case 'array':
return 0 !== count($value);
case 'object':
// TODO: this is super inefficient, find better way
return 0 !== count(get_object_vars($value));
default:
// catch null and resource types for now.
return true;
}
}
/**
* @return string
*/
public function expectedStatement(): string
{
return self::EXPECTS;
}
}