1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ecodev\Felix; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Model\Model; |
8
|
|
|
use GraphQL\Doctrine\Definition\EntityID; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
final class Utility |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Returns the short class name of any object, eg: Application\Model\Calendar => Calendar. |
15
|
|
|
* |
16
|
|
|
* @param class-string|object $object |
17
|
|
|
*/ |
18
|
6 |
|
public static function getShortClassName(object|string $object): string |
19
|
|
|
{ |
20
|
6 |
|
$reflect = new ReflectionClass($object); |
21
|
|
|
|
22
|
6 |
|
return $reflect->getShortName(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Replace EntityID model and don't touch other values. |
27
|
|
|
* |
28
|
|
|
* @param ?array $data mix of objects and scalar values |
29
|
|
|
* |
30
|
|
|
* @return ($data is null ? null : array) |
31
|
|
|
*/ |
32
|
1 |
|
public static function entityIdToModel(?array $data): ?array |
33
|
|
|
{ |
34
|
1 |
|
if ($data === null) { |
|
|
|
|
35
|
1 |
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
foreach ($data as &$value) { |
39
|
1 |
|
if ($value instanceof EntityID) { |
40
|
1 |
|
$value = $value->getEntity(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $data; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Replace object by their ID in the array and don't touch other values. |
49
|
|
|
* |
50
|
|
|
* Support both AbstractModel and EntityID. |
51
|
|
|
* |
52
|
|
|
* @param ?array $data mix of objects and scalar values |
53
|
|
|
* |
54
|
|
|
* @return ($data is null ? null : array) |
55
|
|
|
*/ |
56
|
1 |
|
public static function modelToId(?array $data): ?array |
57
|
|
|
{ |
58
|
1 |
|
if ($data === null) { |
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
foreach ($data as &$value) { |
63
|
1 |
|
if ($value instanceof Model || $value instanceof EntityID) { |
64
|
1 |
|
$value = $value->getId(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Removes duplicate values from an array by using strict comparison. |
73
|
|
|
* |
74
|
|
|
* So it can be used with objects, whereas the native `array_unique` cannot. |
75
|
|
|
*/ |
76
|
1 |
|
public static function unique(array $array): array |
77
|
|
|
{ |
78
|
1 |
|
$result = []; |
79
|
1 |
|
foreach ($array as $value) { |
80
|
1 |
|
if (!in_array($value, $result, true)) { |
81
|
1 |
|
$result[] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Safely quotes an array of values for an SQL statement. |
90
|
|
|
* |
91
|
|
|
* The values are quoted and then returned as a comma-separated string, so: |
92
|
|
|
* |
93
|
|
|
* ``` |
94
|
|
|
* Utility::quoteArray(['foo bar', 2]); // "'foo bar', '2'" |
95
|
|
|
* ``` |
96
|
|
|
* |
97
|
|
|
* @param array<null|scalar> $values |
98
|
|
|
*/ |
99
|
9 |
|
public static function quoteArray(array $values): string |
100
|
|
|
{ |
101
|
9 |
|
$connection = _em()->getConnection(); |
102
|
9 |
|
$quoted = []; |
103
|
9 |
|
foreach ($values as $value) { |
104
|
8 |
|
$quoted[] = $connection->quote((string) $value); |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
return implode(', ', $quoted); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the domain to be used for cookie. |
112
|
|
|
* |
113
|
|
|
* We look for domain name to build the string ".mydomain.com" to specify |
114
|
|
|
* that cookies (session) are available on all subdomains. |
115
|
|
|
* |
116
|
|
|
* This will not work for domain without TLD such as "localhost", because |
117
|
|
|
* RFC specify the domain string must contain two "." characters. |
118
|
|
|
*/ |
119
|
6 |
|
public static function getCookieDomain(string $input): ?string |
120
|
|
|
{ |
121
|
6 |
|
if ($input && preg_match('/([^.]+\.[^.:]+)(:\d+)?$/', $input, $match)) { |
122
|
4 |
|
$cookieDomain = '.' . $match[1]; |
123
|
|
|
} else { |
124
|
2 |
|
$cookieDomain = null; |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
return $cookieDomain; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Concatenate all given iterables into a new iterator that yields key/value pairs exactly as if we iterated through each iterable sequentially. |
132
|
|
|
*/ |
133
|
1 |
|
public static function concat(iterable ...$iterables): iterable |
134
|
|
|
{ |
135
|
1 |
|
foreach ($iterables as $iterable) { |
136
|
1 |
|
foreach ($iterable as $k => $v) { |
137
|
1 |
|
yield $k => $v; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return a new iterable with only key/value pairs that match the given keys. |
144
|
|
|
*/ |
145
|
1 |
|
public static function filterByKeys(iterable $things, string ...$keyToKeep): iterable |
146
|
|
|
{ |
147
|
1 |
|
foreach ($things as $k => $v) { |
148
|
1 |
|
if (in_array($k, $keyToKeep, true)) { |
149
|
1 |
|
yield $k => $v; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|