1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PrettyBx\Support\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\Validator; |
6
|
|
|
use Illuminate\Translation\{ArrayLoader, Translator}; |
7
|
|
|
use PrettyBx\Support\Dictionaries\DefaultValidationMessages as DefaultMessages; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
trait Validatable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Валидирует переданные данные |
14
|
|
|
* |
15
|
|
|
* @access public |
16
|
|
|
* @param array $data |
17
|
|
|
* @param array $rules Default: null |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function validate(array $data, array $rules = null, array $messages = null): void |
21
|
|
|
{ |
22
|
|
|
$rules = $rules ?? (property_exists($this, 'rules') ? $this->rules : []); |
23
|
|
|
|
24
|
|
|
$validator = new Validator( |
25
|
|
|
new Translator(new ArrayLoader($this->getValidationLocale(), ''), $this->getValidationLocale()), |
|
|
|
|
26
|
|
|
$data, |
27
|
|
|
$rules, |
28
|
|
|
$this->getValidationMessages($messages) |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
if ($validator->fails()) { |
32
|
|
|
throw new InvalidArgumentException(implode(', ', $validator->errors()->all())); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Валидирует отдельный атрибут |
38
|
|
|
* |
39
|
|
|
* @access public |
40
|
|
|
* @param string $attribute |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @param array $rules Default: null |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function validateAttribute(string $attribute, $value, array $rules = null): void |
46
|
|
|
{ |
47
|
|
|
if (empty($rules)) { |
48
|
|
|
if (property_exists($this, 'rules') && !empty($this->rules[$attribute])) { |
49
|
|
|
$rules = [$attribute => $this->rules[$attribute]]; |
50
|
|
|
} else { |
51
|
|
|
$rules = [$attribute => []]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$validator = new Validator( |
56
|
|
|
new Translator(new ArrayLoader($this->getValidationLocale(), ''), $this->getValidationLocale()), |
|
|
|
|
57
|
|
|
[$attribute => $value], |
58
|
|
|
$rules, |
59
|
|
|
$this->getValidationMessages() |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
if ($validator->fails()) { |
63
|
|
|
throw new InvalidArgumentException(implode(', ', $validator->errors()->all())); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* getValidationLocale. |
69
|
|
|
* |
70
|
|
|
* @access protected |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function getValidationLocale(): string |
74
|
|
|
{ |
75
|
|
|
return property_exists($this, 'locale') ? $this->locale : 'en_US'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* getValidationMessages. |
80
|
|
|
* |
81
|
|
|
* @access protected |
82
|
|
|
* @param array $messages |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function getValidationMessages(array $messages = null): array |
86
|
|
|
{ |
87
|
|
|
return $messages ?? (property_exists($this, 'messages') ? $this->messages : DefaultMessages::getItems()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.