1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eloquentFilter\QueryFilter\Core\FilterBuilder\core; |
4
|
|
|
|
5
|
|
|
use eloquentFilter\QueryFilter\Detection\DetectionFactory; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class QueryFilterCoreBuilder. |
9
|
|
|
*/ |
10
|
|
|
class QueryFilterCoreBuilder implements QueryFilterCore |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var |
14
|
|
|
*/ |
15
|
|
|
protected $builder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected array $detections; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
protected $injected_detections; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected array $default_detect; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DetectionFactory |
34
|
|
|
*/ |
35
|
|
|
private DetectionFactory $detect_factory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* QueryFilter constructor. |
39
|
|
|
* |
40
|
|
|
* @param array $default_injected |
41
|
|
|
* @param array|null $detect_injected |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $default_injected, array $detect_injected = null) |
44
|
|
|
{ |
45
|
|
|
if (!empty($detect_injected)) { |
46
|
|
|
$this->setInjectedDetections($detect_injected); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->setDefaultDetect($default_injected); |
50
|
|
|
$this->setDetectFactory($this->getDetectorFactory($this->getDefaultDetect(), $this->getInjectedDetections())); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $default_detect |
55
|
|
|
*/ |
56
|
|
|
public function setDefaultDetect($default_detect): void |
57
|
|
|
{ |
58
|
|
|
$this->default_detect = $default_detect; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getDefaultDetect(): array |
65
|
|
|
{ |
66
|
|
|
return $this->default_detect; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $detections |
71
|
|
|
*/ |
72
|
|
|
public function setDetections(array $detections): void |
73
|
|
|
{ |
74
|
|
|
$this->detections = $detections; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param DetectionFactory $detect_factory |
79
|
|
|
*/ |
80
|
|
|
public function setDetectFactory(DetectionFactory $detect_factory): void |
81
|
|
|
{ |
82
|
|
|
$this->detect_factory = $detect_factory; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return DetectionFactory |
87
|
|
|
*/ |
88
|
|
|
public function getDetectFactory(): DetectionFactory |
89
|
|
|
{ |
90
|
|
|
return $this->detect_factory; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getDetections(): array |
97
|
|
|
{ |
98
|
|
|
return $this->detections; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param mixed $detect_injected |
103
|
|
|
*/ |
104
|
|
|
public function setInjectedDetections($detect_injected): void |
105
|
|
|
{ |
106
|
|
|
if (!config('eloquentFilter.enabled_custom_detection')) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$this->injected_detections = $detect_injected; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
public function getInjectedDetections() |
116
|
|
|
{ |
117
|
|
|
return $this->injected_detections; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array|null $default_detect |
122
|
|
|
* @param array|null $detect_injected |
123
|
|
|
* |
124
|
|
|
* @return DetectionFactory |
125
|
|
|
*/ |
126
|
|
|
public function getDetectorFactory(array $default_detect = null, array $detect_injected = null): DetectionFactory |
127
|
|
|
{ |
128
|
|
|
$detections = $default_detect; |
129
|
|
|
|
130
|
|
|
if (!empty($detect_injected)) { |
131
|
|
|
$detections = array_merge($detect_injected, $default_detect); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->setDetections($detections); |
|
|
|
|
135
|
|
|
|
136
|
|
|
return app(DetectionFactory::class, ['detections' => $this->getDetections()]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|