Failed Conditions
Push — psr2-config ( de33cd...0a5b05 )
by Andreas
08:47 queued 04:28
created

SettingArray::out()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 2
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\config\core\Setting;
4
5
/**
6
 * Class setting_array
7
 */
8
class SettingArray extends Setting {
9
10
    /**
11
     * Create an array from a string
12
     *
13
     * @param string $string
14
     * @return array
15
     */
16
    protected function fromString($string) {
17
        $array = explode(',', $string);
18
        $array = array_map('trim', $array);
19
        $array = array_filter($array);
20
        $array = array_unique($array);
21
        return $array;
22
    }
23
24
    /**
25
     * Create a string from an array
26
     *
27
     * @param array $array
28
     * @return string
29
     */
30
    protected function fromArray($array) {
31
        return join(', ', (array) $array);
32
    }
33
34
    /**
35
     * update setting with user provided value $input
36
     * if value fails error check, save it
37
     *
38
     * @param string $input
39
     * @return bool true if changed, false otherwise (incl. on error)
40
     */
41
    public function update($input) {
42
        if(is_null($input)) return false;
43
        if($this->isProtected()) return false;
44
45
        $input = $this->fromString($input);
46
47
        $value = is_null($this->local) ? $this->default : $this->local;
48
        if($value == $input) return false;
49
50
        foreach($input as $item) {
51
            if($this->pattern && !preg_match($this->pattern, $item)) {
52
                $this->error = true;
53
                $this->input = $input;
54
                return false;
55
            }
56
        }
57
58
        $this->local = $input;
59
        return true;
60
    }
61
62
    /**
63
     * Escaping
64
     *
65
     * @param string $string
66
     * @return string
67
     */
68
    protected function escape($string) {
69
        $tr = array("\\" => '\\\\', "'" => '\\\'');
70
        return "'" . strtr(cleanText($string), $tr) . "'";
71
    }
72
73
    /** @inheritdoc */
74
    public function out($var, $fmt = 'php') {
75
76
        if($this->isProtected()) return '';
77
        if(is_null($this->local) || ($this->default == $this->local)) return '';
78
79
        $out = '';
80
81
        if($fmt == 'php') {
82
            $vals = array_map(array($this, 'escape'), $this->local);
83
            $out = '$' . $var . "['" . $this->getArrayKey() . "'] = array(" . join(', ', $vals) . ");\n";
84
        }
85
86
        return $out;
87
    }
88
89
    /** @inheritdoc */
90
    public function html(\admin_plugin_config $plugin, $echo = false) {
91
        $disable = '';
92
93
        if($this->isProtected()) {
94
            $value = $this->protected;
95
            $disable = 'disabled="disabled"';
96
        } else {
97
            if($echo && $this->error) {
98
                $value = $this->input;
99
            } else {
100
                $value = is_null($this->local) ? $this->default : $this->local;
101
            }
102
        }
103
104
        $key = htmlspecialchars($this->key);
105
        $value = htmlspecialchars($this->fromArray($value));
106
107
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
108
        $input = '<input id="config___' . $key . '" name="config[' . $key .
109
            ']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>';
110
        return array($label, $input);
111
    }
112
}
113