Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
internalizer.php
1<?php
2
3
4namespace Bitrix\Sale\Rest;
5
6
10
12{
13 public function __construct($name, $arguments, $controller, $data = [], $scope = '')
14 {
15 $this->setFormat([
16 self::TO_WHITE_LIST,
17 self::TO_SNAKE,
18 self::CHECK_REQUIRED
19 ]);
20
21 parent::__construct($name, $arguments, $controller, $data, $scope);
22 }
23
27 public function process()
28 {
29 $r = new Result();
30
31 $arguments = $this->getArguments();
32
33 if(in_array(self::TO_SNAKE, $this->format))
34 {
35 $arguments = $this->convertToSnakeCase($arguments);
36 }
37
38 if(in_array(self::CHECK_REQUIRED, $this->format))
39 {
40 $check = $this->check($arguments);
41 if(!$check->isSuccess())
42 {
43 $r->addErrors($check->getErrors());
44 }
45 }
46
47 if($r->isSuccess())
48 {
49 if(in_array(self::TO_WHITE_LIST, $this->format))
50 {
51 $arguments = $this->internalize($arguments);
52 }
53 }
54
55
56 return $r->setData(['data'=>$r->isSuccess()?$arguments:null]);
57 }
58
59 protected function convertToSnakeCase($arguments=[])
60 {
61 $name = $this->getName();
63 $controller = $this->getController();
64 $entity = $this->getEntity($controller);
65
66 if ($name == 'list')
67 {
68 if(isset($arguments['select']))
69 {
70 $fields = $arguments['select'];
71 if(!empty($fields))
72 $arguments['select'] = $entity->convertKeysToSnakeCaseSelect($fields);
73 }
74
75 if(isset($arguments['filter']))
76 {
77 $fields = $arguments['filter'];
78 if(!empty($fields))
79 $arguments['filter'] = $entity->convertKeysToSnakeCaseFilter($fields);
80 }
81
82 if(isset($arguments['order']))
83 {
84 $fields = $arguments['order'];
85 if(!empty($fields))
86 $arguments['order'] = $entity->convertKeysToSnakeCaseOrder($fields);
87 }
88 }
89 elseif ($name == 'getfields'){}
90 elseif ($name == 'get'){}
91 elseif ($name == 'delete'){}
92 elseif ($name == 'modify'
93 || $name == 'add'
94 || $name == 'update'
95 || $name == 'tryadd'
96 || $name == 'tryupdate'
97 || $name == 'trymodify')
98 {
99 if(isset($arguments['fields']))
100 {
101 $fields = $arguments['fields'];
102 if(!empty($fields))
103 $arguments['fields'] = $entity->convertKeysToSnakeCaseFields($fields);
104 }
105 }
106 else
107 {
108 $arguments = $entity->convertKeysToSnakeCaseArguments($name, $arguments);
109 }
110
111 return $arguments;
112 }
113
114 private function internalize($arguments)
115 {
116 $name = $this->getName();
118 $controller = $this->getController();
119 $entity = $this->getEntity($controller);
120
121 if($name == 'add')
122 {
123 $fields = $arguments['fields'];
124 if(!empty($fields))
125 $arguments['fields'] = $entity->internalizeFieldsAdd($fields);
126 }
127 elseif ($name == 'update')
128 {
129 $fields = $arguments['fields'];
130 if(!empty($fields))
131 $arguments['fields'] = $entity->internalizeFieldsUpdate($fields);
132 }
133 elseif ($name == 'list')
134 {
135 $fields = $entity->internalizeFieldsList([
136 'select'=>$arguments['select'],
137 'filter'=>$arguments['filter'],
138 'order'=>$arguments['order'],
139 ]);
140
141 $fields = $entity->rewriteFieldsList([
142 'select'=>$fields['select'],
143 'filter'=>$fields['filter'],
144 'order'=>$fields['order'],
145 ]);
146
147 $arguments['select'] = $fields['select'];
148 $arguments['filter'] = $fields['filter'];
149 $arguments['order'] = $fields['order'];
150 }
151 elseif ($name == 'getfields'){}
152 elseif ($name == 'get'){}
153 elseif ($name == 'delete'){}
154 elseif ($name == 'modify')
155 {
156 $fields = $arguments['fields'];
157 if(!empty($fields))
158 $arguments['fields'] = $entity->internalizeFieldsModify($fields);
159 }
160 elseif ($name == 'tryadd')
161 {
162 $fields = $arguments['fields'];
163 if(!empty($fields))
164 $arguments['fields'] = $entity->internalizeFieldsTryAdd($fields);
165 }
166 elseif ($name == 'tryupdate')
167 {
168 $fields = $arguments['fields'];
169 if(!empty($fields))
170 $arguments['fields'] = $entity->internalizeFieldsUpdate($fields);
171 }
172 elseif ($name == 'trymodify')
173 {
174 $fields = $arguments['fields'];
175 if(!empty($fields))
176 $arguments['fields'] = $entity->internalizeFieldsTryModify($fields);
177 }
178 else
179 {
180 $arguments = $entity->internalizeArguments($name, $arguments);
181 }
182
183 return $arguments;
184 }
185
186 protected function check($arguments)
187 {
188 $r = new Result();
189
190 $name = $this->getName();
192 $controller = $this->getController();
193 $entity = $this->getEntity($controller);
194
195 if($name == 'add')
196 {
197 $r = $entity->checkFieldsAdd($arguments['fields']);
198 }
199 elseif ($name == 'update')
200 {
201 $r = $entity->checkFieldsUpdate($arguments['fields']);
202 }
203 elseif ($name == 'list'){}
204 elseif ($name == 'getfields'){}
205 elseif ($name == 'get'){}
206 elseif ($name == 'delete'){}
207 elseif ($name == 'modify')
208 {
209 $r = $entity->checkFieldsModify($arguments['fields']);
210 }
211 elseif ($name == 'tryadd')
212 {
213 $r = $entity->checkFieldsAdd($arguments['fields']);
214 }
215 elseif ($name == 'tryupdate')
216 {
217 $r = $entity->checkFieldsUpdate($arguments['fields']);
218 }
219 elseif ($name == 'trymodify')
220 {
221 $r = $entity->checkFieldsModify($arguments['fields']);
222 }
223 else
224 {
225 $r = $entity->checkArguments($name, $arguments);
226 }
227
228 return $r;
229 }
230}
__construct($name, $arguments, $controller, $data=[], $scope='')