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