Passed
Push — master ( 2d4adf...cc1fa4 )
by WEBEWEB
13:18
created

StringTwigExtension::stringUniqidFunction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 17
rs 9.9332
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2021 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 RuntimeException;
16
use Throwable;
17
use Twig\TwigFilter;
18
use Twig\TwigFunction;
19
use WBW\Library\Types\Helper\DateTimeHelper;
20
use WBW\Library\Types\Helper\StringHelper;
21
22
/**
23
 * String Twig extension.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Bundle\CoreBundle\Twig\Extension
27
 */
28
class StringTwigExtension extends AbstractTwigExtension {
29
30
    /**
31
     * Service name.
32
     *
33
     * @var string
34
     */
35
    const SERVICE_NAME = "wbw.core.twig.extension.string";
36
37
    /**
38
     * Format a date/time.
39
     *
40
     * @param DateTime|null $dateTime The date/time.
41
     * @param string $format The format.
42
     * @return string|null Returns the formatted date/time.
43
     */
44
    public function dateFormat(?DateTime $dateTime, string $format = DateTimeHelper::DATETIME_FORMAT): ?string {
45
        return DateTimeHelper::toString($dateTime, $format);
46
    }
47
48
    /**
49
     * Get the Twig filters.
50
     *
51
     * @return TwigFilter[] Returns the Twig filters.
52
     */
53
    public function getFilters(): array {
54
55
        return [
56
            new TwigFilter("dateFormat", [$this, "dateFormat"], ["is_safe" => ["html"]]),
57
58
            new TwigFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
59
            new TwigFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
60
61
            new TwigFilter("jsonDecode", [$this, "jsonDecode"], ["is_safe" => ["html"]]),
62
63
            new TwigFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]),
64
65
            new TwigFilter("stringExtractUpperCase", [$this, "stringExtractUpperCase"], ["is_safe" => ["html"]]),
66
            new TwigFilter("stringFormat", [$this, "stringFormat"], ["is_safe" => ["html"]]),
67
            new TwigFilter("stringHumanReadable", [$this, "stringHumanReadable"], ["is_safe" => ["html"]]),
68
            new TwigFilter("stringLowerCamelCase", [$this, "stringLowerCamelCase"], ["is_safe" => ["html"]]),
69
            new TwigFilter("stringSnakeCase", [$this, "stringSnakeCase"], ["is_safe" => ["html"]]),
70
            new TwigFilter("stringUpperCamelCase", [$this, "stringUpperCamelCase"], ["is_safe" => ["html"]]),
71
        ];
72
    }
73
74
    /**
75
     * Get the Twig functions.
76
     *
77
     * @return TwigFunction[] Returns the Twig functions.
78
     */
79
    public function getFunctions(): array {
80
81
        return [
82
            new TwigFunction("dateFormat", [$this, "dateFormat"], ["is_safe" => ["html"]]),
83
84
            new TwigFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
85
            new TwigFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
86
87
            new TwigFunction("jsonDecode", [$this, "jsonDecode"], ["is_safe" => ["html"]]),
88
89
            new TwigFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]),
90
91
            new TwigFunction("stringExtractUpperCase", [$this, "stringExtractUpperCase"], ["is_safe" => ["html"]]),
92
            new TwigFunction("stringFormat", [$this, "stringFormat"], ["is_safe" => ["html"]]),
93
            new TwigFunction("stringHumanReadable", [$this, "stringHumanReadable"], ["is_safe" => ["html"]]),
94
            new TwigFunction("stringLowerCamelCase", [$this, "stringLowerCamelCase"], ["is_safe" => ["html"]]),
95
            new TwigFunction("stringSnakeCase", [$this, "stringSnakeCase"], ["is_safe" => ["html"]]),
96
            new TwigFunction("stringUniqid", [$this, "stringUniqidFunction"], ["is_safe" => ["html"]]),
97
            new TwigFunction("stringUpperCamelCase", [$this, "stringUpperCamelCase"], ["is_safe" => ["html"]]),
98
        ];
99
    }
100
101
    /**
102
     * Decode HTML entities.
103
     *
104
     * @param string|null $string The string.
105
     * @return string|null Returns the decoded HTML entities.
106
     */
107
    public function htmlEntityDecode(?string $string): ?string {
108
109
        if (null === $string) {
110
            return null;
111
        }
112
113
        return html_entity_decode($string);
114
    }
115
116
    /**
117
     * Encode HTML entities.
118
     *
119
     * @param string|null $string The string.
120
     * @return string|null Returns the encoded HTML entities.
121
     */
122
    public function htmlEntityEncode(?string $string): ?string {
123
124
        if (null === $string) {
125
            return null;
126
        }
127
128
        return htmlentities($string);
129
    }
130
131
    /**
132
     * Decode a JSON string.
133
     *
134
     * @param string|null $string The string.
135
     * @return array|null Returns the decoded JSON string.
136
     */
137
    public function jsonDecode(?string $string): ?array {
138
139
        if (null === $string) {
140
            return null;
141
        }
142
143
        return json_decode($string, true);
144
    }
145
146
    /**
147
     * MD5.
148
     *
149
     * @param string|null $string The string.
150
     * @return string|null Returns the MD5.
151
     */
152
    public function md5(?string $string): ?string {
153
154
        if (null === $string) {
155
            return null;
156
        }
157
158
        return md5($string);
159
    }
160
161
    /**
162
     * Display the extracted upper case letters.
163
     *
164
     * @param string|null $str The string.
165
     * @param bool $lower Lower case ?
166
     * @return string|null Returns the extracted upper case letters.
167
     */
168
    public function stringExtractUpperCase(?string $str, bool $lower = false): ?string {
169
        return StringHelper::extractUpperCase($str, $lower);
170
    }
171
172
    /**
173
     * Display a formatted string.
174
     *
175
     * @param string|null $string The string.
176
     * @param string|null $format The format.
177
     * @return string|null Returns the formatted string.
178
     */
179
    public function stringFormat(?string $string, ?string $format): ?string {
180
        return StringHelper::format($string, $format);
181
    }
182
183
    /**
184
     * Display an human-readable string.
185
     *
186
     * @param string|null $str The string.
187
     * @return string|null Returns the human-readable string.
188
     */
189
    public function stringHumanReadable(?string $str): ?string {
190
        return StringHelper::toHumanReadable($str);
191
    }
192
193
    /**
194
     * Display a lower camel case string.
195
     *
196
     * @param string|null $str The string.
197
     * @return string|null Returns the lower camel case string.
198
     */
199
    public function stringLowerCamelCase(?string $str): ?string {
200
        return StringHelper::toLowerCamelCase($str);
201
    }
202
203
    /**
204
     * Display a snake case string.
205
     *
206
     * @param string|null $str The string.
207
     * @param string $sep The separator.
208
     * @return string|null Returns the snake case.
209
     */
210
    public function stringSnakeCase(?string $str, string $sep = "_"): ?string {
211
        return StringHelper::toSnakeCase($str, $sep);
212
    }
213
214
    /**
215
     * Display a unique id.
216
     *
217
     * @param int $length The length.
218
     * @return string|null Returns the unique id.
219
     * @throws Throwable Throws an exception if an error occurs.
220
     */
221
    public function stringUniqidFunction(int $length = 13): ?string {
222
223
        if ($length < 1) {
224
            return null;
225
        }
226
227
        if (true === function_exists("random_bytes")) {
228
            $bytes = random_bytes(ceil($length / 2));
0 ignored issues
show
Bug introduced by
ceil($length / 2) of type double is incompatible with the type integer expected by parameter $length of random_bytes(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

228
            $bytes = random_bytes(/** @scrutinizer ignore-type */ ceil($length / 2));
Loading history...
229
        } else if (true === function_exists("openssl_random_pseudo_bytes")) {
230
            $bytes = openssl_random_pseudo_bytes(ceil($length / 2));
0 ignored issues
show
Bug introduced by
ceil($length / 2) of type double is incompatible with the type integer expected by parameter $length of openssl_random_pseudo_bytes(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
            $bytes = openssl_random_pseudo_bytes(/** @scrutinizer ignore-type */ ceil($length / 2));
Loading history...
231
        } else {
232
            throw new RuntimeException("random_bytes() and openssl_random_pseudo_bytes() are not available");
233
        }
234
235
        $hex = bin2hex($bytes);
236
237
        return substr($hex, 0, $length);
238
    }
239
240
    /**
241
     * Display an upper camel case string.
242
     *
243
     * @param string|null $str The string.
244
     * @return string|null Returns the upper camel case string.
245
     */
246
    public function stringUpperCamelCase(?string $str): ?string {
247
        return StringHelper::toUpperCamelCase($str);
248
    }
249
}
250