|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2019 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use Twig_Environment; |
|
16
|
|
|
use Twig_SimpleFilter; |
|
17
|
|
|
use Twig_SimpleFunction; |
|
18
|
|
|
use WBW\Bundle\CoreBundle\Renderer\DateTimeRenderer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Utility Twig extension. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
|
25
|
|
|
*/ |
|
26
|
|
|
class UtilityTwigExtension extends AbstractTwigExtension { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Service name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
const SERVICE_NAME = "webeweb.core.twig.extension.utility"; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Twig_Environment $twigEnvironment The wig environment. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Twig_Environment $twigEnvironment) { |
|
41
|
|
|
parent::__construct($twigEnvironment); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Format a date/time. |
|
46
|
|
|
* |
|
47
|
|
|
* @param DateTime|null $dateTime The date/time. |
|
48
|
|
|
* @param string $format The format. |
|
49
|
|
|
* @return string Returns the formatted date/time. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function formatDate(DateTime $dateTime = null, $format = DateTimeRenderer::DATETIME_FORMAT) { |
|
52
|
|
|
return DateTimeRenderer::renderDateTime($dateTime, $format); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the Twig filters. |
|
57
|
|
|
* |
|
58
|
|
|
* @return Twig_SimpleFilter[] Returns the Twig filters. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getFilters() { |
|
61
|
|
|
return [ |
|
62
|
|
|
new Twig_SimpleFilter("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]), |
|
63
|
|
|
new Twig_SimpleFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]), |
|
64
|
|
|
new Twig_SimpleFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]), |
|
65
|
|
|
new Twig_SimpleFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the Twig functions. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array Returns the Twig functions. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFunctions() { |
|
75
|
|
|
return [ |
|
76
|
|
|
new Twig_SimpleFunction("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]), |
|
77
|
|
|
new Twig_SimpleFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]), |
|
78
|
|
|
new Twig_SimpleFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]), |
|
79
|
|
|
new Twig_SimpleFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]), |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Decodes HTML entities. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $string The string. |
|
87
|
|
|
* @return string Returns the decoded HTML entities. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function htmlEntityDecode($string) { |
|
90
|
|
|
if (null === $string) { |
|
91
|
|
|
return ""; |
|
92
|
|
|
} |
|
93
|
|
|
return html_entity_decode($string); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Encodes HTML entities. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $string The string. |
|
100
|
|
|
* @return string Returns the encoded HTML entities. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function htmlEntityEncode($string) { |
|
103
|
|
|
if (null === $string) { |
|
104
|
|
|
return ""; |
|
105
|
|
|
} |
|
106
|
|
|
return htmlentities($string); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* MD5. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $string The string. |
|
113
|
|
|
* @return string Returns the MD5. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function md5($string) { |
|
116
|
|
|
if (null === $string) { |
|
117
|
|
|
return ""; |
|
118
|
|
|
} |
|
119
|
|
|
return md5($string); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|