1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_numeric
7 */
8class SettingNumeric extends SettingString
9{
10    // This allows for many PHP syntax errors...
11    // var $_pattern = '/^[-+\/*0-9 ]*$/';
12    // much more restrictive, but should eliminate syntax errors.
13    protected $pattern = '/^[-+]? *\d+ *(?:[-+*] *\d+ *)*$/';
14    protected $min;
15    protected $max;
16
17    /** @inheritdoc */
18    public function update($input)
19    {
20        $local = $this->local;
21        $valid = parent::update($input);
22        if ($valid && !(is_null($this->min) && is_null($this->max))) {
23            $numeric_local = (int) eval('return ' . $this->local . ';');
24            if (
25                (!is_null($this->min) && $numeric_local < $this->min) ||
26                (!is_null($this->max) && $numeric_local > $this->max)
27            ) {
28                $this->error = true;
29                $this->input = $input;
30                $this->local = $local;
31                $valid = false;
32            }
33        }
34        return $valid;
35    }
36
37    /** @inheritdoc */
38    public function out($var, $fmt = 'php')
39    {
40        if ($fmt != 'php') return '';
41
42        $local = $this->local === '' ? "''" : $this->local;
43        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
44
45        return $out;
46    }
47}
48