Completed
Push — master ( b362df...addd3e )
by WEBEWEB
02:33
created

UtilityTwigExtension::formatDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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 Exception;
16
use Twig_Environment;
17
use Twig_SimpleFilter;
18
use Twig_SimpleFunction;
19
use WBW\Bundle\CoreBundle\Renderer\DateTimeRenderer;
20
21
/**
22
 * Utility Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\CoreBundle\Twig\Extension
26
 */
27
class UtilityTwigExtension extends AbstractTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.core.twig.extension.utility";
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Twig_Environment $twigEnvironment The wig environment.
40
     */
41
    public function __construct(Twig_Environment $twigEnvironment) {
42
        parent::__construct($twigEnvironment);
43
    }
44
45
    /**
46
     * Calculates an age.
47
     *
48
     * @param DateTime $birthDate The birth date.
49
     * @param DateTime|null $refDate The reference date.
50
     * @return int Returns teh age.
51
     * @throws Exception Throws an exception if an error occurs.
52
     */
53
    public function calcAge(DateTime $birthDate, DateTime $refDate = null) {
54
        return DateTimeRenderer::renderAge($birthDate, $refDate);
55
    }
56
57
    /**
58
     * Format a date/time.
59
     *
60
     * @param DateTime|null $dateTime The date/time.
61
     * @param string $format The format.
62
     * @return string Returns the formatted date/time.
63
     */
64
    public function formatDate(DateTime $dateTime = null, $format = DateTimeRenderer::DATETIME_FORMAT) {
65
        return DateTimeRenderer::renderDateTime($dateTime, $format);
66
    }
67
68
    /**
69
     * Get the Twig filters.
70
     *
71
     * @return Twig_SimpleFilter[] Returns the Twig filters.
72
     */
73
    public function getFilters() {
74
        return [
75
            new Twig_SimpleFilter("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
76
            new Twig_SimpleFilter("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
77
            new Twig_SimpleFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
78
            new Twig_SimpleFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
79
            new Twig_SimpleFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]),
80
        ];
81
    }
82
83
    /**
84
     * Get the Twig functions.
85
     *
86
     * @return array Returns the Twig functions.
87
     */
88
    public function getFunctions() {
89
        return [
90
            new Twig_SimpleFunction("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
91
            new Twig_SimpleFunction("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
92
            new Twig_SimpleFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
93
            new Twig_SimpleFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
94
            new Twig_SimpleFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]),
95
        ];
96
    }
97
98
    /**
99
     * Decodes HTML entities.
100
     *
101
     * @param string $string The string.
102
     * @return string Returns the decoded HTML entities.
103
     */
104
    public function htmlEntityDecode($string) {
105
        if (null === $string) {
106
            return "";
107
        }
108
        return html_entity_decode($string);
109
    }
110
111
    /**
112
     * Encodes HTML entities.
113
     *
114
     * @param string $string The string.
115
     * @return string Returns the encoded HTML entities.
116
     */
117
    public function htmlEntityEncode($string) {
118
        if (null === $string) {
119
            return "";
120
        }
121
        return htmlentities($string);
122
    }
123
124
    /**
125
     * MD5.
126
     *
127
     * @param string $string The string.
128
     * @return string Returns the MD5.
129
     */
130
    public function md5($string) {
131
        if (null === $string) {
132
            return "";
133
        }
134
        return md5($string);
135
    }
136
}
137