| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentValidator; |
| 4 |
|
| 5 |
class Validator |
| 6 |
{ |
| 7 |
use ValidatesAttributes, MessageBag; |
| 8 |
|
| 9 |
/** |
| 10 |
* The data under validation. |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
protected $data; |
| 15 |
|
| 16 |
/** |
| 17 |
* The rules to be applied to the data. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $rules = []; |
| 22 |
|
| 23 |
/** |
| 24 |
* All of the error messages. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
protected $messages; |
| 29 |
|
| 30 |
/** |
| 31 |
* All of the user provided messages. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $customMessages = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* The validation rules that imply the field is required. |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
protected $implicitRules = [ |
| 43 |
'Required', |
| 44 |
'Filled', |
| 45 |
'RequiredWith', |
| 46 |
'RequiredWithAll', |
| 47 |
'RequiredWithout', |
| 48 |
'RequiredWithoutAll', |
| 49 |
'RequiredIf', |
| 50 |
'RequiredUnless', |
| 51 |
'Accepted', |
| 52 |
'Present', |
| 53 |
]; |
| 54 |
|
| 55 |
/** |
| 56 |
* Create a new Validator instance. |
| 57 |
* |
| 58 |
* @param array $data |
| 59 |
* @param array $rules |
| 60 |
* @param array $messages |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function __construct(array $data = [], array $rules = [], array $messages = []) |
| 65 |
{ |
| 66 |
$this->data = $data; |
| 67 |
|
| 68 |
$this->setRules($rules); |
| 69 |
|
| 70 |
$this->customMessages = $messages; |
| 71 |
|
| 72 |
$this->messages = []; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Create a new Validator instance. |
| 77 |
* |
| 78 |
* @param array $data |
| 79 |
* @param array $rules |
| 80 |
* @param array $messages |
| 81 |
* |
| 82 |
* @return \FluentValidator\Validator |
| 83 |
*/ |
| 84 |
public static function make(array $data = [], array $rules = [], array $messages = []) |
| 85 |
{ |
| 86 |
if(!$rules) { |
| 87 |
$rules = []; |
| 88 |
} |
| 89 |
if(!$messages) { |
| 90 |
$messages = []; |
| 91 |
} |
| 92 |
|
| 93 |
return new static($data, $rules, $messages); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Set the validation rules. |
| 98 |
* |
| 99 |
* @param array $rules |
| 100 |
* |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
protected function setRules(array $rules = []) |
| 104 |
{ |
| 105 |
$this->rules = array_merge_recursive( |
| 106 |
$this->rules, (new ValidationRuleParser($this->data))->explode($rules) |
| 107 |
); |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Validate the data against the provided rules. |
| 114 |
* |
| 115 |
* @return $this |
| 116 |
*/ |
| 117 |
public function validate() |
| 118 |
{ |
| 119 |
foreach ($this->rules as $attribute => $rules) { |
| 120 |
foreach ($rules as $rule) { |
| 121 |
$this->validateAttribute($attribute, $rule); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Validate each of the attribute of the data. |
| 130 |
* |
| 131 |
* @param $attribute |
| 132 |
* @param $rule |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
protected function validateAttribute($attribute, $rule) |
| 137 |
{ |
| 138 |
list($rule, $parameters) = ValidationRuleParser::parse($rule); |
| 139 |
|
| 140 |
$value = $this->getValue($attribute); |
| 141 |
|
| 142 |
$ruleCamelCase = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $rule))); |
| 143 |
|
| 144 |
|
| 145 |
$method = 'validate'.$ruleCamelCase; |
| 146 |
|
| 147 |
if ($this->shouldValidate($method, $ruleCamelCase, $attribute, $value) && |
| 148 |
! $this->$method($attribute, $value, $parameters) |
| 149 |
) { |
| 150 |
$this->addFailure($attribute, $rule, $parameters); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Access the data by attribute name. |
| 156 |
* |
| 157 |
* @param $attribute |
| 158 |
* |
| 159 |
* @return mixed |
| 160 |
*/ |
| 161 |
protected function getValue($attribute) |
| 162 |
{ |
| 163 |
$attribute = str_replace(['[', ']'], ['.', ''], $attribute); |
| 164 |
|
| 165 |
return Arr::get($this->data, $attribute); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add error message upon validation failed of an attribute. |
| 170 |
* |
| 171 |
* @param $attribute |
| 172 |
* @param $rule |
| 173 |
* @param $parameters |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
protected function addFailure($attribute, $rule, $parameters) |
| 178 |
{ |
| 179 |
$this->messages[$attribute][$rule] = $this->generate($attribute, $rule, $parameters); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get all of the validation error messages. |
| 184 |
* |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public function errors() |
| 188 |
{ |
| 189 |
return $this->messages; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Determine if the data passes the validation rules. |
| 194 |
* |
| 195 |
* @return bool |
| 196 |
*/ |
| 197 |
public function passes() |
| 198 |
{ |
| 199 |
return ! $this->fails(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Determine if the data fails the validation rules. |
| 204 |
* |
| 205 |
* @return bool |
| 206 |
*/ |
| 207 |
public function fails() |
| 208 |
{ |
| 209 |
return (bool) count($this->messages); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Add conditions to a given field based on a Closure. |
| 214 |
* |
| 215 |
* @param string|array $attribute |
| 216 |
* @param string|array $rules |
| 217 |
* @param callable $callback |
| 218 |
* |
| 219 |
* @return $this |
| 220 |
*/ |
| 221 |
public function sometimes($attribute, $rules, callable $callback) |
| 222 |
{ |
| 223 |
$payload = $this->data; |
| 224 |
|
| 225 |
if (call_user_func($callback, $payload)) { |
| 226 |
foreach ((array) $attribute as $key) { |
| 227 |
$this->setRules([$key => $rules]); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $this; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Determine if the attribute has a required rule. |
| 236 |
* |
| 237 |
* @param $attribute |
| 238 |
* |
| 239 |
* @return bool |
| 240 |
*/ |
| 241 |
public function hasRequired($attribute) |
| 242 |
{ |
| 243 |
foreach ($this->rules[$attribute] as $rule) { |
| 244 |
if (strpos($rule, 'required') !== false) { |
| 245 |
return true; |
| 246 |
break; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Determine if the attribute should be validated. |
| 255 |
* |
| 256 |
* @param $method |
| 257 |
* @param $attribute |
| 258 |
* @param $value |
| 259 |
* |
| 260 |
* @return bool |
| 261 |
*/ |
| 262 |
public function shouldValidate($method, $rule, $attribute, $value) |
| 263 |
{ |
| 264 |
return $this->hasMethod($method) && $this->presentOrRuleIsImplicit($rule, $attribute, $value); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Determines if this object has this method. |
| 269 |
* |
| 270 |
* @param $method |
| 271 |
* |
| 272 |
* @return bool |
| 273 |
*/ |
| 274 |
public function hasMethod($method) |
| 275 |
{ |
| 276 |
return method_exists($this, $method); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Determine if a given rule implies the attribute is required. |
| 281 |
* |
| 282 |
* @param string $rule |
| 283 |
* |
| 284 |
* @return bool |
| 285 |
*/ |
| 286 |
protected function isImplicit($rule) |
| 287 |
{ |
| 288 |
return in_array($rule, $this->implicitRules); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Determine if the field is present, or the rule implies required. |
| 293 |
* |
| 294 |
* @param string $rule |
| 295 |
* @param string $attribute |
| 296 |
* @param mixed $value |
| 297 |
* |
| 298 |
* @return bool |
| 299 |
*/ |
| 300 |
protected function presentOrRuleIsImplicit($rule, $attribute, $value) |
| 301 |
{ |
| 302 |
return $this->validatePresent($attribute, $value) || $this->isImplicit($rule); |
| 303 |
} |
| 304 |
} |