-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat.hh
271 lines (230 loc) · 7.73 KB
/
Format.hh
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Utility;
use \DateTime;
use \Indexish;
/**
* Format provides utility methods for converting raw data to specific visual formats.
*
* @package Titon\Utility
*/
class Format {
/**
* Format a date string to an Atom feed format.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @return string
*/
public static function atom(mixed $time): string {
return date(DateTime::ATOM, Time::toUnix($time));
}
/**
* Format a date string.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @param string $format
* @return string
*/
public static function date(mixed $time, string $format = '%Y-%m-%d'): string {
return strftime($format, Time::toUnix($time));
}
/**
* Format a datetime string.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @param string $format
* @return string
*/
public static function datetime(mixed $time, string $format = '%Y-%m-%d %H:%M:%S'): string {
return strftime($format, Time::toUnix($time));
}
/**
* Format a value to a certain string sequence. All #'s in the format will be replaced by the character in the same position within the sequence.
* All *'s will mask the character in the sequence. Large numbers should be passed as strings.
*
* @example {
* Format::format(1234567890, '(###) ###-####'); (123) 456-7890
* Format::format(1234567890123456, '****-****-####-####'); ***-****-9012-3456
* }
*
* @param string|int $value
* @param string $format
* @return string
*/
public static function format(string $value, string $format): string {
$length = mb_strlen($format);
$result = '';
$pos = 0;
for ($i = 0; $i < $length; $i++) {
$char = $format[$i];
if ($char === '#') {
$raw = mb_substr($value, $pos, 1);
$result .= ($raw === false || $raw === '') ? '#' : $raw;
$pos++;
} else if ($char === '*') {
$result .= $char;
$pos++;
} else {
$result .= $char;
}
}
return $result;
}
/**
* Format a date string to an HTTP header format.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @return string
*/
public static function http(mixed $time): string {
return gmdate('D, d M Y H:i:s T', Time::toUnix($time));
}
/**
* Format a phone number. A phone number can support multiple variations,
* depending on how many numbers are present.
*
* @param string $value
* @param string|Map $format
* @return string
*/
public static function phone(string $value, mixed $format): string {
$value = preg_replace('/[^0-9]+/', '', $value);
$pattern = $format;
if ($format instanceof Map) {
$length = mb_strlen($value);
if ($length >= 11 && $format->contains(11)) {
$pattern = $format[11];
} else if ($length >= 10 && $format->contains(10)) {
$pattern = $format[10];
} else if ($format->contains(7)) {
$pattern = $format[7];
} else {
$pattern = str_repeat('#', $length);
}
}
return static::format($value, (string) $pattern);
}
/**
* Format a timestamp as a date relative human readable string; also known as time ago in words.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @param \Titon\Utility\OptionMap $options
* @param \Titon\Utility\TimeMessageMap $messages
* @return string
*/
public static function relativeTime(mixed $time, OptionMap $options = Map {}, TimeMessageMap $messages = Map {}): string {
$options = (Map {
'now' => 'just now',
'in' => 'in {time}',
'ago' => '{time} ago',
'separator' => ', ',
'verbose' => true,
'depth' => 2,
'time' => time()
})->setAll($options);
// Merged child messages
$baseMessages = Map {
'seconds' => Map {0 => '{count}s', 1 => '{count} second', 2 => '{count} seconds'},
'minutes' => Map {0 => '{count}m', 1 => '{count} minute', 2 => '{count} minutes'},
'hours' => Map {0 => '{count}h', 1 => '{count} hour', 2 => '{count} hours'},
'days' => Map {0 => '{count}d', 1 => '{count} day', 2 => '{count} days'},
'weeks' => Map {0 => '{count}w', 1 => '{count} week', 2 => '{count} weeks'},
'months' => Map {0 => '{count}m', 1 => '{count} month', 2 => '{count} months'},
'years' => Map {0 => '{count}y', 1 => '{count} year', 2 => '{count} years'}
};
foreach ($messages as $timeFrame => $childMessages) {
$baseMessages[$timeFrame]->setAll($childMessages);
}
$messages = $baseMessages;
$diff = Time::difference($options['time'], Time::toUnix($time));
// Present tense
if ($diff === 0) {
return (string) $options['now'];
}
// Past or future tense
$seconds = abs($diff);
$depth = (int) $options['depth'];
$output = Vector {};
while ($seconds > 0 && $depth > 0) {
if ($seconds >= Time::YEAR) {
$key = 'years';
$div = Time::YEAR;
} else if ($seconds >= Time::MONTH) {
$key = 'months';
$div = Time::MONTH;
} else if ($seconds >= Time::WEEK) {
$key = 'weeks';
$div = Time::WEEK;
} else if ($seconds >= Time::DAY) {
$key = 'days';
$div = Time::DAY;
} else if ($seconds >= Time::HOUR) {
$key = 'hours';
$div = Time::HOUR;
} else if ($seconds >= Time::MINUTE) {
$key = 'minutes';
$div = Time::MINUTE;
} else {
$key = 'seconds';
$div = Time::SECOND;
}
$count = (int) round($seconds / $div);
$seconds -= ($count * $div);
if ($options['verbose']) {
$index = ($count === 1) ? 1 : 2;
} else {
$index = 0;
}
$output[] = Str::insert((string) $messages[$key][$index], Map {'count' => $count});
$depth--;
}
return Str::insert((string) $options[($diff > 0) ? 'ago' : 'in'], Map {'time' => implode((string) $options['separator'], $output)});
}
/**
* Format a date string to an RSS feed format.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @return string
*/
public static function rss(mixed $time): string {
return date(DateTime::RSS, Time::toUnix($time));
}
/**
* Format a social security number.
*
* @param string|int $value
* @param string $format
* @return string
*/
public static function ssn(string $value, string $format): string {
return static::format($value, $format);
}
/**
* Format a time string.
*
* @uses Titon\Utility\Time
*
* @param string|int $time
* @param string $format
* @return string
*/
public static function time(mixed $time, string $format = '%H:%M:%S'): string {
return strftime($format, Time::toUnix($time));
}
}