Passed
Push — master ( 7c91ce...412da6 )
by Andreas
22:01
created

midcom_helper_formatter::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Formatting helpers for style elements.
11
 *
12
 * @package midcom.helper
13
 */
14
class midcom_helper_formatter
15
{
16
    /**
17
     * Filter registry
18
     *
19
     * @var array
20
     */
21
    private static $_filters = [
22
        'h' => 'html',
23
        'H' => 'html',
24
        'p' => 'php',
25
        'u' => 'rawurlencode',
26
        'f' => 'nl2br',
27
        's' => 'unmodified',
28
    ];
29
30
    /**
31
     * Register PHP function as string formatter to the Midgard formatting engine.
32
     */
33
    public static function register($name, $function)
34
    {
35
        self::$_filters["x{$name}"] = $function;
36
    }
37
38
    /**
39
     * Return a string as formatted by a specified filter
40
     */
41
    public static function format(string $content, string $name) : string
42
    {
43
        if (!isset(self::$_filters[$name])) {
44
            return $content;
45
        }
46
47
        switch ($name) {
48
            case 's':
49
                //display as-is
50
            case 'h':
51
            case 'H':
52
                //According to documentation, these two should do something, but actually they don't...
53
                return $content;
54
            case 'p':
55
                ob_start();
56
                eval('?>' . $content);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
57
                return ob_get_clean();
58
            default:
59
                return call_user_func(self::$_filters[$name], $content);
60
        }
61
    }
62
63
    /**
64
     * Compile string to php code for the specified filter
65
     *
66
     * @param string $variable The content to modify
67
     */
68 115
    public static function convert_to_php($variable) : string
69
    {
70 115
        $variable_parts = explode(':', $variable[1]);
71 115
        $variable = '$' . $variable_parts[0];
72
73 115
        if (strpos($variable, '.') !== false) {
74 12
            $parts = explode('.', $variable);
75 12
            $variable = $parts[0] . '->' . $parts[1];
76
        }
77
78 115
        if (    isset($variable_parts[1])
79 115
             && array_key_exists($variable_parts[1], self::$_filters)) {
80 49
            switch ($variable_parts[1]) {
81 49
                case 's':
82
                    //display as-is
83 49
                case 'h':
84
                case 'H':
85
                    //According to documentation, these two should do something, but actually they don't...
86 49
                    $command = 'echo ' . $variable;
87 49
                    break;
88
                case 'p':
89
                    $command = 'eval(\'?>\' . ' . $variable . ')';
90
                    break;
91
                default:
92
                    $function = self::$_filters[$variable_parts[1]];
93
                    $command = $function . '(' . $variable . ')';
94 49
                    break;
95
            }
96
        } else {
97 99
            $command = 'echo htmlentities(' . $variable . ', ENT_COMPAT, midcom::get()->i18n->get_current_charset())';
98
        }
99
100 115
        return "<?php $command; ?>";
101
    }
102
}
103