1 | <?php |
||||
2 | |||||
3 | namespace Common\Core\Twig\Extensions; |
||||
4 | |||||
5 | use SpoonFilter; |
||||
6 | |||||
7 | /** |
||||
8 | * Contains Base Frontend-related custom modifiers. |
||||
9 | * These filters work independent of front/backend. |
||||
10 | */ |
||||
11 | class BaseTwigModifiers |
||||
12 | { |
||||
13 | /** |
||||
14 | * Format a number as currency |
||||
15 | * syntax: {{ $string|formatcurrency($currency, $decimals) }}. |
||||
16 | * |
||||
17 | * @param float $number The string to form. |
||||
18 | * @param string $currency The currency to will be used to format the number. |
||||
19 | * @param int $decimals The number of decimals to show. |
||||
20 | * |
||||
21 | * @return string |
||||
22 | */ |
||||
23 | public static function formatCurrency(float $number, string $currency = 'EUR', int $decimals = null): string |
||||
24 | { |
||||
25 | $decimals = $decimals === null ? 2 : $decimals; |
||||
26 | |||||
27 | // @later get settings from backend |
||||
28 | switch ($currency) { |
||||
29 | case 'EUR': |
||||
30 | $currency = '€'; |
||||
31 | break; |
||||
32 | default: |
||||
33 | } |
||||
34 | |||||
35 | return $currency . ' ' . static::formatNumber($number, $decimals); |
||||
36 | } |
||||
37 | |||||
38 | /** |
||||
39 | * Fallback for if our parent functions don't implement this method |
||||
40 | * |
||||
41 | * @param float $number |
||||
42 | * @param int $decimals |
||||
43 | * |
||||
44 | * @return string |
||||
45 | */ |
||||
46 | public static function formatNumber(float $number, int $decimals = null): string |
||||
47 | { |
||||
48 | if ($decimals === null) { |
||||
49 | $decimals = 2; |
||||
50 | } |
||||
51 | |||||
52 | return number_format($number, $decimals, ',', ' '); |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * Highlights all strings in <code> tags. |
||||
57 | * syntax: {{ $string|highlight }}. |
||||
58 | * |
||||
59 | * @param string $string The string passed from the template. |
||||
60 | * |
||||
61 | * @return string |
||||
62 | */ |
||||
63 | public static function highlightCode(string $string): string |
||||
64 | { |
||||
65 | // regex pattern |
||||
66 | $pattern = '/<code>.*?<\/code>/is'; |
||||
67 | |||||
68 | // find matches |
||||
69 | if (preg_match_all($pattern, $string, $matches)) { |
||||
70 | // loop matches |
||||
71 | foreach ($matches[0] as $match) { |
||||
72 | // encase content in highlight_string |
||||
73 | $string = str_replace($match, highlight_string($match, true), $string); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
74 | |||||
75 | // replace highlighted code tags in match |
||||
76 | $string = str_replace(['<code>', '</code>'], '', $string); |
||||
77 | } |
||||
78 | } |
||||
79 | |||||
80 | return $string; |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * Get a random var between a min and max |
||||
85 | * syntax: {{ rand($min, $max) }}. |
||||
86 | * |
||||
87 | * @param int $min The minimum random number. |
||||
88 | * @param int $max The maximum random number. |
||||
89 | * |
||||
90 | * @return int |
||||
91 | */ |
||||
92 | public static function random(int $min, int $max): int |
||||
93 | { |
||||
94 | return random_int($min, $max); |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * Convert a multi line string into a string without newlines so it can be handles by JS |
||||
99 | * syntax: {{ $string|stripnewlines }}. |
||||
100 | * |
||||
101 | * @param string $string The variable that should be processed. |
||||
102 | * |
||||
103 | * @return string |
||||
104 | */ |
||||
105 | 2 | public static function stripNewlines(string $string): string |
|||
106 | { |
||||
107 | 2 | return str_replace(["\r\n", "\n", "\r"], ' ', $string); |
|||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * Transform the string to uppercase. |
||||
112 | * syntax: {{ $string|uppercase }}. |
||||
113 | * |
||||
114 | * @param string $string The string that you want to apply this method on. |
||||
115 | * |
||||
116 | * @return string The string, completly uppercased. |
||||
117 | */ |
||||
118 | 38 | public static function uppercase(string $string): string |
|||
119 | { |
||||
120 | 38 | return mb_convert_case($string, MB_CASE_UPPER, \Spoon::getCharset()); |
|||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * Makes this string lowercase. |
||||
125 | * syntax: {{ $string|lowercase }}. |
||||
126 | * |
||||
127 | * |
||||
128 | * @param string $string The string that you want to apply this method on. |
||||
129 | * |
||||
130 | * @return string The string, completely lowercased. |
||||
131 | */ |
||||
132 | public static function lowercase(string $string): string |
||||
133 | { |
||||
134 | return mb_convert_case($string, MB_CASE_LOWER, \Spoon::getCharset()); |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * snakeCase Converter. |
||||
139 | * syntax: {{ $string|snakecase }}. |
||||
140 | * |
||||
141 | * @internal Untested, Needs testing |
||||
142 | * |
||||
143 | * @param string $string |
||||
144 | * |
||||
145 | * @return string |
||||
146 | */ |
||||
147 | public static function snakeCase(string $string): string |
||||
148 | { |
||||
149 | return ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $string)), '_'); |
||||
150 | } |
||||
151 | |||||
152 | /** |
||||
153 | * CamelCase Converter. |
||||
154 | * syntax: {{ $string|camelcase }}. |
||||
155 | * |
||||
156 | * @internal Untested, Needs testing |
||||
157 | * |
||||
158 | * @param string $string |
||||
159 | * |
||||
160 | * @return string |
||||
161 | */ |
||||
162 | public static function camelCase(string $string): string |
||||
163 | { |
||||
164 | // non-alpha and non-numeric characters become spaces |
||||
165 | $string = preg_replace('/[^a-z0-9' . implode('', []) . ']+/i', ' ', $string); |
||||
166 | $string = trim($string); |
||||
167 | // uppercase the first character of each word |
||||
168 | $string = ucwords($string); |
||||
169 | $string = str_replace(' ', '', $string); |
||||
170 | $string = lcfirst($string); |
||||
171 | |||||
172 | return $string; |
||||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * Formats a language specific date. |
||||
177 | * syntax: {{ $timestamp|spoondate($format, $language) }}. |
||||
178 | * |
||||
179 | * @param string|int $timestamp The timestamp or date that you want to apply the format to. |
||||
180 | * @param string $format The optional format that you want to apply on the provided timestamp. |
||||
181 | * @param string $language The optional language that you want this format in (Check SpoonLocale for the possible languages). |
||||
182 | * |
||||
183 | * @return string The formatted date according to the timestamp, format and provided language. |
||||
184 | */ |
||||
185 | 9 | public static function spoonDate($timestamp, $format = 'Y-m-d H:i:s', $language = 'en') |
|||
186 | { |
||||
187 | 9 | if (is_string($timestamp) && !is_numeric($timestamp)) { |
|||
188 | // use strptime if you want to restrict the input format |
||||
189 | $timestamp = strtotime($timestamp); |
||||
190 | } |
||||
191 | |||||
192 | 9 | return \SpoonDate::getDate($format, $timestamp, $language); |
|||
0 ignored issues
–
show
It seems like
$timestamp can also be of type string ; however, parameter $timestamp of SpoonDate::getDate() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
193 | } |
||||
194 | |||||
195 | /** |
||||
196 | * Shows a v or x to indicate the boolean state (Y|N, j|n, true|false). |
||||
197 | * syntax: {{ showbool($status, $reverse) }}. |
||||
198 | * |
||||
199 | * @param string|bool $status |
||||
200 | * @param bool $reverse show the opposite of the status |
||||
201 | * |
||||
202 | * @return string |
||||
203 | */ |
||||
204 | public static function showBool($status, bool $reverse = false): string |
||||
205 | { |
||||
206 | $showTrue = '<strong style="color:green">✓</strong>'; |
||||
207 | $showFalse = '<strong style="color:red">✘</strong>'; |
||||
208 | |||||
209 | if ($status === 'Y' || $status === 'y' || $status === 1 || $status === '1' || $status === true) { |
||||
210 | return $reverse ? self::showBool(false) : $showTrue; |
||||
211 | } |
||||
212 | |||||
213 | if ($status === 'N' || $status === 'n' || $status === 0 || $status === '0' || $status === false) { |
||||
214 | return $reverse ? self::showBool(true) : $showFalse; |
||||
215 | } |
||||
216 | |||||
217 | return $status; |
||||
218 | } |
||||
219 | |||||
220 | /** |
||||
221 | * Truncate a string |
||||
222 | * syntax: {{ $string|truncate($max-length, $append-hellip, $closest-word) }}. |
||||
223 | * |
||||
224 | * @param null|string $string The string passed from the template. |
||||
225 | * @param int $length The maximum length of the truncated string. |
||||
226 | * @param bool $useHellip Should a hellip be appended if the length exceeds the requested length? |
||||
227 | * @param bool $closestWord Truncate on exact length or on closest word? |
||||
228 | * |
||||
229 | * @return string |
||||
230 | */ |
||||
231 | 2 | public static function truncate( |
|||
232 | ?string $string, |
||||
233 | int $length, |
||||
234 | bool $useHellip = true, |
||||
235 | bool $closestWord = false |
||||
236 | ): string { |
||||
237 | 2 | if ($string === null) { |
|||
238 | return ''; |
||||
239 | } |
||||
240 | |||||
241 | // remove special chars, all of them, also the ones that shouldn't be there. |
||||
242 | 2 | $string = SpoonFilter::htmlentitiesDecode($string, null, ENT_QUOTES); |
|||
243 | |||||
244 | // remove HTML |
||||
245 | 2 | $string = strip_tags($string); |
|||
246 | |||||
247 | // less characters |
||||
248 | 2 | if (mb_strlen($string) <= $length) { |
|||
249 | 2 | return SpoonFilter::htmlspecialchars($string); |
|||
250 | } |
||||
251 | |||||
252 | // more characters |
||||
253 | // hellip is seen as 1 char, so remove it from length |
||||
254 | 2 | if ($useHellip) { |
|||
255 | 2 | --$length; |
|||
256 | } |
||||
257 | |||||
258 | // truncate |
||||
259 | 2 | $string = $closestWord |
|||
260 | 2 | ? mb_substr($string, 0, strrpos(substr($string, 0, $length + 1), ' '), 'UTF-8') |
|||
261 | 2 | : mb_substr($string, 0, $length, 'UTF8'); |
|||
262 | |||||
263 | // add hellip |
||||
264 | 2 | if ($useHellip) { |
|||
265 | 2 | $string .= '…'; |
|||
266 | } |
||||
267 | |||||
268 | // return |
||||
269 | 2 | return SpoonFilter::htmlspecialchars($string, ENT_QUOTES); |
|||
270 | } |
||||
271 | } |
||||
272 |