Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fabric.php
1<?php
16class Fabric
17{
18 protected static $defaultFunctionMap = [
19 'upper' => FunctionUpper::class,
20 'lower' => FunctionLower::class,
21 'translit' => FunctionTranslit::class,
22 'concat' => FunctionConcat::class,
23 'limit' => FunctionLimit::class,
24 'contrast' => FunctionContrast::class,
25 'min' => FunctionMin::class,
26 'max' => FunctionMax::class,
27 'distinct' => FunctionDistinct::class,
28 'ucfirst' => FunctionUcfirst::class,
29 'ucwords' => FunctionUcwords::class,
30 ];
31
32 protected static $functionMap = [];
41 public static function createInstance($functionName, $data = null) //todo rename createInstance
42 {
43 if (!is_string($functionName))
44 {
45 return new FunctionBase($data);
46 }
47 if (isset(self::$defaultFunctionMap[$functionName]))
48 {
50 $functionClass = self::$defaultFunctionMap[$functionName];
51 return new $functionClass($data);
52 }
53 elseif (isset(self::$functionMap[$functionName]))
54 {
56 $functionClass = self::$functionMap[$functionName];
57 return new $functionClass($data);
58 }
59 else
60 {
61 $event = new \Bitrix\Main\Event("iblock", "OnTemplateGetFunctionClass", array($functionName));
62 $event->send();
63 if ($event->getResults())
64 {
65 foreach($event->getResults() as $evenResult)
66 {
67 if($evenResult->getType() == \Bitrix\Main\EventResult::SUCCESS)
68 {
69 $functionClass = $evenResult->getParameters();
70 if (is_string($functionClass) && class_exists($functionClass))
71 {
72 self::$functionMap[$functionName] = $functionClass;
73 }
74 break;
75 }
76 }
77 }
78 if (isset(self::$functionMap[$functionName]))
79 {
80 $functionClass = self::$functionMap[$functionName];
81 return new $functionClass($data);
82 }
83 }
84
85 return new FunctionBase($data);
86 }
87}
88
96{
97 protected $data = null;
98
102 function __construct($data = null)
103 {
104 $this->data = $data;
105 }
114 public function onPrepareParameters(\Bitrix\Iblock\Template\Entity\Base $entity, array $parameters)
115 {
116 $arguments = array();
118 foreach ($parameters as $parameter)
119 {
120 $arguments[] = $parameter->process($entity);
121 }
122 return $arguments;
123 }
124
132 public function calculate(array $parameters)
133 {
134 return "";
135 }
136
144 protected function parametersToString(array $parameters)
145 {
146 $result = array();
147 foreach ($parameters as $param)
148 {
149 if (is_array($param))
150 $result[] = implode(" ", $param);
151 elseif ($param != "")
152 $result[] = $param;
153 }
154 return implode(" ", $result);
155 }
156
164 protected function parametersToArray(array $parameters)
165 {
166 $result = array();
167 foreach ($parameters as $param)
168 {
169 if (is_array($param))
170 {
171 foreach ($param as $p)
172 $result[] = $p;
173 }
174 elseif ($param != "")
175 {
176 $result[] = $param;
177 }
178 }
179 return $result;
180 }
181}
182
190{
198 public function calculate(array $parameters)
199 {
200 return toUpper($this->parametersToString($parameters));
201 }
202}
203
211{
219 public function calculate(array $parameters)
220 {
221 return toLower($this->parametersToString($parameters));
222 }
223}
224
232{
240 public function calculate(array $parameters)
241 {
242 $changeCase = false;
243 $replaceChar = "";
244
245 if (
246 isset($this->data)
247 && isset($this->data["replace_space"])
248 && $this->data["replace_space"] != ""
249 )
250 {
251 $changeCase = isset($this->data[""])? $this->data["change_case"]: false;
252 $replaceChar = $this->data["replace_space"];
253 }
254
255 if (
256 isset($this->data)
257 && isset($this->data["change_case"])
258 && $this->data["change_case"] != ""
259 )
260 {
261 $changeCase = $this->data["change_case"];
262 }
263
264
265 return \CUtil::translit($this->parametersToString($parameters), LANGUAGE_ID, array(
266 //"max_len" => 50,
267 "change_case" => $changeCase, // 'L' - toLower, 'U' - toUpper, false - do not change
268 "replace_space" => $replaceChar,
269 "replace_other" => $replaceChar,
270 "delete_repeat_replace" => true,
271 ));
272 }
273}
274
282{
290 public function calculate(array $parameters)
291 {
292 $result = $this->parametersToArray($parameters);
293 $delimiter = array_pop($result);
294
295 return implode($delimiter, $result);
296 }
297}
298
306{
314 public function calculate(array $parameters)
315 {
316 $result = $this->parametersToArray($parameters);
317 $limit = array_pop($result);
318 $delimiter = array_pop($result);
319 $text = implode(" ", $result);
320
321 $result = preg_split("/([".preg_quote($delimiter, "/")."]+)/", $text);
322 return array_slice($result, 0, $limit);
323 }
324}
325
333{
341 public function calculate(array $parameters)
342 {
343 $result = $this->parametersToArray($parameters);
344 $limit = array_pop($result);
345 $delimiter = array_pop($result);
346 $text = strip_tags(implode(" ", $result));
347
348 $words = array();
349 $result = preg_split("/([".preg_quote($delimiter, "/")."]+)/", $text);
350 if ($result)
351 {
352 foreach ($result as $word)
353 {
354 if (mb_strlen($word) > 1)
355 $words[$word]++;
356 }
357 $len = log(max(20, array_sum($words)));
358 foreach ($words as $word => $count)
359 {
360 $words[$word] = log($count + 1) / $len;
361 }
362 arsort($words);
363 }
364 return array_keys(array_slice($words, 0, $limit));
365 }
366}
367
375{
383 public function calculate(array $parameters)
384 {
385 $result = $this->parametersToArray($parameters);
386 $asFloat = [];
387 foreach ($result as $rawValue)
388 {
389 if (!isset($asFloat[$rawValue]))
390 {
391 $value = preg_replace("/&\#[0-9]+;/", '', $rawValue);
392 $floatFalue = (float)preg_replace("/[^0-9.]+/", "", $value);
393 $asFloat[$rawValue] = $floatFalue;
394 }
395 }
396
397 if (empty($asFloat))
398 {
399 return '';
400 }
401 elseif (count($asFloat) == 1)
402 {
403 return end($result);
404 }
405 else
406 {
407 $min = min($asFloat);
408
409 return array_search($min, $asFloat);
410 }
411 }
412}
413
421{
429 public function calculate(array $parameters)
430 {
431 $result = $this->parametersToArray($parameters);
432 $asFloat = [];
433 foreach ($result as $rawValue)
434 {
435 if (!isset($asFloat[$rawValue]))
436 {
437 $value = preg_replace("/&\#[0-9]+;/", '', $rawValue);
438 $floatFalue = (float)preg_replace("/[^0-9.]+/", '', $value);
439 $asFloat[$rawValue] = $floatFalue;
440 }
441 }
442 if (empty($asFloat))
443 {
444 return '';
445 }
446 elseif (count($asFloat) == 1)
447 {
448 return end($result);
449 }
450 else
451 {
452 $max = max($asFloat);
453
454 return array_search($max, $asFloat);
455 }
456 }
457}
458
466{
474 public function calculate(array $parameters)
475 {
476 $result = array();
477 foreach ($this->parametersToArray($parameters) as $value)
478 $result[$value] = $value;
479 return array_values($result);
480 }
481}