Required   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 164
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A shouldBreakChain() 0 4 1
A validate() 0 4 1
A isValid() 0 15 3
A setRequired() 0 8 2
A overwriteRequired() 0 5 1
A setRequiredCallback() 0 5 1
A getMessageParameters() 0 7 1
A isRequired() 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 required value is set.
17
 *
18
 * @package Particle\Validator\Rule
19
 */
20
class Required extends Rule
21
{
22
    use StringifyCallbackTrait;
23
24
    /**
25
     * The error code when a required field doesn't exist.
26
     */
27
    const NON_EXISTENT_KEY = 'Required::NON_EXISTENT_KEY';
28
29
    /**
30
     * The templates for the possible messages this validator can return.
31
     *
32
     * @var array
33
     */
34
    protected $messageTemplates = [
35
        self::NON_EXISTENT_KEY => '{{ key }} must be provided, but does not exist'
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 is required.
47
     *
48
     * @var bool
49
     */
50
    protected $required;
51
52
    /**
53
     * Optionally contains a callable to overwrite the required requirement on time of validation.
54
     *
55
     * @var callable
56
     */
57
    protected $requiredCallback;
58
59
    /**
60
     * Contains the input container.
61
     *
62
     * @var Container
63
     */
64
    protected $input;
65
66
    /**
67
     * Construct the Required validator.
68
     *
69
     * @param bool $required
70
     */
71 264
    public function __construct($required)
72
    {
73 264
        $this->required = $required;
74 264
    }
75
76
    /**
77
     * @return bool
78
     */
79 259
    public function shouldBreakChain()
80
    {
81 259
        return $this->shouldBreak;
82
    }
83
84
    /**
85
     * Does nothing, because validity is determined in isValid.
86
     *
87
     * @param mixed $value
88
     * @return bool
89
     */
90 254
    public function validate($value)
91
    {
92 254
        return true;
93
    }
94
95
    /**
96
     * Determines whether or not the key is set when required, and if there is a value if allow empty is false.
97
     *
98
     * @param string $key
99
     * @param Container $input
100
     * @return bool
101
     */
102 259
    public function isValid($key, Container $input)
103
    {
104 259
        $this->shouldBreak = false;
105 259
        $this->required = $this->isRequired($input);
106
107 259
        if (!$input->has($key)) {
108 12
            $this->shouldBreak = true;
109
110 12
            if ($this->required) {
111 9
                return $this->error(self::NON_EXISTENT_KEY);
112
            }
113 4
        }
114
115 254
        return $this->validate($input->get($key));
116
    }
117
118
    /**
119
     * Set a callable to potentially alter the required requirement at the time of validation.
120
     *
121
     * This may be incredibly useful for conditional validation.
122
     *
123
     * @param callable|bool $required
124
     * @return $this
125
     */
126 5
    public function setRequired($required)
127
    {
128 5
        if (is_callable($required)) {
129 4
            return $this->setRequiredCallback($required);
130
        }
131
132 1
        return $this->overwriteRequired((bool) $required);
133
    }
134
135
    /**
136
     * Overwrite the required requirement after instantiation of this object.
137
     *
138
     * @param bool $required
139
     * @return $this
140
     */
141 1
    protected function overwriteRequired($required)
142
    {
143 1
        $this->required = $required;
144 1
        return $this;
145
    }
146
147
    /**
148
     * Set the required callback, and return $this.
149
     *
150
     * @param callable $requiredCallback
151
     * @return $this
152
     */
153 4
    protected function setRequiredCallback(callable $requiredCallback)
154
    {
155 4
        $this->requiredCallback = $requiredCallback;
156 4
        return $this;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 11
    protected function getMessageParameters()
163
    {
164 11
        return array_merge(parent::getMessageParameters(), [
165 11
            'required' => $this->required,
166 11
            'callback' => $this->getCallbackAsString($this->requiredCallback)
167 11
        ]);
168
    }
169
170
    /**
171
     * Determines if the value is required.
172
     *
173
     * @param Container $input
174
     * @return bool
175
     */
176 259
    protected function isRequired(Container $input)
177
    {
178 259
        if (isset($this->requiredCallback)) {
179 2
            $this->required = call_user_func_array($this->requiredCallback, [$input->getArrayCopy()]);
180 2
        }
181 259
        return $this->required;
182
    }
183
}
184