forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilters.php
More file actions
244 lines (208 loc) · 6.01 KB
/
Copy pathFilters.php
File metadata and controls
244 lines (208 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\View;
use Config\Services;
use NumberFormatter;
/**
* View filters
*/
class Filters
{
/**
* Returns $value as all lowercase with the first letter capitalized.
*/
public static function capitalize(string $value): string
{
return ucfirst(strtolower($value));
}
/**
* Formats a date into the given $format.
*
* @param mixed $value
*/
public static function date($value, string $format): string
{
if (is_string($value) && ! is_numeric($value)) {
$value = strtotime($value);
}
return date($format, $value);
}
/**
* Given a string or DateTime object, will return the date modified
* by the given value. Returns the value as a unix timestamp
*
* Example:
* my_date|date_modify(+1 day)
*
* @param string $value
*
* @return false|int
*/
public static function date_modify($value, string $adjustment)
{
$value = static::date($value, 'Y-m-d H:i:s');
return strtotime($adjustment, strtotime($value));
}
/**
* Returns the given default value if $value is empty or undefined.
*
* @param mixed $value
*/
public static function default($value, string $default): string
{
return empty($value)
? $default
: $value;
}
/**
* Escapes the given value with our `esc()` helper function.
*
* @param string $value
*/
public static function esc($value, string $context = 'html'): string
{
return esc($value, $context);
}
/**
* Returns an excerpt of the given string.
*/
public static function excerpt(string $value, string $phrase, int $radius = 100): string
{
helper('text');
return excerpt($value, $phrase, $radius);
}
/**
* Highlights a given phrase within the text using '<mark></mark>' tags.
*/
public static function highlight(string $value, string $phrase): string
{
helper('text');
return highlight_phrase($value, $phrase);
}
/**
* Highlights code samples with HTML/CSS.
*
* @param string $value
*/
public static function highlight_code($value): string
{
helper('text');
return highlight_code($value);
}
/**
* Limits the number of characters to $limit, and trails of with an ellipsis.
* Will break at word break so may be more or less than $limit.
*
* @param string $value
*/
public static function limit_chars($value, int $limit = 500): string
{
helper('text');
return character_limiter($value, $limit);
}
/**
* Limits the number of words to $limit, and trails of with an ellipsis.
*
* @param string $value
*/
public static function limit_words($value, int $limit = 100): string
{
helper('text');
return word_limiter($value, $limit);
}
/**
* Returns the $value displayed in a localized manner.
*
* @param float|int $value
*/
public static function local_number($value, string $type = 'decimal', int $precision = 4, ?string $locale = null): string
{
helper('number');
$types = [
'decimal' => NumberFormatter::DECIMAL,
'currency' => NumberFormatter::CURRENCY,
'percent' => NumberFormatter::PERCENT,
'scientific' => NumberFormatter::SCIENTIFIC,
'spellout' => NumberFormatter::SPELLOUT,
'ordinal' => NumberFormatter::ORDINAL,
'duration' => NumberFormatter::DURATION,
];
return format_number($value, $precision, $locale, ['type' => $types[$type]]);
}
/**
* Returns the $value displayed as a currency string.
*
* @param float|int $value
* @param int $fraction
*/
public static function local_currency($value, string $currency, ?string $locale = null, $fraction = null): string
{
helper('number');
$options = [
'type' => NumberFormatter::CURRENCY,
'currency' => $currency,
'fraction' => $fraction,
];
return format_number($value, 2, $locale, $options);
}
/**
* Returns a string with all instances of newline character (\n)
* converted to an HTML <br/> tag.
*/
public static function nl2br(string $value): string
{
$typography = Services::typography();
return $typography->nl2brExceptPre($value);
}
/**
* Takes a body of text and uses the auto_typography() method to
* turn it into prettier, easier-to-read, prose.
*/
public static function prose(string $value): string
{
$typography = Services::typography();
return $typography->autoTypography($value);
}
/**
* Rounds a given $value in one of 3 ways;
*
* - common Normal rounding
* - ceil always rounds up
* - floor always rounds down
*
* @param mixed $precision
*
* @return float|string
*/
public static function round(string $value, $precision = 2, string $type = 'common')
{
if (! is_numeric($precision)) {
$type = $precision;
$precision = 2;
}
switch ($type) {
case 'common':
return round((float) $value, $precision);
case 'ceil':
return ceil((float) $value);
case 'floor':
return floor((float) $value);
}
// Still here, just return the value.
return $value;
}
/**
* Returns a "title case" version of the string.
*/
public static function title(string $value): string
{
return ucwords(strtolower($value));
}
}