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
98 lines (78 loc) · 2.46 KB
/
Utils.php
File metadata and controls
98 lines (78 loc) · 2.46 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
<?php
declare(strict_types=1);
namespace CodelyTv\Shared\Domain;
use DateTimeImmutable;
use DateTimeInterface;
use ReflectionClass;
use RuntimeException;
use function Lambdish\Phunctional\filter;
final class Utils
{
public static function endsWith(string $needle, string $haystack): bool
{
$length = strlen($needle);
if ($length === 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
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);
}
public static function jsonDecode(string $json): array
{
$data = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error());
}
return $data;
}
public static function toSnakeCase(string $text): string
{
return ctype_lower($text) ? $text : strtolower(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, static::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) => strstr($possibleModule, $fileType),
scandir($path)
);
}
public static function extractClassName(object $object): string
{
$reflect = new ReflectionClass($object);
return $reflect->getShortName();
}
public static function iterableToArray(iterable $iterable): array
{
if (is_array($iterable)) {
return $iterable;
}
return iterator_to_array($iterable);
}
}