Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
formatter.php
1<?php
3
5
6Loc::loadMessages(__FILE__);
7
8final class Formatter
9{
10 const TYPE_SIMPLE = 0x0001;
11 const TYPE_VALUE = 0x0002;
12 const TYPE_LIMIT_VALUE = 0x0004;
13 const TYPE_FIXED = 0x0008;
14 const TYPE_MAX_BOUND = 0x0010;
15 const TYPE_SIMPLE_GIFT = 0x0020;
16
17 const VALUE_TYPE_PERCENT = 'P';
19 const VALUE_TYPE_SUMM = 'S';
21
23 const VALUE_ACTION_EXTRA = 'E';
25
26 const LIMIT_MAX = 'MAX';
27 const LIMIT_MIN = 'MIN';
28
29 private static $errors = array();
30
38 public static function prepareRow($type, $data)
39 {
40 self::clearErrors();
41 $process = true;
42 $result = array();
43
44 $type = (int)$type;
45
46 if ($type != self::TYPE_SIMPLE)
47 {
48 if (empty($data) || !is_array($data))
49 {
50 $process = false;
51 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
52 }
53 }
54 switch ($type)
55 {
56 case self::TYPE_SIMPLE:
57 if (empty($data) || !is_string($data))
58 {
59 $process = false;
60 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
61 }
62 if ($process)
63 {
64 $result = array(
65 'TYPE' => self::TYPE_SIMPLE,
66 'DESCR' => $data
67 );
68 }
69 break;
70 case self::TYPE_LIMIT_VALUE:
71 case self::TYPE_VALUE:
72 if ($type == self::TYPE_LIMIT_VALUE)
73 {
74 if ($process)
75 {
76 if (!isset($data['LIMIT_TYPE']) || !isset($data['LIMIT_VALUE']) || !isset($data['LIMIT_UNIT']))
77 {
78 $process = false;
79 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
80 }
81 elseif ($data['LIMIT_TYPE'] != self::LIMIT_MAX && $data['LIMIT_TYPE'] != self::LIMIT_MIN)
82 {
83 $process = false;
84 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
85 }
86 }
87 if ($process)
88 {
89 if ($data['VALUE_TYPE'] != self::VALUE_TYPE_PERCENT)
90 {
91 $process = false;
92 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
93 }
94 }
95 if ($process)
96 {
97 $result['LIMIT_TYPE'] = $data['LIMIT_TYPE'];
98 $result['LIMIT_VALUE'] = $data['LIMIT_VALUE'];
99 $result['LIMIT_UNIT'] = $data['LIMIT_UNIT'];
100 }
101 }
102 if ($process)
103 {
104 if (!isset($data['VALUE']) || !isset($data['VALUE_TYPE']))
105 {
106 $process = false;
107 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
108 }
109 }
110 if ($process)
111 {
112 if (
113 $data['VALUE_TYPE'] != self::VALUE_TYPE_PERCENT
114 && $data['VALUE_TYPE'] != self::VALUE_TYPE_CURRENCY
115 && $data['VALUE_TYPE'] != self::VALUE_TYPE_SUMM
116 && $data['VALUE_TYPE'] != self::VALUE_TYPE_SUMM_BASKET
117 )
118 {
119 $process = false;
120 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
121 }
122 elseif (
123 $data['VALUE_TYPE'] == self::VALUE_TYPE_CURRENCY
124 || $data['VALUE_TYPE'] == self::VALUE_TYPE_SUMM
125 || $data['VALUE_TYPE'] == self::VALUE_TYPE_SUMM_BASKET
126 )
127 {
128 if (!isset($data['VALUE_UNIT']))
129 {
130 $process = false;
131 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
132 }
133 }
134 }
135 if ($process)
136 {
137 if (!isset($data['VALUE_ACTION']))
138 $data['VALUE_ACTION'] = self::VALUE_ACTION_DISCOUNT;
139 if (
140 $data['VALUE_ACTION'] != self::VALUE_ACTION_DISCOUNT
141 && $data['VALUE_ACTION'] != self::VALUE_ACTION_EXTRA
142 && $data['VALUE_ACTION'] != self::VALUE_ACTION_CUMULATIVE
143 )
144 {
145 $process = false;
146 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
147 }
148 }
149 if ($process)
150 {
151 $result['TYPE'] = $type;
152 $result['VALUE'] = $data['VALUE'];
153 $result['VALUE_TYPE'] = $data['VALUE_TYPE'];
154 $result['VALUE_ACTION'] = $data['VALUE_ACTION'];
155
156 if (
157 $data['VALUE_TYPE'] == self::VALUE_TYPE_CURRENCY
158 || $data['VALUE_TYPE'] == self::VALUE_TYPE_SUMM
159 || $data['VALUE_TYPE'] == self::VALUE_TYPE_SUMM_BASKET
160 )
161 $result['VALUE_UNIT'] = $data['VALUE_UNIT'];
162 if (isset($data['RESULT_VALUE']) && isset($data['RESULT_UNIT']))
163 {
164 $result['RESULT_VALUE'] = (string)$data['RESULT_VALUE'];
165 $result['RESULT_UNIT'] = $data['RESULT_UNIT'];
166 }
167 }
168 break;
169 case self::TYPE_FIXED:
170 if ($process)
171 {
172 if (!isset($data['VALUE']) || !isset($data['VALUE_UNIT']))
173 {
174 $process = false;
175 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
176 }
177 }
178 if ($process)
179 {
180 $result = array(
181 'TYPE' => $type,
182 'VALUE' => $data['VALUE'],
183 'VALUE_UNIT' => $data['VALUE_UNIT']
184 );
185 }
186 break;
187 case self::TYPE_MAX_BOUND:
188 if ($process)
189 {
190 if (!isset($data['VALUE']) || !isset($data['VALUE_UNIT']))
191 {
192 $process = false;
193 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
194 }
195 }
196 if ($process)
197 {
198 $result = array(
199 'TYPE' => $type,
200 'VALUE' => $data['VALUE'],
201 'VALUE_UNIT' => $data['VALUE_UNIT']
202 );
203 if (isset($data['RESULT_VALUE']) && isset($data['RESULT_UNIT']))
204 {
205 $result['RESULT_VALUE'] = (string)$data['RESULT_VALUE'];
206 $result['RESULT_UNIT'] = $data['RESULT_UNIT'];
207 }
208 }
209 break;
210 case self::TYPE_SIMPLE_GIFT:
211 $result = array(
212 'TYPE' => self::TYPE_SIMPLE_GIFT
213 );
214 break;
215 default:
216 $process = false;
217 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_DESCR_BAD'));
218 break;
219 }
220
221 return ($process ? $result : null);
222 }
223
230 public static function formatRow(array $action)
231 {
232 self::clearErrors();
233 $result = null;
234 if (!isset($action['TYPE']))
235 {
236 self::addError(Loc::getMessage('SALE_DISCOUNT_FORMATTER_ERR_FORMAT_TYPE_BAD'));
237 return $result;
238 }
239
240 switch ($action['TYPE'])
241 {
242 case self::TYPE_SIMPLE:
243 $result = $action['DESCR'];
244 break;
245 case self::TYPE_VALUE:
246 if ($action['VALUE_TYPE'] == self::VALUE_TYPE_PERCENT)
247 {
248 $value = $action['VALUE'].'%';
249 if (isset($action['RESULT_VALUE']) && isset($action['RESULT_UNIT']))
250 $value .= ' ('.\CCurrencyLang::CurrencyFormat($action['RESULT_VALUE'], $action['RESULT_UNIT'], true).')';
251 }
252 else
253 {
254 if ($action['VALUE_TYPE'] == self::VALUE_TYPE_CURRENCY)
255 {
256 $value = \CCurrencyLang::CurrencyFormat($action['VALUE'], $action['VALUE_UNIT'], true);
257 }
258 else
259 {
260 $subMessageID = (
261 $action['VALUE_TYPE'] == self::VALUE_TYPE_SUMM
262 ? 'SALE_DISCOUNT_FORMATTER_MESS_SUMM_FORMAT'
263 : 'SALE_DISCOUNT_FORMATTER_MESS_SUMM_BASKET_FORMAT'
264 );
265 $value = Loc::getMessage(
266 $subMessageID,
267 array('#VALUE#' => \CCurrencyLang::CurrencyFormat($action['VALUE'], $action['VALUE_UNIT'], true))
268 );
269 unset($subMessageID);
270 }
271 if (isset($action['RESULT_VALUE']) && isset($action['RESULT_UNIT']) && $action['VALUE_UNIT'] != $action['RESULT_UNIT'])
272 $value .= ' ('.\CCurrencyLang::CurrencyFormat($action['RESULT_VALUE'], $action['RESULT_UNIT'], true).')';
273 }
274 $messageId = 'SALE_DISCOUNT_FORMATTER_MESS_TYPE_DISCOUNT';
275 if (isset($action['VALUE_ACTION']))
276 {
277 switch ($action['VALUE_ACTION'])
278 {
279 case self::VALUE_ACTION_EXTRA:
280 $messageId = 'SALE_DISCOUNT_FORMATTER_MESS_TYPE_EXTRA';
281 break;
282 case self::VALUE_ACTION_CUMULATIVE:
283 $messageId = 'SALE_DISCOUNT_FORMATTER_MESS_TYPE_CUMULATIVE';
284 break;
285 }
286 }
287 $result = Loc::getMessage($messageId, array('#VALUE#' => $value));
288 unset($value, $messageId);
289 break;
290 case self::TYPE_LIMIT_VALUE:
291 $messageId = (
292 isset($action['LIMIT_TYPE']) && $action['LIMIT_TYPE'] == self::LIMIT_MIN
293 ? 'SALE_DISCOUNT_FORMATTER_MESS_LIMIT_MIN_FORMAT'
294 : 'SALE_DISCOUNT_FORMATTER_MESS_LIMIT_MAX_FORMAT'
295 );
296 $value = Loc::getMessage(
297 $messageId,
298 array(
299 '#PERCENT#' => $action['VALUE'].'%',
300 '#LIMIT#' => \CCurrencyLang::CurrencyFormat($action['LIMIT_VALUE'], $action['LIMIT_UNIT'], true)
301 )
302 );
303 if (isset($action['RESULT_VALUE']) && isset($action['RESULT_UNIT']))
304 $value .= ' ('.\CCurrencyLang::CurrencyFormat($action['RESULT_VALUE'], $action['RESULT_UNIT'], true).')';
305 $messageId = (
306 isset($action['VALUE_ACTION']) && $action['VALUE_ACTION'] == self::VALUE_ACTION_EXTRA
307 ? 'SALE_DISCOUNT_FORMATTER_MESS_TYPE_EXTRA'
308 : 'SALE_DISCOUNT_FORMATTER_MESS_TYPE_DISCOUNT'
309 );
310 $result = Loc::getMessage($messageId, array('#VALUE#' => $value));
311 unset($value, $messageId);
312 break;
313 case self::TYPE_FIXED:
314 $result = Loc::getMessage(
315 'SALE_DISCOUNT_FORMATTER_MESS_FIXED_FORMAT',
316 array('#VALUE#' => \CCurrencyLang::CurrencyFormat($action['VALUE'], $action['VALUE_UNIT'], true))
317 );
318 break;
319 case self::TYPE_MAX_BOUND:
320 $value = \CCurrencyLang::CurrencyFormat($action['VALUE'], $action['VALUE_UNIT'], true);
321 if (isset($action['RESULT_VALUE']) && isset($action['RESULT_UNIT']))
322 $value .= ' ('.\CCurrencyLang::CurrencyFormat($action['RESULT_VALUE'], $action['RESULT_UNIT'], true).')';
323 else
324 $value .= ' ('.$value.')';
325 $result = Loc::getMessage(
326 'SALE_DISCOUNT_FORMATTER_MESS_MAX_BOUND_FORMAT',
327 array('#VALUE#' => $value)
328 );
329 unset($value);
330 break;
331 case self::TYPE_SIMPLE_GIFT:
332 $result = Loc::getMessage('SALE_DISCOUNT_FORMATTER_MESS_SIMPLE_GIFT');
333 break;
334 default:
335 break;
336 }
337
338 return $result;
339 }
340
347 public static function formatList(array $actionList)
348 {
349 self::clearErrors();
350 $result = array();
351 if (!empty($actionList))
352 {
353 foreach ($actionList as $row)
354 {
355 if (!is_array($row))
356 return null;
357 $value = self::formatRow($row);
358 if ($value === null)
359 return null;
360 $result[] = $value;
361 }
362 unset($value, $row);
363 }
364 return (empty($result) ? null: $result);
365 }
366
372 public static function clearErrors()
373 {
374 self::$errors = array();
375 }
376
382 public static function getErrors()
383 {
384 return self::$errors;
385 }
386
393 private static function addError($error)
394 {
395 if ($error === '')
396 return;
397 self::$errors[] = $error;
398 }
399}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static prepareRow($type, $data)
Definition formatter.php:38
static formatList(array $actionList)
static formatRow(array $action)