-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathUtils.php
73 lines (59 loc) · 1.65 KB
/
Utils.php
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
<?php
declare(strict_types=1);
namespace CodelyTv\Shared\Domain;
use DateTimeImmutable;
use DateTimeInterface;
use function Lambdish\Phunctional\filter;
final class Utils
{
public static function dateToString(DateTimeInterface $date): string
{
return $date->format(DateTimeInterface::ATOM);
}
public static function stringToDate(string $date): DateTimeImmutable
{
return new DateTimeImmutable($date);
}
public static function jsonEncode(array $values): string
{
return json_encode($values, JSON_THROW_ON_ERROR);
}
public static function jsonDecode(string $json): array
{
return json_decode($json, true, flags: JSON_THROW_ON_ERROR);
}
public static function toSnakeCase(string $text): string
{
return ctype_lower($text) ? $text : strtolower((string) preg_replace('/([^A-Z\s])([A-Z])/', '$1_$2', $text));
}
public static function toCamelCase(string $text): string
{
return lcfirst(str_replace('_', '', ucwords($text, '_')));
}
public static function dot(array $array, string $prepend = ''): array
{
$results = [];
foreach ($array as $key => $value) {
if (is_array($value) && !empty($value)) {
$results = array_merge($results, self::dot($value, $prepend . $key . '.'));
} else {
$results[$prepend . $key] = $value;
}
}
return $results;
}
public static function filesIn(string $path, string $fileType): array
{
return filter(
static fn (string $possibleModule): false | string => strstr($possibleModule, $fileType),
scandir($path)
);
}
public static function iterableToArray(iterable $iterable): array
{
if (is_array($iterable)) {
return $iterable;
}
return iterator_to_array($iterable);
}
}