|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eloquentFilter\QueryFilter\Detection; |
|
4
|
|
|
|
|
5
|
|
|
use eloquentFilter\QueryFilter\Detection\Contract\DetectorConditionsContract; |
|
6
|
|
|
use eloquentFilter\QueryFilter\Exceptions\EloquentFilterException; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class DetectorConditions. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DetectorCondition |
|
13
|
|
|
{ |
|
14
|
|
|
protected $errorExceptionWhileList = "You must set %s in whiteListFilter in %s |
|
15
|
|
|
or create a override method with name %s or call ignoreRequest function for ignore %s."; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var |
|
18
|
|
|
*/ |
|
19
|
|
|
private $detector; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* DetectorConditions constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $detector |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(array $detector) |
|
27
|
|
|
{ |
|
28
|
|
|
$detector_collect = collect($detector); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$detector_collect->map(function ($detector_obj) { |
|
31
|
|
|
if (!empty($detector_obj)) { |
|
32
|
|
|
$reflect = new \ReflectionClass($detector_obj); |
|
33
|
|
|
if ($reflect->implementsInterface(DetectorConditionsContract::class)) { |
|
34
|
|
|
return $detector_obj; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
})->toArray(); |
|
38
|
|
|
|
|
39
|
|
|
$this->detector = $detector_collect; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $field |
|
44
|
|
|
* @param $params |
|
45
|
|
|
* @param null $model |
|
|
|
|
|
|
46
|
|
|
* |
|
47
|
|
|
* @return string|null |
|
48
|
|
|
* @throws Exception |
|
49
|
|
|
* |
|
50
|
|
|
*/ |
|
51
|
|
|
public function detect(string $field, $params, $model = null): ?string |
|
52
|
|
|
{ |
|
53
|
|
|
foreach ($this->detector as $detector_obj) { |
|
54
|
|
|
if ($this->handelListFields($field, $model->getWhiteListFilter(), $model->checkModelHasOverrideMethod($field), $model)) { |
|
|
|
|
|
|
55
|
|
|
$out = $detector_obj::detect($field, $params, $model->checkModelHasOverrideMethod($field)); |
|
56
|
|
|
if (!empty($out)) { |
|
57
|
|
|
return $out; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $field |
|
67
|
|
|
* @param array|null $list_white_filter_model |
|
68
|
|
|
* @param bool $has_method |
|
69
|
|
|
* @param $model_class |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
* @throws Exception |
|
73
|
|
|
* |
|
74
|
|
|
*/ |
|
75
|
|
|
private function handelListFields(string $field, ?array $list_white_filter_model, bool $has_method, $model_class): bool |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->checkSetWhiteListFields($field, $list_white_filter_model) || ($field == 'f_params' || $field == 'or') || $has_method) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$class_name = class_basename($model_class); |
|
82
|
|
|
|
|
83
|
|
|
throw new EloquentFilterException(sprintf($this->errorExceptionWhileList, $field, $class_name, $field, $field), 1); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $field |
|
88
|
|
|
* @param array|null $query |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
private function checkSetWhiteListFields(string $field, ?array $query): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if (in_array($field, $query) || |
|
95
|
|
|
$query[0] == '*') { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|