1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Queries; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixModels\Adapters\D7Adapter; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
class D7Query extends BaseQuery |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Query select. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $select = ['*']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Query group by. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
public $group = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Query runtime. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
public $runtime = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Query limit. |
33
|
|
|
* |
34
|
|
|
* @var int|null |
35
|
|
|
*/ |
36
|
|
|
public $limit = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Query offset. |
40
|
|
|
* |
41
|
|
|
* @var int|null |
42
|
|
|
*/ |
43
|
|
|
public $offset = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Cache joins? |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
public $cacheJoins = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Data doubling? |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
public $dataDoubling = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adapter to interact with Bitrix D7 API. |
61
|
|
|
* |
62
|
|
|
* @var D7Adapter |
63
|
|
|
*/ |
64
|
|
|
protected $bxObject; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get count of users that match $filter. |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function count() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$className = $this->bxObject->getClassName(); |
74
|
|
|
$queryType = 'D7Query::count'; |
75
|
|
|
$filter = $this->filter; |
76
|
|
|
|
77
|
|
|
$callback = function () use ($filter) { |
78
|
|
|
return (int) $this->bxObject->getCount($filter); |
79
|
|
|
}; |
80
|
|
|
|
81
|
|
|
return $this->handleCacheIfNeeded(compact('className', 'filter', 'queryType'), $callback); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get list of items. |
86
|
|
|
* |
87
|
|
|
* @return Collection |
88
|
|
|
*/ |
89
|
|
|
protected function loadModels() |
90
|
|
|
{ |
91
|
|
|
$params = [ |
92
|
|
|
'select' => $this->select, |
93
|
|
|
'filter' => $this->filter, |
94
|
|
|
'group' => $this->group, |
95
|
|
|
'order' => $this->sort, |
96
|
|
|
'limit' => $this->limit, |
97
|
|
|
'offset' => $this->offset, |
98
|
|
|
'runtime' => $this->runtime, |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if ($this->cacheTtl && $this->cacheJoins) { |
102
|
|
|
$params['cache'] = ['ttl' => $this->cacheTtl, 'cache_joins' => true]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$className = $this->bxObject->getClassName(); |
106
|
|
|
$queryType = 'D7Query::getList'; |
107
|
|
|
$keyBy = $this->keyBy; |
108
|
|
|
|
109
|
|
|
$callback = function () use ($className, $params) { |
110
|
|
|
$rows = []; |
111
|
|
|
$result = $this->bxObject->getList($params); |
112
|
|
|
while ($row = $result->fetch()) { |
113
|
|
|
$this->addItemToResultsUsingKeyBy($rows, new $this->modelName($row['ID'], $row)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return new Collection($rows); |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
return $this->handleCacheIfNeeded(compact('className', 'params', 'queryType', 'keyBy'), $callback); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Setter for limit. |
124
|
|
|
* |
125
|
|
|
* @param int|null $value |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function limit($value) |
129
|
|
|
{ |
130
|
|
|
$this->limit = $value; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Setter for offset. |
137
|
|
|
* |
138
|
|
|
* @param int|null $value |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function offset($value) |
142
|
|
|
{ |
143
|
|
|
$this->offset = $value; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the "page number" value of the query. |
150
|
|
|
* |
151
|
|
|
* @param int $num |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function page($num) |
155
|
|
|
{ |
156
|
|
|
return $this->offset((int) $this->limit * ($num - 1)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Setter for offset. |
161
|
|
|
* |
162
|
|
|
* @param array|\Bitrix\Main\Entity\ExpressionField $fields |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function runtime($fields) |
166
|
|
|
{ |
167
|
|
|
$this->runtime = is_array($fields) ? $fields : [$fields]; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Setter for cacheJoins. |
174
|
|
|
* |
175
|
|
|
* @param bool $value |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function cacheJoins($value = true) |
179
|
|
|
{ |
180
|
|
|
$this->cacheJoins = $value; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function enableDataDoubling() |
186
|
|
|
{ |
187
|
|
|
$this->dataDoubling = true; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function disableDataDoubling() |
193
|
|
|
{ |
194
|
|
|
$this->dataDoubling = false; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* For testing. |
201
|
|
|
* |
202
|
|
|
* @param $bxObject |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function setAdapter($bxObject) |
206
|
|
|
{ |
207
|
|
|
$this->bxObject = $bxObject; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.