Completed
Push — master ( de52f8...424961 )
by Mikael
03:48
created

ViewHelperFunctions.php ➔ classList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\View;
4
5
/**
6
 * Define helper functions to include before processing the view template.
7
 * The functions here are exposed to the view and can be used in the view.
8
 */
9
10
/**
11
 * Shortcut to create an url for a static asset.
12
 *
13
 * @param string $url url to use when creating the url.
14
 *
15
 * @return string as resulting url.
16
 */
17
function asset($url = "")
18
{
19
    global $di;
20
    return $di->get("url")->asset($url);
21
}
22
23
24
25
/**
26
 * Shortcut to create an url for routing in the framework.
27
 *
28
 * @param null|string $url url to use when creating the url.
29
 *
30
 * @return string as resulting url.
31
 */
32
function url($url = "")
33
{
34
    global $di;
35
    return $di->get("url")->create($url);
36
}
37
38
39
40
/**
41
 * Render a view with an optional data set of variables.
42
 *
43
 * @param string $template the template file, or array
44
 * @param array  $data     variables to make available to the
45
 *                         view, default is empty
46
 *
47
 * @return void
48
 */
49
function renderView($template, $data = [])
50
{
51
    global $di;
52
    $view = new View();
53
    $template = $di->get("view")->getTemplateFile($template);
54
    $view->set($template, $data);
55
    $view->render($di);
56
}
57
58
59
60
/**
61
 * Check if the region in the view container has views to render.
62
 *
63
 * @param string $region to check
64
 *
65
 * @return boolean true or false
66
 */
67
function regionHasContent($region)
68
{
69
    global $di;
70
    return $di->get("view")->hasContent($region);
71
}
72
73
74
75
/**
76
 * Render views, from the view container, in the region.
77
 *
78
 * @param string $region to render in
79
 *
80
 * @return boolean true or false
81
 */
82
function renderRegion($region)
83
{
84
    global $di;
85
    return $di->get("view")->render($region);
86
}
87
88
89
90
/**
91
 * Create a class attribute from a string or array.
92
 *
93
 * @param string|array $args variable amount of classlists.
94
 *
95
 * @return string as complete class attribute
96
 */
97
function classList(...$args)
98
{
99 5
    $classes = [];
100
101 5
    foreach ($args as $arg) {
102 5
        if (empty($arg)) {
103 2
            continue;
104 4
        } elseif (is_string($arg)) {
105 4
            $arg = explode(" ", $arg);
106
        }
107 4
        $classes = array_merge($classes, $arg);
108
    }
109
110 5
    return "class=\"" . implode(" ", $classes) . "\"";
111
}
112
113
114
115
/**
116
 * Get current url, without querystring.
117
 *
118
 * @return string as resulting url.
119
 */
120
function currentUrl()
121
{
122
    global $di;
123
    return $di->get("request")->getCurrentUrl(false);
124
}
125
126
127
128
/**
129
 * Get current route.
130
 *
131
 * @return string as resulting route.
132
 */
133
function currentRoute()
134
{
135
    global $di;
136
    return $di->get("request")->getRoute();
137
}
138
139
140
141
/**
142
 * Show variables/functions that are currently defined and can
143
 * be used within the view. Call the function with get_defined_vars()
144
 * as the parameter.
145
 *
146
 * @param array $variables should be the returned value from
147
 *                         get_defined_vars()
148
 * @param array $functions should be the returned value from
149
 *                         get_defined_functions()
150
 *
151
 * @return string showing variables and functions.
152
 */
153
function showEnvironment($variables, $functions)
154
{
155
    $all = array_keys($variables);
156
    sort($all);
157
    $res = "<pre>\n"
158
        . "VIEW DEVELOPMENT INFORMATION\n"
159
        . "----------------------------\n"
160
        . "Variables available:\n"
161
        . " (var_dump each for more information):\n";
162
    foreach ($all as $var) {
163
        $variable = $variables[$var];
164
        $res .= "* $var (" . gettype($variable) . ")";
165
        if (is_integer($variable) || is_double($variable)) {
166
            $res .= ": $variable";
167
        } elseif (is_string($variable)) {
168
            $res .= ": \"$variable\"";
169
        } elseif (is_bool($variable)) {
170
            $res .= ": " . ( $variable ? "true" : "false" );
171
        }
172
        $res .= "\n";
173
    }
174
175
    $res .= "\nView helper functions available:\n (see " . __FILE__ . ")\n";
176
    $namespace = strtolower(__NAMESPACE__);
177
    $matches = array_filter(
178
        $functions["user"],
179
        function ($function) use ($namespace) {
180
            return substr($function, 0, strlen($namespace)) === $namespace;
181
        }
182
    );
183
    sort($matches);
184
    $res .= "* " . implode(",\n* ", $matches);
185
    $res .= "</pre>";
186
187
    return $res;
188
}
189
190
191
192
/* OLDER VERSIONS MIGHT NEED IMPLEMENTING */
193
194
/**
195
 * Load content from a route and return details to view.
196
 *
197
 * @param string $route to load content from.
198
 *
199
 * @return array with values to extract in view.
200
 */
201
// public function getContentForRoute($route)
202
// {
203
//     $content = $this->di->get("content")->contentForInternalRoute($route);
204
//     return $content->views["main"]["data"];
205
// }
206
207
208
209
/**
210
 * Wrap a HTML element with start and end.
211
 *
212
 * @param string  $text  with content
213
 * @param string  $tag   HTML tag to search for
214
 * @param string  $start wrap start part
215
 * @param string  $end   wrap end part
216
 * @param number  $count hits to search for
217
 *
218
 * @return array with values to extract in view.
219
 */
220
// public function wrapElementWithStartEnd($text, $tag, $start, $end, $count)
221
// {
222
//     return $this->di->get("textFilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count);
223
// }
224
225
226
227
/**
228
 * Wrap content of a HTML element with start and end.
229
 *
230
 * @param string  $text  with content
231
 * @param string  $tag   HTML tag to search for
232
 * @param string  $start wrap start part
233
 * @param string  $end   wrap end part
234
 * @param number  $count hits to search for
235
 *
236
 * @return array with values to extract in view.
237
 */
238
// public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count)
239
// {
240
//     return $this->di->get("textFilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count);
241
// }
242
243
244
245
/**
246
 * Extrat the publish or update date for the article.
247
 *
248
 * @param array $dates a collection of possible date values.
249
 *
250
 * @return array with values for showing the date.
251
 */
252
// public function getPublishedDate($dates)
253
// {
254
//     $defaults = [
255
//         "revision" => [],
256
//         "published" => null,
257
//         "updated" => null,
258
//         "created" => null,
259
//     ];
260
//     $dates = array_merge($defaults, $dates);
261
//
262
//     if ($dates["revision"]) {
263
//         return [t("Latest revision"), key($dates["revision"])];
264
//     } elseif ($dates["published"]) {
265
//         return [t("Published"), $dates["published"]];
266
//     } elseif ($dates["updated"]) {
267
//         return [t("Updated"), $dates["updated"]];
268
//     } elseif ($dates["created"]) {
269
//         return [t("Created"), $dates["created"]];
270
//     }
271
//
272
//     return [t("Missing pubdate."), null];
273
// }
274