Completed
Push — master ( 7a3977...907682 )
by WEBEWEB
02:08
created

UtilityTwigExtension::formatString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
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\TwigFilter;
17
use Twig\TwigFunction;
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
     * Format a string.
60
     *
61
     * @param string $string The string.
62
     * @param string $format The format.
63
     * @return string Returns the formatted string.
64
     */
65
    public function formatString($string, $format) {
66
67
        $fmt = str_replace("_", "%s", $format);
68
        $str = str_split($string);
69
70
        return vsprintf($fmt, $str);
71
    }
72
73
    /**
74
     * Get the Twig filters.
75
     *
76
     * @return TwigFilter[] Returns the Twig filters.
77
     */
78
    public function getFilters() {
79
        return [
80
            new TwigFilter("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
81
82
            new TwigFilter("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
83
            new TwigFilter("fmtDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
84
85
            new TwigFilter("formatString", [$this, "formatString"], ["is_safe" => ["html"]]),
86
            new TwigFilter("fmtString", [$this, "formatString"], ["is_safe" => ["html"]]),
87
88
            new TwigFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
89
90
            new TwigFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
91
92
            new TwigFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]),
93
        ];
94
    }
95
96
    /**
97
     * Get the Twig functions.
98
     *
99
     * @return TwigFunction[] Returns the Twig functions.
100
     */
101
    public function getFunctions() {
102
        return [
103
            new TwigFunction("calcAge", [$this, "calcAge"], ["is_safe" => ["html"]]),
104
105
            new TwigFunction("formatDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
106
            new TwigFunction("fmtDate", [$this, "formatDate"], ["is_safe" => ["html"]]),
107
108
            new TwigFunction("formatString", [$this, "formatString"], ["is_safe" => ["html"]]),
109
            new TwigFunction("fmtString", [$this, "formatString"], ["is_safe" => ["html"]]),
110
111
            new TwigFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
112
113
            new TwigFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
114
115
            new TwigFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]),
116
        ];
117
    }
118
119
    /**
120
     * Decodes HTML entities.
121
     *
122
     * @param string $string The string.
123
     * @return string Returns the decoded HTML entities.
124
     */
125
    public function htmlEntityDecode($string) {
126
        if (null === $string) {
127
            return "";
128
        }
129
        return html_entity_decode($string);
130
    }
131
132
    /**
133
     * Encodes HTML entities.
134
     *
135
     * @param string $string The string.
136
     * @return string Returns the encoded HTML entities.
137
     */
138
    public function htmlEntityEncode($string) {
139
        if (null === $string) {
140
            return "";
141
        }
142
        return htmlentities($string);
143
    }
144
145
    /**
146
     * MD5.
147
     *
148
     * @param string $string The string.
149
     * @return string Returns the MD5.
150
     */
151
    public function md5($string) {
152
        if (null === $string) {
153
            return "";
154
        }
155
        return md5($string);
156
    }
157
}
158