-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc.inc.php
155 lines (130 loc) · 3.86 KB
/
calc.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* 与えた数式の答えを算出するプラグイン
*
* @version 1.0.0
* @author kanateko
* @link https://jpngamerswiki.com/?f51cd63681
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
* -- Updates --
* 2023-10-11 v1.0.0 初版作成
*/
// 数字をカンマ区切りで表示する
define('PLUGIN_CALC_FORMAT_AS_DEFAULT', false);
// カンマ区切り時のデフォルトの小数点以下の桁数
define('PLUGIN_CALC_DEFAULT_PRECISION', 2);
function plugin_calc_init(): void
{
$msg['_calc_messages'] = [
'msg_usage' => '&calc([precision, true/false]){formula};',
'err_unknown' => '#calc Error: Unknown argument. (%s)',
'err_invalid' => '#calc Error: Invalid formula.',
];
set_plugin_messages($msg);
}
function plugin_calc_inline(string ...$args): string
{
$calc = new PluginCalc($args);
$result = $calc->calc();
return $result;
}
class PluginCalc
{
public array $err;
private string $formula;
private array $options;
/**
* コンストラクタ
*
* @param array $args
*/
public function __construct(array $args)
{
$this->err = [];
$formula = array_pop($args);
if ($formula === '') {
$this->err = ['msg_usage'];
} elseif ($this->is_valid($formula)) {
$this->formula = $formula;
$this->get_options($args);
}
// デフォルト設定
$this->options['format'] ??= PLUGIN_CALC_FORMAT_AS_DEFAULT;
$this->options['precision'] ??= $this->options['formant'] ? PLUGIN_CALC_DEFAULT_PRECISION : null;
}
/**
* 計算
*
* @return string
*/
public function calc(): string
{
if ($this->has_error()) return $this->show_msg();
$format = $this->options['format'];
$precision = $this->options['precision'];
$result = '';
try {
$result = eval("return {$this->formula};");
if ($precision !== null) $result = round($result, $precision);
if ($format) $result = number_format($result, $precision);
} catch (ParseError $e) {
$this->err = ['err_invalid'];
$result = $this->show_msg();
}
return $result;
}
/**
* 計算式以外の文字がないか確認
*
* @param string $formula
* @return boolean
*/
private function is_valid(string $formula): bool
{
if (preg_match('/[^\s\d+\-*\/%=().]/', $formula)) {
$this->err = ['err_invalid'];
return false;
}
return true;
}
private function get_options(array $args): void
{
$args = array_map('trim', $args);
foreach($args as $i => $arg) if ($arg === '') $args[$i] = null;
// 小数点以下の桁数
if (is_numeric($args[0])) $this->options['precision'] = $args[0];
elseif ($args[0] !== null) $this->err = ['err_unknown' => $args[0]];
// カンマ区切り
if ($args[1] === 'false') $this->options['format'] = false;
elseif ($args[1] === 'true') $this->options['format'] = true;
elseif ($args[1] !== null) $this->err = ['err_unknown' => $args[1]];
}
/**
* エラーの確認
*
* @return boolean
*/
private function has_error(): bool
{
return $this->err !== [];
}
/**
* メッセージ表示
*
* @return string
*/
private function show_msg(): string
{
global $_calc_messages;
$msg = '';
if (array_values($this->err) === $this->err) {
$msg = $_calc_messages[$this->err[0]];
} else {
foreach ($this->err as $key => $val) {
$msg = $_calc_messages[$key];
$msg = str_replace('%s', htmlsc($val), $msg);
}
}
return "<p>$msg</p>";
}
}