xref: /dokuwiki/lib/plugins/config/core/Setting/SettingNumeric.php (revision 7d34963b3e75ea04c63ec066a6b7a692e123cb53)
10a5b05ebSAndreas Gohr<?php
20a5b05ebSAndreas Gohr
30a5b05ebSAndreas Gohrnamespace dokuwiki\plugin\config\core\Setting;
40a5b05ebSAndreas Gohr
50a5b05ebSAndreas Gohr/**
60a5b05ebSAndreas Gohr * Class setting_numeric
70a5b05ebSAndreas Gohr */
88c7c53b0SAndreas Gohrclass SettingNumeric extends SettingString
98c7c53b0SAndreas Gohr{
100a5b05ebSAndreas Gohr    // This allows for many PHP syntax errors...
110a5b05ebSAndreas Gohr    // var $_pattern = '/^[-+\/*0-9 ]*$/';
120a5b05ebSAndreas Gohr    // much more restrictive, but should eliminate syntax errors.
13467c1427SAndreas Gohr    protected $pattern = '/^[-+]? *\d+ *(?:[-+*] *\d+ *)*$/';
14467c1427SAndreas Gohr    protected $min;
15467c1427SAndreas Gohr    protected $max;
160a5b05ebSAndreas Gohr
170a5b05ebSAndreas Gohr    /** @inheritdoc */
18d868eb89SAndreas Gohr    public function update($input)
19d868eb89SAndreas Gohr    {
200a5b05ebSAndreas Gohr        $local = $this->local;
210a5b05ebSAndreas Gohr        $valid = parent::update($input);
220a5b05ebSAndreas Gohr        if ($valid && !(is_null($this->min) && is_null($this->max))) {
230a5b05ebSAndreas Gohr            $numeric_local = (int) eval('return ' . $this->local . ';');
24*7d34963bSAndreas Gohr            if (
25*7d34963bSAndreas Gohr                (!is_null($this->min) && $numeric_local < $this->min) ||
26*7d34963bSAndreas Gohr                (!is_null($this->max) && $numeric_local > $this->max)
27*7d34963bSAndreas Gohr            ) {
280a5b05ebSAndreas Gohr                $this->error = true;
290a5b05ebSAndreas Gohr                $this->input = $input;
300a5b05ebSAndreas Gohr                $this->local = $local;
310a5b05ebSAndreas Gohr                $valid = false;
320a5b05ebSAndreas Gohr            }
330a5b05ebSAndreas Gohr        }
340a5b05ebSAndreas Gohr        return $valid;
350a5b05ebSAndreas Gohr    }
360a5b05ebSAndreas Gohr
370a5b05ebSAndreas Gohr    /** @inheritdoc */
38d868eb89SAndreas Gohr    public function out($var, $fmt = 'php')
39d868eb89SAndreas Gohr    {
40f00299d8SAndreas Gohr        if ($fmt != 'php') return '';
410a5b05ebSAndreas Gohr
420a5b05ebSAndreas Gohr        $local = $this->local === '' ? "''" : $this->local;
43f00299d8SAndreas Gohr        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
440a5b05ebSAndreas Gohr
450a5b05ebSAndreas Gohr        return $out;
460a5b05ebSAndreas Gohr    }
470a5b05ebSAndreas Gohr}
48