Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ArrayLib.php
1<?php
2
4
7
8class ArrayLib extends BaseLib
9{
10 public function getFunctions(): array
11 {
12 return [
13 'implode' => [
14 'args' => true,
15 'func' => 'callImplode',
16 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_IMPLODE_DESCRIPTION'),
17 ],
18 'explode' => [
19 'args' => true,
20 'func' => 'callExplode',
21 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_EXPLODE_DESCRIPTION'),
22 ],
23 'merge' => [
24 'args' => true,
25 'func' => 'callMerge',
26 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_MERGE_DESCRIPTION'),
27 ],
28 'shuffle' => [
29 'args' => true,
30 'func' => 'callShuffle',
31 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_SHUFFLE_DESCRIPTION'),
32 ],
33 'firstvalue' => [
34 'args' => true,
35 'func' => 'callFirstValue',
36 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_FIRSTVALUE_DESCRIPTION'),
37 ],
38 'swirl' => [
39 'args' => true,
40 'func' => 'callSwirl',
41 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_SWIRL_DESCRIPTION'),
42 ],
43 ];
44 }
45
46 public function callImplode(Arguments $args)
47 {
48 $glue = $args->getFirst();
49
50 if (!is_scalar($glue))
51 {
52 return null;
53 }
54
55 $pieces = \CBPHelper::makeArrayFlat($args->getSecond());
56
57 if (!$pieces)
58 {
59 return '';
60 }
61
62 return implode($glue, $pieces);
63 }
64
65 public function callExplode(Arguments $args)
66 {
67 $delimiter = $args->getFirst();
68 $str = $args->getSecondSingle();
69
70 if (empty($delimiter) || !is_scalar($str) || !is_scalar($delimiter))
71 {
72 return null;
73 }
74
75 $str = (string)$str;
76
77 return explode($delimiter, $str);
78 }
79
80 public function callMerge(Arguments $args)
81 {
82 $arrays = $args->getArray();
83
84 foreach ($arrays as &$a)
85 {
86 $a = is_object($a) ? [$a] : (array)$a;
87 }
88
89 return array_merge(...$arrays);
90 }
91
92 public function callShuffle(Arguments $args)
93 {
94 $array = array_filter($args->getFlatArray(), fn ($arg) => $arg !== null);
95 shuffle($array);
96
97 return $array;
98 }
99
100 public function callFirstValue(Arguments $args)
101 {
102 $array = $args->getFlatArray();
103
104 return $array[0] ?? null;
105 }
106
107 public function callSwirl(Arguments $args)
108 {
109 $array = array_values(
110 array_filter($args->getFlatArray(), fn ($arg) => $arg !== null)
111 );
112
113 if (count($array) <= 1)
114 {
115 return $array;
116 }
117
118 return array_merge(array_slice($array, 1), [$array[0]]);
119 }
120}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29