Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
externalizer.php
1<?php
2
3
4namespace Bitrix\Sale\Rest;
5
6
11
18 implements Arrayable
19{
20 public function __construct($name, $arguments, $controller, $data = [], $scope = '')
21 {
22 $this->setFormat([
23 self::TO_WHITE_LIST,
24 self::TO_CAMEL,
25 self::SORTING_KEYS
26 ]);
27
28 parent::__construct($name, $arguments, $controller, $data, $scope);
29 }
30
31 public function process()
32 {
33 $r = new Result();
34
35 $data = $this->getData();
36 $id = $this->getIdList($data);
37
38 $data = $data[$id];
39 if(in_array(self::TO_WHITE_LIST, $this->format))
40 {
41 $data = $this->externalize($data);
42 }
43
44 if(in_array(self::TO_CAMEL, $this->format))
45 {
46 $data = $this->convertKeysToCamelCase([$id=>$data]);
47 }
48
49 if(in_array(self::SORTING_KEYS, $this->format))
50 {
52 }
53
54 return $r->setData(['data'=>$data]);
55 }
56
57 public function toArray()
58 {
59 return $this->process()->getData()['data'];
60 }
61
62 protected function multiSortKeysArray(array $data)
63 {
64 ksort($data);
65
66 foreach ($data as $k=>&$item)
67 {
68 if(is_array($item))
69 $item = $this->multiSortKeysArray($item);
70 }
71
72 return $data;
73 }
74
80 private function externalize($fields)
81 {
82 $name = $this->getName();
83 $controller = $this->getController();
84 $entity = $this->getEntity($controller);
85
86 if($name == 'getfields'){}
87 elseif($name == 'delete'){}
88 elseif($name == 'get'
89 || $name == 'add'
90 || $name == 'update'
91 || $name == 'tryadd'
92 || $name == 'tryupdate')
93 {
94 $fields = $entity->externalizeFields($fields);
95 }
96 elseif($name == 'list')
97 {
98 $fields = $entity->externalizeListFields($fields);
99 }
100 elseif($name == 'modify')
101 {
102 $fields = $entity->externalizeFieldsModify($fields);
103 }
104 elseif($name == 'trymodify')
105 {
106 $fields = $entity->externalizeFieldsTryModify($fields);
107 }
108 else
109 {
110 $fields = $entity->externalizeResult($name, $fields);
111 }
112
113 return $fields;
114 }
115
121 private function getIdList($data)
122 {
123 return key($data);
124 }
125
126 protected function convertKeysToCamelCase($fields)
127 {
128 $controller = $this->getController();
129 $entity = $this->getEntity($controller);
130
131 return $entity->convertKeysToCamelCase($fields);
132 }
133
134 public function getPage(Page $page)
135 {
136 $id = $this->convertKeysToCamelCase($page->getId());
137 return new Page($id, $this->toArray()[$id], $page->getTotalCount());
138 }
139}
__construct($name, $arguments, $controller, $data=[], $scope='')