forked from CodelyTV/php-ddd-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.php
More file actions
50 lines (40 loc) · 1.31 KB
/
Copy pathutils.php
File metadata and controls
50 lines (40 loc) · 1.31 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
<?php
declare(strict_types = 1);
namespace CodelyTv\Utils\Shared;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use function Lambdish\Phunctional\filter_null;
use function Lambdish\Phunctional\map;
function date_to_string(DateTimeInterface $date): string
{
$timestamp = $date->getTimestamp();
$microseconds = $date->format('u');
$millisecondsOnASecond = 1000;
return (string) (((float) ($timestamp . '.' . $microseconds)) * $millisecondsOnASecond);
}
function string_to_date($milliseconds): DateTimeImmutable
{
$millisecondsOnASecond = 1000;
$asSeconds = (int) floor($milliseconds / $millisecondsOnASecond);
$dateTime = new DateTimeImmutable('@' . ((string) $asSeconds), new DateTimeZone('UTC'));
return new DateTimeImmutable(
$dateTime->format('Y-m-d\TH:i:s') .
'.' .
sprintf('%03d', $milliseconds % $millisecondsOnASecond) .
'000' .
$dateTime->format('O')
);
}
function snake_to_camel($word)
{
return lcfirst(str_replace('_', '', ucwords($word, '_')));
}
function camel_to_snake($word)
{
return ctype_lower($word) ? $word : strtolower(preg_replace('/([^A-Z\s])([A-Z])/', '$1_$2', $word));
}
function map_no_null(callable $fn, $coll)
{
return filter_null(map($fn, $coll));
}