Failed Conditions
Push — psr2-config ( de33cd...0a5b05 )
by Andreas
04:27
created

SettingNumeric::update()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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