Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
3
5
6
13
14abstract class Base
15{
16 const TYPE_INT = 'integer';
17 const TYPE_FLOAT = 'double';
18 const TYPE_STRING = 'string';
19 const TYPE_CHAR = 'char';
20 const TYPE_LIST = 'list';
21 const TYPE_TEXT = 'text';
22 const TYPE_FILE = 'file';
23 const TYPE_DATE = 'date';
24 const TYPE_DATETIME = 'datetime';
25 const TYPE_DATATYPE = 'datatype';
26
27 abstract public function getFields();
28
29 public function prepareFieldInfos($fields)
30 {
31 $result = [];
32 if (is_array($fields))
33 {
34 foreach($fields as $name => $info)
35 {
36 $attributs = isset($info['ATTRIBUTES']) ? $info['ATTRIBUTES'] : [];
37
38 if(in_array(Attributes::Hidden, $attributs, true))
39 {
40 continue;
41 }
42
43 $result[$name] = array(
44 'TYPE' => $info['TYPE'],
45 'IS_REQUIRED' => in_array(Attributes::Required, $attributs, true),
46 'IS_READ_ONLY' => in_array(Attributes::ReadOnly, $attributs, true),
47 'IS_IMMUTABLE' => in_array(Attributes::Immutable, $attributs, true)
48 );
49 }
50 }
51
52 return $result;
53 }
54
55 public function getSettableFields()
56 {
57 return array_keys(
58 $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]])
59 );
60 }
61
62 public function getListFieldInfo(array $fieldsInfo, $params=[])
63 {
64 $list = [];
65
66 $filter = is_set($params, 'filter')?$params['filter']:[];
67 $ignoredAttributes = is_set($filter, 'ignoredAttributes')?$filter['ignoredAttributes']:[];
68 $ignoredFields = is_set($filter, 'ignoredFields')?$filter['ignoredFields']:[];
69 $skipFields = is_set($filter, 'skipFields')?$filter['skipFields']:[];
70
71 foreach ($fieldsInfo as $name=>$info)
72 {
73 if(in_array($name, $ignoredFields))
74 {
75 continue;
76 }
77 elseif(in_array($name, $skipFields) == false)
78 {
79 if(isset($info['ATTRIBUTES']))
80 {
81 $skipAttr = array_intersect($ignoredAttributes, $info['ATTRIBUTES']);
82 if(!empty($skipAttr))
83 {
84 continue;
85 }
86 }
87 }
88
89 $list[$name] = $info;
90 }
91
92 return $list;
93 }
94
95 protected function isNewItem($fields)
96 {
97 return (isset($fields['ID']) === false);
98 }
99
100 //region convert keys to snake case
101 public function convertKeysToSnakeCaseFields($fields)
102 {
103 return $this->convertKeysToSnakeCase($fields);
104 }
105
106 public function convertKeysToSnakeCaseSelect($fields)
107 {
108 $converter = new Converter(Converter::VALUES | Converter::TO_SNAKE | Converter::TO_UPPER| Converter::TO_SNAKE_DIGIT);
109 $items = $converter->process($fields);
110 return $this->converterValuesProcessOnAfter($items);
111 }
112
113 public function convertKeysToSnakeCaseFilter($fields)
114 {
115 return $this->convertKeysToSnakeCase($fields);
116 }
117
118 public function convertKeysToSnakeCaseOrder($fields)
119 {
120 $result = [];
121
122 $converter = new Converter(Converter::VALUES | Converter::TO_UPPER);
123 $converterForKey = new Converter(Converter::KEYS | Converter::TO_SNAKE | Converter::TO_UPPER | Converter::TO_SNAKE_DIGIT);
124
125 foreach ($converter->process($fields) as $key=>$value)
126 {
127 $result[$converterForKey->process($key)] = $value;
128 }
129 return $this->converterKeysProcessOnAfter($result);
130 }
131
132 public function convertKeysToSnakeCaseArguments($name, $arguments)
133 {
134 return $arguments;
135 }
136
137 protected function convertKeysToSnakeCase($data): array
138 {
139 $converter = new Converter(Converter::KEYS | Converter::RECURSIVE | Converter::TO_SNAKE | Converter::TO_UPPER | Converter::TO_SNAKE_DIGIT);
140 $items = $converter->process($data);
141 return $this->converterKeysProcessOnAfter($items);
142 }
143
144 private function converterKeysProcessOnAfter($items): array
145 {
146 $result = [];
147 foreach ($items as $key=>$item)
148 {
149 $result[$this->resolveFieldName($key)] = $item;
150 }
151 return $result;
152 }
153
154 private function converterValuesProcessOnAfter($items): array
155 {
156 $result = [];
157 foreach ($items as $key=>$item)
158 {
159 $result[$key] = $this->resolveFieldName($item);
160 }
161 return $result;
162 }
163
164 private function resolveFieldName($name)
165 {
166 return ($name === 'ID_1_C') ? 'ID_1C':$name;
167 }
168 //endregion
169
170 //region internalize fields
177 public function internalizeArguments($name, $arguments)
178 {
179 throw new NotImplementedException('Internalize arguments. The method '.$name.' is not implemented.');
180 }
181
182 public function internalizeFieldsList($arguments)
183 {
184 $fieldsInfo = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
185
186 $filter = isset($arguments['filter']) ? $this->internalizeFilterFields($arguments['filter'], $fieldsInfo):[];
187 $select = isset($arguments['select']) ? $this->internalizeSelectFields($arguments['select'], $fieldsInfo):[];
188 $order = isset($arguments['order']) ? $this->internalizeOrderFields($arguments['order'], $fieldsInfo):[];
189
190 return [
191 'filter'=>$filter,
192 'select'=>$select,
193 'order'=>$order,
194 ];
195 }
196
197 public function internalizeFieldsAdd($fields)
198 {
199 return $this->internalizeFields(
200 $fields,
201 $this->getListFieldInfo(
202 $this->getFields(),
203 ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]
204 )
205 );
206 }
207
208 public function internalizeFieldsUpdate($fields)
209 {
210 return $this->internalizeFields(
211 $fields,
212 $this->getListFieldInfo(
213 $this->getFields(),
214 ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]]
215 )
216 );
217 }
218
225 public function internalizeFieldsModify($fields)
226 {
227 throw new NotImplementedException('The method internalizeFieldsModify is not implemented.');
228 }
229
230 public function internalizeFieldsTryAdd($fields)
231 {
232 return $this->internalizeFieldsAdd($fields);
233 }
234
235 public function internalizeFieldsTryUpdate($fields)
236 {
237 return $this->internalizeFieldsUpdate($fields);
238 }
239
240 public function internalizeFieldsTryModify($fields)
241 {
242 return $this->internalizeFieldsModify($fields);
243 }
244
245 protected function internalizeFieldValue($value, $info)
246 {
247 $result = new Result();
248
249 $type = $info['TYPE'] ?? '';
250
251 if($type === self::TYPE_DATE || $type === self::TYPE_DATETIME)
252 {
253 if($value === '')
254 {
255 $date = '';
256 }
257 else
258 {
259 $time = strtotime($value);
260 $date = ($time) ? \Bitrix\Main\Type\DateTime::createFromTimestamp($time):'';
261 }
262
263 if($date instanceof Date)
264 {
265 $value = $date;
266 }
267 else
268 {
269 $result->addError(new Error('internalize data field error', 0));
270 }
271 }
272 elseif($type === self::TYPE_FILE)
273 {
274 //InternalizeFileField()
275 }
276 $result->addData([$value]);
277
278 return $result;
279 }
280
281 protected function internalizeFields($fields, array $fieldsInfo)
282 {
283 $result = [];
284
285 foreach ($fields as $name=>$value)
286 {
287 $info = isset($fieldsInfo[$name]) ? $fieldsInfo[$name]:null;
288 if(!$info)
289 {
290 continue;
291 }
292
293 $r = $this->internalizeFieldValue($value, $info);
294 if($r->isSuccess())
295 {
296 $value = current($r->getData());
297 }
298 else
299 {
300 continue;
301 }
302
303 $result[$name] = $value;
304 }
305 return $result;
306 }
307
308 protected function internalizeFilterFields($fields, array $fieldsInfo)
309 {
310 $result = [];
311
312 $fieldsInfo = empty($fieldsInfo)? $this->getFields():$fieldsInfo;
313
314 if(is_array($fields) && count($fields)>0)
315 {
316 $listFieldsInfo = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
317
318 foreach ($fields as $rawName=>$value)
319 {
320 $field = \CSqlUtil::GetFilterOperation($rawName);
321
322 $info = isset($listFieldsInfo[$field['FIELD']]) ? $listFieldsInfo[$field['FIELD']]:null;
323 if(!$info)
324 {
325 continue;
326 }
327
328 $r = $this->internalizeFieldValue($value, $info);
329 if($r->isSuccess())
330 {
331 $value = current($r->getData());
332 }
333 else
334 {
335 continue;
336 }
337
338 $operation = mb_substr($rawName, 0, mb_strlen($rawName) - mb_strlen($field['FIELD']));
339 if(isset($info['FORBIDDEN_FILTERS'])
340 && is_array($info['FORBIDDEN_FILTERS'])
341 && in_array($operation, $info['FORBIDDEN_FILTERS'], true))
342 {
343 continue;
344 }
345
346 $result[$rawName]=$value;
347 }
348 }
349 return $result;
350 }
351
352 protected function internalizeSelectFields($fields, array $fieldsInfo)
353 {
354 $result = [];
355
356 $fieldsInfo = empty($fieldsInfo)? $this->getFields():$fieldsInfo;
357
358 $listFieldsInfo = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
359
360 if(empty($fields) || in_array('*', $fields, true))
361 {
362 $result = array_keys($listFieldsInfo);
363 }
364 else
365 {
366 foreach ($fields as $name)
367 {
368 $info = isset($listFieldsInfo[$name]) ? $listFieldsInfo[$name]:null;
369 if(!$info)
370 {
371 continue;
372 }
373
374 $result[] = $name;
375 }
376 }
377
378 return $result;
379 }
380
381 protected function internalizeOrderFields($fields, array $fieldsInfo)
382 {
383 $result = [];
384
385 $fieldsInfo = empty($fieldsInfo)? $this->getFields():$fieldsInfo;
386
387 if(is_array($fields)
388 && count($fields)>0)
389 {
390 $listFieldsInfo = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
391
392 foreach ($fields as $field=>$order)
393 {
394 $info = isset($listFieldsInfo[$field]) ? $listFieldsInfo[$field]:null;
395 if(!$info)
396 {
397 continue;
398 }
399
400 $result[$field]=$order;
401 }
402 }
403
404 return $result;
405 }
406
407 public function rewriteFieldsList($arguments)
408 {
409 $filter = isset($arguments['filter']) ? $this->rewriteFilterFields($arguments['filter']):[];
410 $select = isset($arguments['select']) ? $this->rewriteSelectFields($arguments['select']):[];
411 $order = isset($arguments['order']) ? $this->rewriteOrderFields($arguments['order']):[];
412
413 return [
414 'filter'=>$filter,
415 'select'=>$select,
416 'order'=>$order,
417 ];
418 }
419
420 protected function rewriteSelectFields($fields)
421 {
422 $result = [];
423 $rewriteFields = $this->getRewritedFields();
424
425 foreach ($fields as $name)
426 {
427 $fieldsIsAlias = isset($rewriteFields[$name]);
428
429 if($fieldsIsAlias)
430 {
431 if(isset($rewriteFields[$name]['REFERENCE_FIELD']))
432 {
433 $result[$name] = $rewriteFields[$name]['REFERENCE_FIELD'];
434 }
435 }
436 else
437 {
438 $result[] = $name;
439 }
440 }
441
442 return $result;
443 }
444
445 protected function rewriteFilterFields($fields)
446 {
447 $result = [];
448 $rewriteFields = $this->getRewritedFields();
449
450
451 foreach ($fields as $rawName=>$value)
452 {
453 $field = \CSqlUtil::GetFilterOperation($rawName);
454
455 $fieldsIsAlias = isset($rewriteFields[$field['FIELD']]);
456
457 if($fieldsIsAlias)
458 {
459 if(isset($rewriteFields[$field['FIELD']]['REFERENCE_FIELD']))
460 {
461 $originalName = $rewriteFields[$field['FIELD']]['REFERENCE_FIELD'];
462 $operation = mb_substr($rawName, 0, mb_strlen($rawName) - mb_strlen($field['FIELD']));
463 $result[$operation.$originalName] = $value;
464 }
465 }
466 else
467 {
468 $result[$rawName] = $value;
469 }
470 }
471
472 return $result;
473 }
474
475 protected function rewriteOrderFields($fields)
476 {
477 $result = [];
478 $rewriteFields = $this->getRewritedFields();
479
480 foreach ($fields as $name=>$value)
481 {
482 $fieldsIsAlias = isset($rewriteFields[$name]);
483
484 if($fieldsIsAlias)
485 {
486 if(isset($rewriteFields[$name]['REFERENCE_FIELD']))
487 {
488 $result[$rewriteFields[$name]['REFERENCE_FIELD']] = $value;
489 }
490 }
491 else
492 {
493 $result[$name] = $value;
494 }
495 }
496
497 return $result;
498 }
499
504 protected function getRewritedFields()
505 {
506 return [];
507 }
508
509 protected function internalizeListFields($list, $fieldsInfo=[])
510 {
511 $result = [];
512
513 $fieldsInfo = empty($fieldsInfo) ? $this->getFields():$fieldsInfo;
514
515 $listFieldsInfo = $this->getListFieldInfo($fieldsInfo, ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
516
517 if(is_array($list) && count($list)>0)
518 {
519 foreach ($list as $k=>$item)
520 {
521 $result[$k] = $this->internalizeFields($item, $listFieldsInfo);
522 }
523 }
524 return $result;
525 }
526
527 //endregion
528
529 // region externalize fields
530 public function externalizeFields($fields)
531 {
532 $result = [];
533 $fieldsInfo = $this->getListFieldInfo($this->getFields(), ['filter'=>['ignoredAttributes'=>[Attributes::Hidden]]]);
534
535 if(is_array($fields) && count($fields)>0)
536 {
537 foreach($fields as $name => $value)
538 {
539 $info = isset($fieldsInfo[$name]) ? $fieldsInfo[$name] : null;
540 if(!$info)
541 {
542 continue;
543 }
544
545 /*$attributes = isset($info['ATTRIBUTES']) ? $info['ATTRIBUTES']:[];
546 $skipAttr = in_array(Attributes::Hidden, $attributes, true);
547
548 if($skipAttr)
549 {
550 continue;
551 }*/
552
553 $type = isset($info['TYPE']) ? $info['TYPE']:'';
554 if($type === self::TYPE_FILE)
555 {
556 //externalizeFileField()
557 }
558
559 $result[$name] = $value;
560 }
561 }
562 return $result;
563 }
564
565 public function externalizeListFields($list)
566 {
567 $result = [];
568 if(is_array($list) && count($list)>0)
569 {
570 foreach($list as $k=>$fields)
571 $result[$k] = $this->externalizeFields($fields);
572 }
573 return $result;
574 }
575
581 protected function externalizeFieldsModify($fields)
582 {
583 throw new NotImplementedException('The method externalizeFieldsModify is not implemented.');
584 }
585
586 public function externalizeFieldsTryModify($fields)
587 {
588 return $this->externalizeFieldsModify($fields);
589 }
590
597 public function externalizeResult($name, $fields)
598 {
599 throw new NotImplementedException('Externalize result. The method '.$name.' is not implemented.');
600 }
601 // endregion
602
603 //region convert keys to camel case
604 public function convertKeysToCamelCase($fields)
605 {
606 return Converter::toJson()
607 ->process($fields);
608 }
609 // endregion
610
611 //region check fields
612 public function checkFieldsAdd($fields)
613 {
614 $r = new Result();
615
616 $required = $this->checkRequiredFieldsAdd($fields);
617 if(!$required->isSuccess())
618 $r->addError(new Error('Required fields: '.implode(', ', $required->getErrorMessages())));
619
620 return $r;
621 }
622
623 public function checkFieldsUpdate($fields)
624 {
625 $r = new Result();
626
627 $required = $this->checkRequiredFieldsUpdate($fields);
628 if(!$required->isSuccess())
629 $r->addError(new Error('Required fields: '.implode(', ', $required->getErrorMessages())));
630
631 return $r;
632 }
633
634 public function checkFieldsModify($fields)
635 {
636 $r = new Result();
637
638 $required = $this->checkRequiredFieldsModify($fields);
639 if(!$required->isSuccess())
640 $r->addError(new Error('Required fields: '.implode(' ', $required->getErrorMessages())));
641
642 return $r;
643 }
644
645 public function checkArguments($name, $arguments)
646 {
647 return new Result();
648 }
649
650 protected function checkRequiredFieldsAdd($fields)
651 {
652 return $this->checkRequiredFields($fields, $this->getListFieldInfo(
653 $this->getFields(),
654 ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly]]]
655 ));
656 }
657
658 protected function checkRequiredFieldsUpdate($fields)
659 {
660 return $this->checkRequiredFields($fields, $this->getListFieldInfo(
661 $this->getFields(),
662 ['filter'=>['ignoredAttributes'=>[Attributes::Hidden, Attributes::ReadOnly, Attributes::Immutable]]]
663 ));
664 }
665
671 protected function checkRequiredFieldsModify($fields)
672 {
673 throw new NotImplementedException('The method checkFieldsModify is not implemented.');
674 }
675
676 protected function checkRequiredFields($fields, array $fieldsInfo, $params=[])
677 {
678 $r = new Result();
679
680 $addRequiredFields = is_set($params, '+required') ? $params['+required']:[];
681 $delRequiredFields = is_set($params, '-required') ? $params['-required']:[];
682
683 foreach ($this->prepareFieldInfos($fieldsInfo) as $name=>$info)
684 {
685 if(in_array($name, $delRequiredFields))
686 {
687 continue;
688 }
689 elseif($info['IS_REQUIRED'] == 'Y' || in_array($name, $addRequiredFields))
690 {
691 if(!is_set($fields, $name))
692 $r->addError(new Error($this->convertKeysToCamelCase($name)));
693 }
694 }
695
696 return $r;
697 }
698 //endregion
699}
static createFromTimestamp($timestamp)
Definition datetime.php:246
checkRequiredFieldsAdd($fields)
Definition base.php:650
rewriteFieldsList($arguments)
Definition base.php:407
internalizeFieldValue($value, $info)
Definition base.php:245
internalizeFieldsTryUpdate($fields)
Definition base.php:235
internalizeFieldsTryModify($fields)
Definition base.php:240
internalizeSelectFields($fields, array $fieldsInfo)
Definition base.php:352
internalizeArguments($name, $arguments)
Definition base.php:177
convertKeysToCamelCase($fields)
Definition base.php:604
getListFieldInfo(array $fieldsInfo, $params=[])
Definition base.php:62
internalizeFilterFields($fields, array $fieldsInfo)
Definition base.php:308
externalizeFieldsModify($fields)
Definition base.php:581
externalizeFieldsTryModify($fields)
Definition base.php:586
convertKeysToSnakeCaseOrder($fields)
Definition base.php:118
convertKeysToSnakeCaseArguments($name, $arguments)
Definition base.php:132
checkArguments($name, $arguments)
Definition base.php:645
internalizeFieldsTryAdd($fields)
Definition base.php:230
internalizeOrderFields($fields, array $fieldsInfo)
Definition base.php:381
internalizeFieldsUpdate($fields)
Definition base.php:208
convertKeysToSnakeCaseSelect($fields)
Definition base.php:106
checkRequiredFieldsUpdate($fields)
Definition base.php:658
internalizeFieldsList($arguments)
Definition base.php:182
externalizeResult($name, $fields)
Definition base.php:597
checkRequiredFieldsModify($fields)
Definition base.php:671
internalizeFields($fields, array $fieldsInfo)
Definition base.php:281
internalizeListFields($list, $fieldsInfo=[])
Definition base.php:509
convertKeysToSnakeCaseFields($fields)
Definition base.php:101
convertKeysToSnakeCaseFilter($fields)
Definition base.php:113
checkRequiredFields($fields, array $fieldsInfo, $params=[])
Definition base.php:676
internalizeFieldsModify($fields)
Definition base.php:225