Failed Conditions
Push — psr2-config ( eb1b59...98a151 )
by Andreas
03:25
created

SettingNumeric   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B update() 0 15 8
A out() 0 8 3
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
        if($fmt != 'php') return '';
36
37
        $local = $this->local === '' ? "''" : $this->local;
38
        $out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
39
40
        return $out;
41
    }
42
}
43