Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StringLib.php
1<?php
2
4
8
9class StringLib extends BaseLib
10{
11 public function getFunctions(): array
12 {
13 return [
14 'numberformat' => [
15 'args' => true,
16 'func' => 'callNumberFormat',
17 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_NUMBER_FORMAT_DESCRIPTION'),
18 ],
19 'substr' => [
20 'args' => true,
21 'func' => 'callSubstr',
22 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_SUBSTR_DESCRIPTION'),
23 ],
24 'strpos' => [
25 'args' => true,
26 'func' => 'callStrpos',
27 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_STRPOS_DESCRIPTION'),
28 ],
29 'strlen' => [
30 'args' => true,
31 'func' => 'callStrlen',
32 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_STRLEN_DESCRIPTION'),
33 ],
34 'randstring' => [
35 'args' => true,
36 'func' => 'callRandString',
37 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_RANDSTRING_DESCRIPTION'),
38 ],
39 'strtolower' => [
40 'args' => true,
41 'func' => 'callStrtolower',
42 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_STRTOLOWER_DESCRIPTION'),
43 ],
44 'strtoupper' => [
45 'args' => true,
46 'func' => 'callStrtoupper',
47 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_STRTOUPPER_DESCRIPTION'),
48 ],
49 'ucwords' => [
50 'args' => true,
51 'func' => 'callUcwords',
52 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_UCWORDS_DESCRIPTION'),
53 ],
54 'ucfirst' => [
55 'args' => true,
56 'func' => 'callUcfirst',
57 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_UCFIRST_DESCRIPTION'),
58 ],
59 'trim' => [
60 'args' => true,
61 'func' => 'callTrim',
62 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_TRIM_DESCRIPTION'),
63 ],
64 'urlencode' => [
65 'args' => true,
66 'func' => 'callUrlencode',
67 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_URLENCODE_DESCRIPTION'),
68 ],
69 ];
70 }
71
72 public function callNumberFormat(Arguments $args)
73 {
74 $number = (float)$args->getFirstSingle();
75 $decimals = (int)($args->getSecond() ?: 0);
76 $decPoint = $args->getThird();
77
78 if (!is_scalar($decPoint))
79 {
80 $decPoint = '.';
81 }
82 $decPoint = (string)$decPoint;
83
84 $thousandsSeparator = $args->getArg(3);
85 if (!is_scalar($thousandsSeparator))
86 {
87 $thousandsSeparator = ',';
88 }
89 $thousandsSeparator = (string)$thousandsSeparator;
90
91 return number_format($number, $decimals, $decPoint, $thousandsSeparator);
92 }
93
94 public function callRandString(Arguments $args)
95 {
96 $len = (int)$args->getFirstSingle();
97
98 return Main\Security\Random::getString($len, true);
99 }
100
101 public function callSubstr(Arguments $args)
102 {
103 $str = $args->getFirstSingle();
104 $pos = (int)$args->getSecond();
105 $len = (int)$args->getThird();
106
107 if (is_array($str))
108 {
109 return null;
110 }
111
112 if (!$str)
113 {
114 return '';
115 }
116
117 if ($len)
118 {
119 return mb_substr($str, $pos, $len);
120 }
121
122 return mb_substr($str, $pos);
123 }
124
125 public function callStrpos(Arguments $args)
126 {
127 $haystack = $args->getFirstSingle();
128 $needle = $args->getSecondSingle();
129
130 if (empty($haystack) || is_array($haystack) || is_array($needle))
131 {
132 return false;
133 }
134 $haystack = (string)$haystack;
135 $needle = (string)$needle;
136
137 $maxOffset = mb_strlen($haystack);
138 $minOffset = -1 * $maxOffset;
139
140 $offset = max($minOffset, min($maxOffset, (int)$args->getThird()));
141
142 return mb_strpos($haystack, $needle, $offset);
143 }
144
145 public function callStrlen(Arguments $args)
146 {
147 $str = $args->getFirstSingle();
148
149 if (!is_scalar($str))
150 {
151 return null;
152 }
153
154 $str = (string)$str;
155
156 return mb_strlen($str);
157 }
158
159 public function callUrlencode(Arguments $args)
160 {
161 $str = $args->getFirstSingle();
162
163 if (!is_scalar($str))
164 {
165 if (is_array($str) && count($str) === 1)
166 {
167 $str = \CBPHelper::stringify($str);
168 }
169 else
170 {
171 return null;
172 }
173 }
174
175 $str = (string)$str;
176
177 return urlencode($str);
178 }
179
180 public function callStrtolower(Arguments $args)
181 {
182 $str = $args->getFirstSingle();
183
184 if (!is_scalar($str))
185 {
186 return null;
187 }
188
189 return mb_strtolower((string)$str);
190 }
191
192 public function callStrtoupper(Arguments $args)
193 {
194 $str = $args->getFirstSingle();
195
196 if (!is_scalar($str))
197 {
198 return null;
199 }
200
201 return mb_strtoupper((string)$str);
202 }
203
204 public function callUcwords(Arguments $args)
205 {
206 $str = $args->getFirstSingle();
207
208 if (!is_scalar($str))
209 {
210 return null;
211 }
212
213 return mb_convert_case((string)$str, MB_CASE_TITLE);
214 }
215
216 public function callUcfirst(Arguments $args)
217 {
218 $str = $args->getFirstSingle();
219
220 if (!is_scalar($str))
221 {
222 return null;
223 }
224
225 return $this->mbUcFirst((string)$str);
226 }
227
228 private function mbUcFirst($str)
229 {
230 $len = mb_strlen($str);
231 $firstChar = mb_substr($str, 0, 1);
232 $otherChars = mb_substr($str, 1, $len - 1);
233
234 return mb_strtoupper($firstChar) . $otherChars;
235 }
236
237 public function callTrim(Arguments $args)
238 {
239 $array = $args->getFlatArray();
240
241 $result = [];
242 foreach ($array as $str)
243 {
244 if (is_scalar($str) || (is_object($str) && method_exists($str, '__toString')))
245 {
246 $result[] = trim((string)$str);
247 }
248 }
249
250 return count($result) > 1 ? $result : ($result[0] ?? '');
251 }
252}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29