• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-bundle / 5375277340

pending completion
5375277340

push

github

webeweb
Update CHANGELOG

1376 of 1394 relevant lines covered (98.71%)

105.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/Twig/Extension/StringTwigExtension.php
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);
17✔
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 [
72✔
56
            new TwigFilter("dateFormat", [$this, "dateFormat"], ["is_safe" => ["html"]]),
136✔
57

58
            new TwigFilter("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
136✔
59
            new TwigFilter("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
136✔
60

61
            new TwigFilter("jsonDecode", [$this, "jsonDecode"], ["is_safe" => ["html"]]),
136✔
62

63
            new TwigFilter("md5", [$this, "md5"], ["is_safe" => ["html"]]),
136✔
64

65
            new TwigFilter("stringExtractUpperCase", [$this, "stringExtractUpperCase"], ["is_safe" => ["html"]]),
136✔
66
            new TwigFilter("stringFormat", [$this, "stringFormat"], ["is_safe" => ["html"]]),
136✔
67
            new TwigFilter("stringHumanReadable", [$this, "stringHumanReadable"], ["is_safe" => ["html"]]),
136✔
68
            new TwigFilter("stringLowerCamelCase", [$this, "stringLowerCamelCase"], ["is_safe" => ["html"]]),
136✔
69
            new TwigFilter("stringSnakeCase", [$this, "stringSnakeCase"], ["is_safe" => ["html"]]),
136✔
70
            new TwigFilter("stringUpperCamelCase", [$this, "stringUpperCamelCase"], ["is_safe" => ["html"]]),
136✔
71
        ];
72✔
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 [
72✔
82
            new TwigFunction("dateFormat", [$this, "dateFormat"], ["is_safe" => ["html"]]),
136✔
83

84
            new TwigFunction("htmlEntityDecode", [$this, "htmlEntityDecode"], ["is_safe" => ["html"]]),
136✔
85
            new TwigFunction("htmlEntityEncode", [$this, "htmlEntityEncode"], ["is_safe" => ["html"]]),
136✔
86

87
            new TwigFunction("jsonDecode", [$this, "jsonDecode"], ["is_safe" => ["html"]]),
136✔
88

89
            new TwigFunction("md5", [$this, "md5"], ["is_safe" => ["html"]]),
136✔
90

91
            new TwigFunction("stringExtractUpperCase", [$this, "stringExtractUpperCase"], ["is_safe" => ["html"]]),
136✔
92
            new TwigFunction("stringFormat", [$this, "stringFormat"], ["is_safe" => ["html"]]),
136✔
93
            new TwigFunction("stringHumanReadable", [$this, "stringHumanReadable"], ["is_safe" => ["html"]]),
136✔
94
            new TwigFunction("stringLowerCamelCase", [$this, "stringLowerCamelCase"], ["is_safe" => ["html"]]),
136✔
95
            new TwigFunction("stringSnakeCase", [$this, "stringSnakeCase"], ["is_safe" => ["html"]]),
136✔
96
            new TwigFunction("stringUniqid", [$this, "stringUniqidFunction"], ["is_safe" => ["html"]]),
136✔
97
            new TwigFunction("stringUpperCamelCase", [$this, "stringUpperCamelCase"], ["is_safe" => ["html"]]),
136✔
98
        ];
72✔
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) {
17✔
110
            return null;
17✔
111
        }
112

113
        return html_entity_decode($string);
17✔
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) {
17✔
125
            return null;
17✔
126
        }
127

128
        return htmlentities($string);
17✔
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) {
17✔
140
            return null;
17✔
141
        }
142

143
        return json_decode($string, true);
17✔
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) {
17✔
155
            return null;
17✔
156
        }
157

158
        return md5($string);
17✔
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);
17✔
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);
17✔
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);
17✔
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);
17✔
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);
17✔
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) {
17✔
224
            return null;
17✔
225
        }
226

227
        if (true === function_exists("random_bytes")) {
17✔
228
            $bytes = random_bytes(ceil($length / 2));
17✔
229
        } else if (true === function_exists("openssl_random_pseudo_bytes")) {
×
230
            $bytes = openssl_random_pseudo_bytes(ceil($length / 2));
×
231
        } else {
232
            throw new RuntimeException("random_bytes() and openssl_random_pseudo_bytes() are not available");
×
233
        }
234

235
        $hex = bin2hex($bytes);
17✔
236

237
        return substr($hex, 0, $length);
17✔
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);
17✔
248
    }
249
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc