Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
15 / 15 |
Rule | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
15 / 15 |
__construct | |
100.00% |
1 / 1 |
2 | |
100.00% |
2 / 2 |
|||
boot | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getClassName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
validate | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
passes | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setError | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
message | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\ValidationRules\Rules; |
4 | |
5 | use Illuminate\Contracts\Validation\Rule as BaseRule; |
6 | use Illuminate\Support\Facades\Validator; |
7 | use Illuminate\Support\Str; |
8 | |
9 | class Rule implements BaseRule |
10 | { |
11 | /** |
12 | * Undocumented variable |
13 | * |
14 | * @var array |
15 | */ |
16 | protected $errors = []; |
17 | |
18 | /** |
19 | * Undocumented variable |
20 | * |
21 | * @var \Illuminate\Validation\Validator |
22 | */ |
23 | protected $validator; |
24 | |
25 | /** |
26 | * Undocumented variable |
27 | * |
28 | * @var string |
29 | */ |
30 | protected $message = ''; |
31 | |
32 | |
33 | /** |
34 | * Undocumented function |
35 | * |
36 | * @param string|array $parameters |
37 | */ |
38 | public function __construct($parameters = []) |
39 | { |
40 | $this->parameters = is_array($parameters) ? $parameters : [$parameters]; |
41 | } |
42 | |
43 | /** |
44 | * Undocumented function |
45 | * |
46 | * @return void |
47 | */ |
48 | public static function boot(): void |
49 | { |
50 | Validator::extend(self::getClassName(), static::class); |
51 | } |
52 | |
53 | /** |
54 | * Undocumented function |
55 | * |
56 | * @return string |
57 | */ |
58 | protected static function getClassName(): string |
59 | { |
60 | return Str::snake(class_basename(static::class)); |
61 | } |
62 | |
63 | /** |
64 | * Undocumented function |
65 | * |
66 | * @param string $attribute |
67 | * @param string $value |
68 | * @param array $parameters |
69 | * @param \Illuminate\Validation\Validator $validator |
70 | * @return bool |
71 | */ |
72 | public function validate($attribute, $value, $parameters, $validator): bool |
73 | { |
74 | $this->validator = $validator; |
75 | $this->setError(); |
76 | return $this->check($attribute, $value, $parameters); |
77 | } |
78 | |
79 | /** |
80 | * Undocumented function |
81 | * |
82 | * @param [type] $attribute |
83 | * @param [type] $value |
84 | * @return void |
85 | */ |
86 | public function passes($attribute, $value): bool { |
87 | return $this->check($attribute, $value, $this->parameters); |
88 | } |
89 | |
90 | /** |
91 | * Undocumented function |
92 | * |
93 | * @param array|string $error |
94 | * @return void |
95 | */ |
96 | protected function setError($error = null): void |
97 | { |
98 | if ($error) { |
99 | $this->errors[] = $error; |
100 | } |
101 | |
102 | if(isset($this->validator)) { |
103 | $this->validator->fallbackMessages[self::getClassName()] = $this->message(); |
104 | } |
105 | } |
106 | |
107 | /** |
108 | * Get the validation error message. |
109 | * |
110 | * @return string |
111 | */ |
112 | public function message(): string |
113 | { |
114 | return implode('. ', array_filter(array_merge([$this->message], $this->errors))); |
115 | } |
116 | } |