Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
5
9
11{
12 protected function isNewItem($fields): bool
13 {
14 return (isset($fields['ID']) === false);
15 }
16
17 protected function getRewriteFields(): array
18 {
19 return [];
20 }
21
22 public function internalizeFieldsList($arguments, $fieldsInfo = []): array
23 {
24 $fields = parent::internalizeFieldsList($arguments, $fieldsInfo);
25
26 return $this->rewriteFieldsListAsAliases([
27 'select'=>$fields['select'],
28 'filter'=>$fields['filter'],
29 'order'=>$fields['order'],
30 ]);
31 }
32
33 //region convert keys to snake case
34 public function convertKeysToSnakeCaseArguments($name, $arguments)
35 {
36 if ($name == 'modify')
37 {
38 if(isset($arguments['fields']))
39 {
40 $fields = $arguments['fields'];
41 if(!empty($fields))
42 $arguments['fields'] = $this->convertKeysToSnakeCaseFields($fields);
43 }
44 }
45 else
46 {
47 $arguments = parent::convertKeysToSnakeCaseArguments($name, $arguments);
48 }
49
50 return $arguments;
51 }
52 //endregion
53
54 //region internalize fields
55 public function internalizeArguments($name, $arguments): array
56 {
57 if ($name == 'modify')
58 {
59 $fields = $arguments['fields'];
60 if(!empty($fields))
61 $arguments['fields'] = $this->internalizeFieldsModify($fields);
62 }
63 else
64 {
65 $arguments = parent::internalizeArguments($name, $arguments);
66 }
67 return $arguments;
68 }
69
70 public function internalizeFieldsModify($fields): array
71 {
72 throw new NotImplementedException('The method internalizeFieldsModify is not implemented.');
73 }
74 //endregion
75
76 //region externalize fields
77 function externalizeFieldsGet($fields, $fieldsInfo = []): array
78 {
79 $fields = $this->rewriteFieldsGetAsOrigFields($fields);
80 return parent::externalizeFieldsGet($fields, $fieldsInfo);
81 }
82
83 public function externalizeResult($name, $fields): array
84 {
85 if($name == 'modify')
86 {
87 $fields = $this->externalizeFieldsModify($fields);
88 }
89 else
90 {
91 $fields = parent::externalizeResult($name, $fields);
92 }
93
94 return $fields;
95
96
97 }
98
99 public function externalizeFieldsModify($fields)
100 {
101 throw new NotImplementedException('The method externalizeFieldsModify is not implemented.');
102 }
103 //endregion
104
105 //region check fields
106 public function checkArguments($name, $arguments): Result
107 {
108 if ($name == 'modify')
109 {
110 $r = $this->checkFieldsModify($arguments['fields']);
111 }
112 else
113 {
114 $r = parent::checkArguments($name, $arguments);
115 }
116
117 return $r;
118 }
119
120 public function checkFieldsModify($fields): Result
121 {
122 $r = new Result();
123
124 $required = $this->checkRequiredFieldsModify($fields);
125 if(!$required->isSuccess())
126 $r->addError(new Error('Required fields: '.implode(' ', $required->getErrorMessages())));
127
128 return $r;
129 }
130
131 protected function checkRequiredFieldsModify($fields): Result
132 {
133 throw new NotImplementedException('The method checkFieldsModify is not implemented.');
134 }
135 //endregion
136
137 //region rewrite
138 final protected function getOrigNameField($name)
139 {
140 $rewriteFields = $this->getRewriteFields();
141 foreach ($rewriteFields as $origName=>$rewriteField)
142 {
143 $alias = $rewriteField['REFERENCE_FIELD'];
144 if($name === $alias)
145 {
146 return $origName;
147 }
148 }
149 return null;
150 }
151
152 final protected function rewriteFieldsAsOrigFields($fields): array
153 {
154 $result = [];
155 foreach ($fields as $name=>$value)
156 {
157 $origName = $this->getOrigNameField($name);
158
159 if($origName)
160 {
161 $result[$origName] = $value;
162 }
163 else
164 {
165 $result[$name] = $value;
166 }
167 }
168 return $result;
169 }
170
171 final protected function rewriteFieldsGetAsOrigFields($fields): array
172 {
173 return $this->rewriteFieldsAsOrigFields($fields);
174 }
175
176 public function rewriteFieldsListAsAliases($arguments): array
177 {
178 $filter = isset($arguments['filter']) ? $this->rewriteFilterFieldsAsAliases($arguments['filter']):[];
179 $select = isset($arguments['select']) ? $this->rewriteSelectFieldsAsAliases($arguments['select']):[];
180 $order = isset($arguments['order']) ? $this->rewriteOrderFieldsAsAliases($arguments['order']):[];
181
182 return [
183 'filter'=>$filter,
184 'select'=>$select,
185 'order'=>$order,
186 ];
187 }
188
189 protected function rewriteSelectFieldsAsAliases($fields): array
190 {
191 $result = [];
192 $rewriteFields = $this->getRewriteFields();
193
194 foreach ($fields as $name)
195 {
196 $fieldsIsAlias = isset($rewriteFields[$name]);
197
198 if($fieldsIsAlias)
199 {
200 if(isset($rewriteFields[$name]['REFERENCE_FIELD']))
201 {
202 $result[$name] = $rewriteFields[$name]['REFERENCE_FIELD'];
203 }
204 }
205 else
206 {
207 $result[] = $name;
208 }
209 }
210
211 return $result;
212 }
213
214 protected function rewriteFilterFieldsAsAliases($fields): array
215 {
216 $result = [];
217 $rewriteFields = $this->getRewriteFields();
218
219
220 foreach ($fields as $rawName=>$value)
221 {
222 $field = \CSqlUtil::GetFilterOperation($rawName);
223
224 $fieldsIsAlias = isset($rewriteFields[$field['FIELD']]);
225
226 if($fieldsIsAlias)
227 {
228 if(isset($rewriteFields[$field['FIELD']]['REFERENCE_FIELD']))
229 {
230 $originalName = $rewriteFields[$field['FIELD']]['REFERENCE_FIELD'];
231 $operation = mb_substr($rawName, 0, mb_strlen($rawName) - mb_strlen($field['FIELD']));
232 $result[$operation.$originalName] = $value;
233 }
234 }
235 else
236 {
237 $result[$rawName] = $value;
238 }
239 }
240
241 return $result;
242 }
243
244 protected function rewriteOrderFieldsAsAliases($fields): array
245 {
246 $result = [];
247 $rewriteFields = $this->getRewriteFields();
248
249 foreach ($fields as $name=>$value)
250 {
251 $fieldsIsAlias = isset($rewriteFields[$name]);
252
253 if($fieldsIsAlias)
254 {
255 if(isset($rewriteFields[$name]['REFERENCE_FIELD']))
256 {
257 $result[$rewriteFields[$name]['REFERENCE_FIELD']] = $value;
258 }
259 }
260 else
261 {
262 $result[$name] = $value;
263 }
264 }
265 return $result;
266 }
267 //endregion
268}
rewriteSelectFieldsAsAliases($fields)
Definition base.php:189
internalizeFieldsList($arguments, $fieldsInfo=[])
Definition base.php:22
rewriteFilterFieldsAsAliases($fields)
Definition base.php:214
rewriteFieldsAsOrigFields($fields)
Definition base.php:152
internalizeArguments($name, $arguments)
Definition base.php:55
externalizeFieldsModify($fields)
Definition base.php:99
convertKeysToSnakeCaseArguments($name, $arguments)
Definition base.php:34
externalizeFieldsGet($fields, $fieldsInfo=[])
Definition base.php:77
checkArguments($name, $arguments)
Definition base.php:106
rewriteFieldsListAsAliases($arguments)
Definition base.php:176
rewriteOrderFieldsAsAliases($fields)
Definition base.php:244
externalizeResult($name, $fields)
Definition base.php:83
checkRequiredFieldsModify($fields)
Definition base.php:131
rewriteFieldsGetAsOrigFields($fields)
Definition base.php:171
internalizeFieldsModify($fields)
Definition base.php:70