NotEmptyConstraint::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * Check if the value is not "empty" (not null, 0, false, '', or []).
10
 */
11
class NotEmptyConstraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * NotEmptyConstraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        if (is_string($value)) {
34
            $value = trim($value);
35
        }
36
37
        return ! empty($value);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getMessage() {
44
        if ($this->message !== null) {
45
            return $this->message;
46
        }
47
48
        return 'Must not be empty.';
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getOptions() {
55
        return [];
56
    }
57
}
58