summaryrefslogtreecommitdiffstats
path: root/lib/plugins/config/core/Setting/SettingNumeric.php
blob: 27ca1945c243a082124bc23e7679b7901c05525f (plain)
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
<?php

namespace dokuwiki\plugin\config\core\Setting;

/**
 * Class setting_numeric
 */
class SettingNumeric extends SettingString
{
    // This allows for many PHP syntax errors...
    // var $_pattern = '/^[-+\/*0-9 ]*$/';
    // much more restrictive, but should eliminate syntax errors.
    protected $pattern = '/^[-+]? *\d+ *(?:[-+*] *\d+ *)*$/';
    protected $min;
    protected $max;

    /** @inheritdoc */
    public function update($input)
    {
        $local = $this->local;
        $valid = parent::update($input);
        if ($valid && !(is_null($this->min) && is_null($this->max))) {
            $numeric_local = (int) eval('return ' . $this->local . ';');
            if (
                (!is_null($this->min) && $numeric_local < $this->min) ||
                (!is_null($this->max) && $numeric_local > $this->max)
            ) {
                $this->error = true;
                $this->input = $input;
                $this->local = $local;
                $valid = false;
            }
        }
        return $valid;
    }

    /** @inheritdoc */
    public function out($var, $fmt = 'php')
    {
        if ($fmt != 'php') return '';

        $local = $this->local === '' ? "''" : $this->local;
        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";

        return $out;
    }
}