xref: /dokuwiki/lib/plugins/config/core/Setting/SettingNumeric.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
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        $local = $this->local;
20        $valid = parent::update($input);
21        if($valid && !(is_null($this->min) && is_null($this->max))) {
22            $numeric_local = (int) eval('return ' . $this->local . ';');
23            if((!is_null($this->min) && $numeric_local < $this->min) ||
24                (!is_null($this->max) && $numeric_local > $this->max)) {
25                $this->error = true;
26                $this->input = $input;
27                $this->local = $local;
28                $valid = false;
29            }
30        }
31        return $valid;
32    }
33
34    /** @inheritdoc */
35    public function out($var, $fmt = 'php') {
36        if($fmt != 'php') return '';
37
38        $local = $this->local === '' ? "''" : $this->local;
39        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
40
41        return $out;
42    }
43}
44