Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
MathLib.php
1<?php
2
4
7
8class MathLib extends BaseLib
9{
10 public function getFunctions(): array
11 {
12 return [
13 'abs' => [
14 'args' => true,
15 'func' => 'callAbs',
16 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_ABS_DESCRIPTION'),
17 ],
18 'intval' => [
19 'args' => true,
20 'func' => 'callIntval',
21 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_INTVAL_DESCRIPTION'),
22 ],
23 'floatval' => [
24 'args' => true,
25 'func' => 'callFloatval',
26 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_FLOATVAL_DESCRIPTION'),
27 ],
28 'min' => [
29 'args' => true,
30 'func' => 'callMin',
31 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_MIN_DESCRIPTION'),
32 ],
33 'max' => [
34 'args' => true,
35 'func' => 'callMax',
36 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_MAX_DESCRIPTION'),
37 ],
38 'rand' => [
39 'args' => true,
40 'func' => 'callRand',
41 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_RAND_DESCRIPTION'),
42 ],
43 'round' => [
44 'args' => true,
45 'func' => 'callRound',
46 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_ROUND_DESCRIPTION'),
47 ],
48 'ceil' => [
49 'args' => true,
50 'func' => 'callCeil',
51 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_CEIL_DESCRIPTION'),
52 ],
53 'floor' => [
54 'args' => true,
55 'func' => 'callFloor',
56 'description' => Loc::getMessage('BIZPROC_CALC_FUNCTION_FLOOR_DESCRIPTION'),
57 ],
58 ];
59 }
60
61 public function callAbs(Arguments $args)
62 {
63 return abs((float)$args->getFirstSingle());
64 }
65
66 public function callRound(Arguments $args)
67 {
68 $val = (float)$args->getFirstSingle();
69 $precision = (int)$args->getSecond();
70
71 return round($val, $precision);
72 }
73
74 public function callCeil(Arguments $args)
75 {
76 return ceil((double)$args->getFirstSingle());
77 }
78
79 public function callFloor(Arguments $args)
80 {
81 return floor((double)$args->getFirstSingle());
82 }
83
84 public function callMin(Arguments $args)
85 {
86 $array = array_filter($args->getFlatArray(), static fn($item) => is_scalar($item));
87
88 return $array ? min($array) : false;
89 }
90
91 public function callMax(Arguments $args)
92 {
93 $array = array_filter($args->getFlatArray(), static fn($item) => is_scalar($item));
94
95 return $array ? max($array) : false;
96 }
97
98 public function callRand(Arguments $args)
99 {
100 $min = (int)$args->getFirstSingle();
101 $max = (int)$args->getSecondSingle();
102
103 if (!$max || !is_finite($max))
104 {
105 $max = mt_getrandmax();
106 }
107
108 if ($min > $max)
109 {
110 //$args->getParser()->setError('rand(): Argument #2 ($max) must be greater than or equal to argument #1 ($min)');
111
112 return null;
113 }
114
115 return mt_rand($min, $max);
116 }
117
118 public function callIntval(Arguments $args)
119 {
120 return (int)$args->getFirstSingle();
121 }
122
123 public function callFloatval(Arguments $args)
124 {
125 return (float)$args->getFirstSingle();
126 }
127}
callFloatval(Arguments $args)
Definition MathLib.php:123
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29