1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrightComponents\Common\AbstractClasses; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Contracts\Validation\Rule; |
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
8
|
|
|
|
9
|
|
|
abstract class CustomRule implements Rule |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The current validator instance. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
15
|
|
|
*/ |
16
|
|
|
protected static $validator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The current FormRequest instance. |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Http\Request |
22
|
|
|
*/ |
23
|
|
|
protected static $request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the validator property. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
29
|
|
|
*/ |
30
|
|
|
public static function validator(? Validator $validator = null) |
31
|
|
|
{ |
32
|
|
|
if (! isset(static::$validator) && $validator) { |
33
|
|
|
static::$validator = $validator; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return static::$validator; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the request property. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Http\Request $request |
43
|
|
|
*/ |
44
|
|
|
public static function request(? Request $request = null) |
45
|
|
|
{ |
46
|
|
|
if (! isset(static::$request) && $request) { |
47
|
|
|
static::$request = $request; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return static::$request; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Determine if the validation rule passes. |
55
|
|
|
* |
56
|
|
|
* @param string $attribute |
57
|
|
|
* @param mixed $value |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
abstract public function passes($attribute, $value); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the validation error message. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
abstract public function message(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Handle property accessibility for CustomRule. |
72
|
|
|
* |
73
|
|
|
* @param string $property |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function __get($property) |
78
|
|
|
{ |
79
|
|
|
if ('validator' === $property || 'request' === $property) { |
80
|
|
|
return static::$property ?? static::$property(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->$property; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|