|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mjedari\Larafilter\Filters; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\Validator; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Mjedari\Larafilter\Exceptions\InvalidQueryString; |
|
10
|
|
|
|
|
11
|
|
|
abstract class FilterContract |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $key; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
protected $value; |
|
16
|
|
|
|
|
17
|
|
|
protected Request $request; |
|
18
|
|
|
|
|
19
|
|
|
protected $cast; |
|
20
|
|
|
|
|
21
|
|
|
protected static $queryName; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->request = request(); |
|
26
|
|
|
$this->value = $this->request->get($this->getKey()) ?? null; |
|
27
|
|
|
$this->valid(); |
|
28
|
|
|
$this->value = $this->castedAttribute(); |
|
29
|
|
|
$this->setValue(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function apply(Builder $builder) |
|
33
|
|
|
{ |
|
34
|
|
|
// |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function options() |
|
38
|
|
|
{ |
|
39
|
|
|
// |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function calledClassName() |
|
43
|
|
|
{ |
|
44
|
|
|
return strtolower(Str::afterLast(get_called_class(), '\\')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getKey(): string |
|
51
|
|
|
{ |
|
52
|
|
|
// Two way: |
|
53
|
|
|
// there is a query name |
|
54
|
|
|
if (static::$queryName) { |
|
55
|
|
|
return static::$queryName; |
|
56
|
|
|
} |
|
57
|
|
|
// there is no query name |
|
58
|
|
|
return $this->calledClassName(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function valid() |
|
62
|
|
|
{ |
|
63
|
|
|
$validator = Validator::make([$this->getKey() => $this->value], $this->getRules()); |
|
64
|
|
|
if (! $validator->passes()) { |
|
65
|
|
|
$messages = $this->getErrorMessages($validator->errors()->messages()[$this->getKey()]); |
|
66
|
|
|
throw new InvalidQueryString($messages); |
|
67
|
|
|
} |
|
68
|
|
|
// return ; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function rules() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
'required', |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function castedAttribute() |
|
79
|
|
|
{ |
|
80
|
|
|
switch ($this->cast) { |
|
81
|
|
|
case 'boolean': |
|
82
|
|
|
return strtolower($this->value) === 'true'; |
|
83
|
|
|
case 'integer': |
|
84
|
|
|
return (int) $this->value; |
|
85
|
|
|
case 'float': |
|
86
|
|
|
return (float) $this->value; |
|
87
|
|
|
case 'double': |
|
88
|
|
|
return (float) $this->value; |
|
89
|
|
|
default: |
|
90
|
|
|
return $this->value; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function getErrorMessages($errors) |
|
95
|
|
|
{ |
|
96
|
|
|
$result = ''; |
|
97
|
|
|
foreach ($errors as $message) { |
|
98
|
|
|
$result .= '<li>'.$message.'</li>'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function getRules(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
$this->getKey() => $this->rules(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function setValue(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$propertyName = $this->getKey(); |
|
114
|
|
|
$this->$propertyName = $this->value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|