Completed
Push — master ( addd3e...e24f70 )
by WEBEWEB
02:21
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_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
     * Calculates an age.
37
     *
38
     * @param DateTime $birthDate The birth date.
39
     * @param DateTime|null $refDate The reference date.
40
     * @return int Returns teh age.
41
     * @throws Exception Throws an exception if an error occurs.
42
     */
43
    public function calcAge(DateTime $birthDate, DateTime $refDate = null) {
44
        return DateTimeRenderer::renderAge($birthDate, $refDate);
45
    }
46
47
    /**
48
     * Format a date/time.
49
     *
50
     * @param DateTime|null $dateTime The date/time.
51
     * @param string $format The format.
52
     * @return string Returns the formatted date/time.
53
     */
54
    public function formatDate(DateTime $dateTime = null, $format = DateTimeRenderer::DATETIME_FORMAT) {
55
        return DateTimeRenderer::renderDateTime($dateTime, $format);
56
    }
57
58
    /**
59
     * Get the Twig filters.
60
     *
61
     * @return Twig_SimpleFilter[] Returns the Twig filters.
62
     */
63
    public function getFilters() {
64
        return [
65
            new Twig_SimpleFilter("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
66
            new Twig_SimpleFilter("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
67
            new Twig_SimpleFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
68
            new Twig_SimpleFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
69
            new Twig_SimpleFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]),
70
        ];
71
    }
72
73
    /**
74
     * Get the Twig functions.
75
     *
76
     * @return array Returns the Twig functions.
77
     */
78
    public function getFunctions() {
79
        return [
80
            new Twig_SimpleFunction("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
81
            new Twig_SimpleFunction("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
82
            new Twig_SimpleFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
83
            new Twig_SimpleFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
84
            new Twig_SimpleFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]),
85
        ];
86
    }
87
88
    /**
89
     * Decodes HTML entities.
90
     *
91
     * @param string $string The string.
92
     * @return string Returns the decoded HTML entities.
93
     */
94
    public function htmlEntityDecode($string) {
95
        if (null === $string) {
96
            return "";
97
        }
98
        return html_entity_decode($string);
99
    }
100
101
    /**
102
     * Encodes HTML entities.
103
     *
104
     * @param string $string The string.
105
     * @return string Returns the encoded HTML entities.
106
     */
107
    public function htmlEntityEncode($string) {
108
        if (null === $string) {
109
            return "";
110
        }
111
        return htmlentities($string);
112
    }
113
114
    /**
115
     * MD5.
116
     *
117
     * @param string $string The string.
118
     * @return string Returns the MD5.
119
     */
120
    public function md5($string) {
121
        if (null === $string) {
122
            return "";
123
        }
124
        return md5($string);
125
    }
126
}
127