|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrightComponents\Valid; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Contracts\Validation\Rule; |
|
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
8
|
|
|
use BrightComponents\Valid\Contracts\ValidationService\ValidationService; |
|
9
|
|
|
|
|
10
|
|
|
abstract class CustomRule implements Rule |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The current validator instance. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static $validator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The current FormRequest instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Illuminate\Http\Request |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The current ValidationService instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \BrightComponents\Valid\ValidationService\ValidationService |
|
30
|
|
|
*/ |
|
31
|
|
|
protected static $service; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the validator property. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function validator(?Validator $validator = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if (! isset(static::$validator) && $validator) { |
|
41
|
|
|
static::$validator = $validator; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return static::$validator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set the request property. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Illuminate\Http\Request $request |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function request(?Request $request = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (! isset(static::$request) && $request) { |
|
55
|
|
|
static::$request = $request; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return static::$request; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set the Validation Service property. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \BrightComponents\Valid\Contracts\ValidationService\ValidationService $service |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function service(? ValidationService $service = null) |
|
67
|
|
|
{ |
|
68
|
|
|
if (! isset(static::$service) && $service) { |
|
69
|
|
|
static::$service = $service; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return static::$service; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Determine if the validation rule passes. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $attribute |
|
79
|
|
|
* @param mixed $value |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract public function passes($attribute, $value); |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the validation error message. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function message(); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Handle property accessibility for CustomRule. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $property |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
public function __get($property) |
|
100
|
|
|
{ |
|
101
|
|
|
if ('validator' === $property || 'request' === $property || 'service' === $property) { |
|
102
|
|
|
return static::$property ?? static::$property(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->$property; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.