-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.php
99 lines (76 loc) · 2.57 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Twig\Blank;
class Utils extends \Twig_Extension
{
public function getName()
{
return 'twig_utils';
}
// Translations
public function setTranslations ($translations)
{
$this->translations = $translations;
}
// Register functions
public function getFunctions()
{
return array(
'file_exists' => new \Twig_Function_Function('file_exists'),
'print_r' => new \Twig_Function_Function('print_r'),
'var_dump' => new \Twig_Function_Function('var_dump'),
'echo' => new \Twig_Function_Function('echo'),
'die' => new \Twig_Function_Function('die'),
'substr' => new \Twig_Function_Function('substr'),
'encode_data' => new \Twig_Function_Method($this, 'encode_data'),
'handleize' => new \Twig_Function_Method($this, 'handleize'),
'getUrl' => new \Twig_Function_Method($this, 'get_url'),
'icons' => new \Twig_Function_Method($this, 'icons', array('is_safe' => array('html'))),
'translations' => new \Twig_Function_Method($this, 'translations')
);
}
// Register filters
public function getFilters()
{
return array(
'handleize' => new \Twig_Filter_Method($this, 'handleize'),
'encode_data' => new \Twig_Filter_Method($this, 'encode_data'),
'translate' => new \Twig_Filter_Method($this, 'translate')
);
}
// Functions
public function encode_data($str)
{
$str = base64_encode(json_encode($str));
return $str;
}
public function handleize($str)
{
$str = str_replace('<br/>', '-', $str);
$str = strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($str, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
return $str;
}
public function icons($name)
{
$html = '<svg class="icon ' . $name . '">';
$html .= ' <use xlink:href="#' . $name . '"></use>';
$html .= '</svg>';
return $html;
}
public function get_url()
{
return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
public function translate($key)
{
if (array_key_exists($key, $this->translations)){
return $this->translations->{$key};
}
else {
return $key;
}
}
public function translations()
{
print_r($this->translations);
}
}