GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#115)
by Carlos
02:30
created

NotEmpty::allowEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 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;
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 140
    public function __construct($allowEmpty)
72
    {
73 140
        $this->allowEmpty = (bool) $allowEmpty;
74 140
    }
75
76
    /**
77
     * @return bool
78
     */
79 130
    public function shouldBreakChain()
80
    {
81 130
        return $this->shouldBreak;
82
    }
83
84
    /**
85
     * Ensures a certain key has a value.
86
     *
87
     * @param mixed $value
88
     * @return bool
89
     */
90 130
    public function validate($value)
91
    {
92 130
        if ($this->isEmpty($value)) {
93 6
            $this->shouldBreak = true;
94
95 6
            return !$this->allowEmpty($this->input) ? $this->error(self::EMPTY_VALUE) : true;
96
        }
97 125
        return true;
98
    }
99
100
    /**
101
     * Determines whether or not value $value is to be considered "empty".
102
     *
103
     * @param mixed $value
104
     * @return bool
105
     */
106 130
    protected function isEmpty($value)
107
    {
108 130
        return (is_string($value) && strlen($value) === 0) || $value === null || (is_array($value) && count($value) === 0);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109
    }
110
111
    /**
112
     * @inheritdoc
113
     *
114
     * @param string $key
115
     * @param Container $input
116
     * @return bool
117
     */
118 130
    public function isValid($key, Container $input)
119
    {
120 130
        $this->input = $input;
121
122 130
        return $this->validate($input->get($key));
123
    }
124
125
    /**
126
     * Set a callable or boolean value to potentially alter the allow empty requirement at the time of validation.
127
     *
128
     * This may be incredibly useful for conditional validation.
129
     *
130
     * @param callable|bool $allowEmpty
131
     * @return $this
132
     */
133 4
    public function setAllowEmpty($allowEmpty)
134
    {
135 4
        if (is_callable($allowEmpty)) {
136 2
            return $this->setAllowEmptyCallback($allowEmpty);
137
        }
138 2
        return $this->overwriteAllowEmpty($allowEmpty);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 6
    protected function getMessageParameters()
145
    {
146 6
        return array_merge(parent::getMessageParameters(), [
147 6
            'allowEmpty' => $this->allowEmpty,
148 6
            'callback' => $this->getCallbackAsString($this->allowEmptyCallback)
149 6
        ]);
150
    }
151
152
    /**
153
     * Overwrite the allow empty requirement after instantiation of this rule.
154
     *
155
     * @param bool $allowEmpty
156
     * @return $this
157
     */
158 2
    protected function overwriteAllowEmpty($allowEmpty)
159
    {
160 2
        $this->allowEmpty = $allowEmpty;
161 2
        return $this;
162
    }
163
164
    /**
165
     * Set the callback to execute to determine whether or not the rule should allow empty.
166
     *
167
     * @param callable $allowEmptyCallback
168
     * @return $this
169
     */
170 2
    protected function setAllowEmptyCallback(callable $allowEmptyCallback)
171
    {
172 2
        $this->allowEmptyCallback = $allowEmptyCallback;
173 2
        return $this;
174
    }
175
176
    /**
177
     * Determines whether or not the value may be empty.
178
     *
179
     * @param Container $input
180
     * @return bool
181
     */
182 6
    protected function allowEmpty(Container $input)
183
    {
184 6
        if (isset($this->allowEmptyCallback)) {
185 1
            $this->allowEmpty = call_user_func($this->allowEmptyCallback, $input->getArrayCopy());
186 1
        }
187 6
        return $this->allowEmpty;
188
    }
189
}
190