Completed
Pull Request — v1 (#422)
by
unknown
03:17
created

TemplateHelper::slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Util;
8
9
/**
10
 * Exposes useful tools for working with/in templates
11
 */
12
class TemplateHelper
13
{
14
    /**
15
     * An array of variables to be passed to all templates
16
     * @var array
17
     */
18
    private $variables = array();
19
20
    /**
21
     * Escapes a string for output in an HTML document
22
     *
23
     * @param  string $raw
24
     * @return string
25
     */
26 2
    public function escape($raw)
27
    {
28 2
        $flags = ENT_QUOTES;
29
30
        // HHVM has all constants defined, but only ENT_IGNORE
31
        // works at the moment
32 2
        if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
33 2
            $flags |= ENT_SUBSTITUTE;
34 2
        } else {
35
            // This is for 5.3.
36
            // The documentation warns of a potential security issue,
37
            // but it seems it does not apply in our case, because
38
            // we do not blacklist anything anywhere.
39
            $flags |= ENT_IGNORE;
40
        }
41
42 2
        return htmlspecialchars($raw, $flags, "UTF-8");
43
    }
44
45
    /**
46
     * Escapes a string for output in an HTML document, but preserves
47
     * URIs within it, and converts them to clickable anchor elements.
48
     *
49
     * @param  string $raw
50
     * @return string
51
     */
52 1
    public function escapeButPreserveUris($raw)
53
    {
54 1
        $escaped = $this->escape($raw);
55 1
        return preg_replace(
56 1
            "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
57 1
            "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
58 1
        );
59
    }
60
61
    /**
62
     * Convert a string to a slug version of itself
63
     *
64
     * @param  string $original
65
     * @return string
66
     */
67 1
    public function slug($original)
68
    {
69 1
        $slug = str_replace(" ", "-", $original);
70 1
        $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
71 1
        return strtolower($slug);
72
    }
73
74
    /**
75
     * Given a template path, render it within its own scope. This
76
     * method also accepts an array of additional variables to be
77
     * passed to the template.
78
     *
79
     * @param string $template
80
     * @param array  $additionalVariables
81
     */
82 1
    public function render($template, array $additionalVariables = null)
83
    {
84 1
        $variables = $this->getVariables();
85
86
        // Pass the helper to the template:
87 1
        $variables["tpl"] = $this;
88
89 1
        if ($additionalVariables !== null) {
90 1
            $variables = array_replace($variables, $additionalVariables);
91 1
        }
92
93 1
        call_user_func(function () {
94 1
            extract(func_get_arg(1));
95 1
            require func_get_arg(0);
96 1
        }, $template, $variables);
97 1
    }
98
99
    /**
100
     * Sets the variables to be passed to all templates rendered
101
     * by this template helper.
102
     *
103
     * @param array $variables
104
     */
105 1
    public function setVariables(array $variables)
106
    {
107 1
        $this->variables = $variables;
108 1
    }
109
110
    /**
111
     * Sets a single template variable, by its name:
112
     *
113
     * @param string $variableName
114
     * @param mixd   $variableValue
115
     */
116 1
    public function setVariable($variableName, $variableValue)
117
    {
118 1
        $this->variables[$variableName] = $variableValue;
119 1
    }
120
121
    /**
122
     * Gets a single template variable, by its name, or
123
     * $defaultValue if the variable does not exist
124
     *
125
     * @param  string $variableName
126
     * @param  mixed  $defaultValue
127
     * @return mixed
128
     */
129 1
    public function getVariable($variableName, $defaultValue = null)
130
    {
131 1
        return isset($this->variables[$variableName]) ?
132 1
            $this->variables[$variableName] : $defaultValue;
133
    }
134
135
    /**
136
     * Unsets a single template variable, by its name
137
     *
138
     * @param string $variableName
139
     */
140 1
    public function delVariable($variableName)
141
    {
142 1
        unset($this->variables[$variableName]);
143 1
    }
144
145
    /**
146
     * Returns all variables for this helper
147
     *
148
     * @return array
149
     */
150 1
    public function getVariables()
151
    {
152 1
        return $this->variables;
153
    }
154
}
155