Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statistic.php
1<?php
2
3
5
6
13
14final class Statistic extends Base
15{
16
17 public function getFields()
18 {
19 return [
20 'ID'=>[
21 'TYPE'=>DataType::TYPE_INT,
22 'ATTRIBUTES'=>[
23 Attributes::READONLY
24 ]
25 ],
26 'ENTITY_TYPE_ID'=>[
27 'TYPE'=>DataType::TYPE_INT,
28 'ATTRIBUTES'=>[
29 Attributes::REQUIRED,
30 Attributes::IMMUTABLE
31 ]
32 ],
33 'ENTITY_ID'=>[
34 'TYPE'=>DataType::TYPE_INT,
35 'ATTRIBUTES'=>[
36 Attributes::REQUIRED,
37 Attributes::IMMUTABLE
38 ]
39 ],
40 'DATE_UPDATE'=>[
41 'TYPE'=>DataType::TYPE_DATETIME,
42 'ATTRIBUTES'=>[
43 Attributes::REQUIRED
44 ]
45 ],
46 'TIMESTAMP_X'=>[
47 'TYPE'=>DataType::TYPE_DATETIME,
48 'ATTRIBUTES'=>[
49 Attributes::READONLY
50 ]
51 ],
52 'PROVIDER_ID'=>[
53 'TYPE'=>DataType::TYPE_INT,
54 'ATTRIBUTES'=>[
55 Attributes::REQUIRED,
56 Attributes::IMMUTABLE
57 ]
58 ],
59 'CURRENCY'=>[
60 'TYPE'=>DataType::TYPE_STRING,
61 'ATTRIBUTES'=>[
62 Attributes::REQUIRED
63 ]
64 ],
65 'STATUS'=>[
66 'TYPE'=>DataType::TYPE_STRING,
67 'ATTRIBUTES'=>[
68 Attributes::REQUIRED
69 ]
70 ],
71 'XML_ID'=>[
72 'TYPE'=>DataType::TYPE_STRING,
73 'ATTRIBUTES'=>[
74 Attributes::REQUIRED
75 ]
76 ],
77 'AMOUNT'=>[
78 'TYPE'=>DataType::TYPE_FLOAT,
79 'ATTRIBUTES'=>[
80 Attributes::REQUIRED
81 ]
82 ]
83 ];
84 }
85
86 public function convertKeysToSnakeCaseArguments($name, $arguments)
87 {
88 if($name == 'upsert')
89 {
90 if(isset($arguments['fields']))
91 {
92 $fields = $arguments['fields'];
93 if(!empty($fields))
94 {
95 $arguments['fields'] = $this->convertKeysToSnakeCaseFields($fields);
96 }
97 }
98 }
99 elseif ($name == 'modify')
100 {
101 $fields = $arguments['fields'];
102 $arguments['fields'] = $this->convertKeysToSnakeCaseFieldsModify($fields);
103 }
104 else
105 {
106 throw new NotImplementedException('Convert arguments. The method '.$name.' is not implemented.');
107 }
108
109 return $arguments;
110 }
111
112 protected function convertKeysToSnakeCaseFieldsModify($fields)
113 {
114 $fields = $this->convertKeysToSnakeCaseFields($fields);
115
116 return $fields;
117 }
118
119 public function checkArguments($name, $arguments): \Bitrix\Main\Result
120 {
121 $r = new Result();
122
123 if($name == 'upsert')
124 {
125 $r = $this->checkFieldsAdd($arguments['fields']);
126 }
127 elseif ($name == 'modify')
128 {
129 $required = $this->checkFieldsModify($arguments['fields']);
130
131 if($required->isSuccess() === false)
132 {
133 $r->addError(new Error(implode(', ', $required->getErrorMessages())));
134 }
135 }
136 else
137 {
138 throw new NotImplementedException('Check arguments. The method '.$name.' is not implemented.');
139 }
140 return $r;
141 }
142
143 protected function checkFieldsModify($fields)
144 {
145 $r = new Result();
146
147 $emptyFields = [];
148 if(!isset($fields['PROVIDER']['ID']))
149 {
150 $emptyFields[] = '[provider][id]';
151 }
152 if(!isset($fields['STATISTICS']) || !is_array($fields['STATISTICS']))
153 {
154 $emptyFields[] = '[statistics][]';
155 }
156
157 if(count($emptyFields)>0)
158 {
159 $r->addError(new Error('Required fields: '.implode(', ', $emptyFields)));
160 }
161 else
162 {
163 foreach ($fields['STATISTICS'] as $k=>$fields)
164 {
165 $required = $this->checkFieldsAdd($fields);
166 if($required->isSuccess() === false)
167 {
168 $r->addError(new Error('[fields][statistics]['.$k.'] - '.implode(', ', $required->getErrorMessages()).'.'));
169 }
170 }
171 }
172
173 return $r;
174 }
175
176 public function internalizeArguments($name, $arguments): array
177 {
178 if($name == 'upsert')
179 {
180 $fields = $arguments['fields'];
181 if(!empty($fields))
182 {
183 $arguments['fields'] = $this->internalizeFieldsAdd($fields);
184 }
185 }
186 elseif ($name == 'modify')
187 {
188 $fields = $arguments['fields'];
189 $arguments['fields'] = $this->internalizeFieldsModify($fields);
190 }
191 else
192 {
193 parent::internalizeArguments($name, $arguments);
194 }
195
196 return $arguments;
197 }
198
199 protected function internalizeFieldsModify($fields)
200 {
201 $result = [];
202
203 $result['PROVIDER']['ID'] = (int)$fields['PROVIDER']['ID'];
204
205 foreach ($fields['STATISTICS'] as $k=>$statistic)
206 {
207 $result['STATISTICS'][$k] = $this->internalizeFieldsAdd($statistic);
208 }
209
210 return $result;
211 }
212
213 public function externalizeResult($name, $fields): array
214 {
215 if($name == 'upsert')
216 {
217 return $this->externalizeFieldsGet($fields);
218 }
219 elseif ($name == 'modify')
220 {
221 $provider = new StatisticProvider();
222 $fields['PROVIDER'] = $this->externalizeFieldsGet($fields['PROVIDER'], $provider->getFields());
223 }
224 else
225 {
226 parent::externalizeResult($name, $fields);
227 }
228
229 return $fields;
230 }
231}
internalizeFieldsAdd($fields, $fieldsInfo=[])
Definition base.php:153
externalizeFieldsGet($fields, $fieldsInfo=[])
Definition base.php:702