NotEmpty   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 179
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A shouldBreakChain() 0 4 1
A validate() 0 10 3
A isEmpty() 0 12 6
A isValid() 0 6 1
A setAllowEmpty() 0 7 2
A getMessageParameters() 0 7 1
A overwriteAllowEmpty() 0 5 1
A setAllowEmptyCallback() 0 5 1
A allowEmpty() 0 7 2
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\StringifyCallbackTrait;
12
use Particle\Validator\Rule;
13
use Particle\Validator\Value\Container;
14
15
/**
16
 * This class is responsible for checking if a certain key has a value.
17
 *
18
 * @package Particle\Validator\Rule
19
 */
20
class NotEmpty extends Rule
21
{
22
    use StringifyCallbackTrait;
23
24
    /**
25
     * The error code for when a value is empty while this is not allowed.
26
     */
27
    const EMPTY_VALUE = 'NotEmpty::EMPTY_VALUE';
28
29
    /**
30
     * The templates for the possible messages this validator can return.
31
     *
32
     * @var array
33
     */
34
    protected $messageTemplates = [
35
        self::EMPTY_VALUE => '{{ name }} must not be empty',
36
    ];
37
38
    /**
39
     * Denotes whether or not the chain should be stopped after this rule.
40
     *
41
     * @var bool
42
     */
43
    protected $shouldBreak = false;
44
45
    /**
46
     * Indicates if the value can be empty.
47
     *
48
     * @var bool
49
     */
50
    protected $allowEmpty;
51
52
    /**
53
     * Optionally contains a callable to overwrite the allow empty requirement on time of validation.
54
     *
55
     * @var callable
56
     */
57
    protected $allowEmptyCallback;
58
59
    /**
60
     * Contains the input container.
61
     *
62
     * @var Container
63
     */
64
    protected $input;
65
66
    /**
67
     * Construct the NotEmpty validator.
68
     *
69
     * @param bool $allowEmpty
70
     */
71 264
    public function __construct($allowEmpty)
72
    {
73 264
        $this->allowEmpty = (bool) $allowEmpty;
74 264
    }
75
76
    /**
77
     * @return bool
78
     */
79 251
    public function shouldBreakChain()
80
    {
81 251
        return $this->shouldBreak;
82
    }
83
84
    /**
85
     * Ensures a certain key has a value.
86
     *
87
     * @param mixed $value
88
     * @return bool
89
     */
90 251
    public function validate($value)
91
    {
92 251
        $this->shouldBreak = false;
93 251
        if ($this->isEmpty($value)) {
94 10
            $this->shouldBreak = true;
95
96 10
            return !$this->allowEmpty($this->input) ? $this->error(self::EMPTY_VALUE) : true;
97
        }
98 244
        return true;
99
    }
100
101
    /**
102
     * Determines whether or not value $value is to be considered "empty".
103
     *
104
     * @param mixed $value
105
     * @return bool
106
     */
107 251
    protected function isEmpty($value)
108
    {
109 251
        if (is_string($value) && strlen($value) === 0) {
110 5
            return true;
111 249
        } elseif ($value === null) {
112 4
            return true;
113 246
        } elseif (is_array($value) && count($value) === 0) {
114 2
            return true;
115
        }
116
117 244
        return false;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     *
123
     * @param string $key
124
     * @param Container $input
125
     * @return bool
126
     */
127 251
    public function isValid($key, Container $input)
128
    {
129 251
        $this->input = $input;
130
131 251
        return $this->validate($input->get($key));
132
    }
133
134
    /**
135
     * Set a callable or boolean value to potentially alter the allow empty requirement at the time of validation.
136
     *
137
     * This may be incredibly useful for conditional validation.
138
     *
139
     * @param callable|bool $allowEmpty
140
     * @return $this
141
     */
142 11
    public function setAllowEmpty($allowEmpty)
143
    {
144 11
        if (is_callable($allowEmpty)) {
145 2
            return $this->setAllowEmptyCallback($allowEmpty);
146
        }
147 9
        return $this->overwriteAllowEmpty($allowEmpty);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 10
    protected function getMessageParameters()
154
    {
155 10
        return array_merge(parent::getMessageParameters(), [
156 10
            'allowEmpty' => $this->allowEmpty,
157 10
            'callback' => $this->getCallbackAsString($this->allowEmptyCallback)
158 10
        ]);
159
    }
160
161
    /**
162
     * Overwrite the allow empty requirement after instantiation of this rule.
163
     *
164
     * @param bool $allowEmpty
165
     * @return $this
166
     */
167 9
    protected function overwriteAllowEmpty($allowEmpty)
168
    {
169 9
        $this->allowEmpty = $allowEmpty;
170 9
        return $this;
171
    }
172
173
    /**
174
     * Set the callback to execute to determine whether or not the rule should allow empty.
175
     *
176
     * @param callable $allowEmptyCallback
177
     * @return $this
178
     */
179 2
    protected function setAllowEmptyCallback(callable $allowEmptyCallback)
180
    {
181 2
        $this->allowEmptyCallback = $allowEmptyCallback;
182 2
        return $this;
183
    }
184
185
    /**
186
     * Determines whether or not the value may be empty.
187
     *
188
     * @param Container $input
189
     * @return bool
190
     */
191 10
    protected function allowEmpty(Container $input)
192
    {
193 10
        if (isset($this->allowEmptyCallback)) {
194 1
            $this->allowEmpty = call_user_func($this->allowEmptyCallback, $input->getArrayCopy());
195 1
        }
196 10
        return $this->allowEmpty;
197
    }
198
}
199