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
|
|
|
abstract 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
|
|
|
* ```php |
94
|
|
|
* Utility::quoteArray(['foo bar', 2]); // "'foo bar', '2'" |
95
|
|
|
* ``` |
96
|
|
|
*/ |
97
|
9 |
|
public static function quoteArray(array $value): string |
98
|
|
|
{ |
99
|
9 |
|
$connection = _em()->getConnection(); |
100
|
9 |
|
$quoted = []; |
101
|
9 |
|
foreach ($value as $v) { |
102
|
8 |
|
$quoted[] = $connection->quote($v); |
103
|
|
|
} |
104
|
|
|
|
105
|
9 |
|
return implode(', ', $quoted); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the domain to be used for cookie. |
110
|
|
|
* |
111
|
|
|
* We look for domain name to build the string ".mydomain.com" to specify |
112
|
|
|
* that cookies (session) are available on all subdomains. |
113
|
|
|
* |
114
|
|
|
* This will not work for domain without TLD such as "localhost", because |
115
|
|
|
* RFC specify the domain string must contain two "." characters. |
116
|
|
|
*/ |
117
|
6 |
|
public static function getCookieDomain(string $input): ?string |
118
|
|
|
{ |
119
|
6 |
|
if ($input && preg_match('/([^.]+\.[^.:]+)(:\d+)?$/', $input, $match)) { |
120
|
4 |
|
$cookieDomain = '.' . $match[1]; |
121
|
|
|
} else { |
122
|
2 |
|
$cookieDomain = null; |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
return $cookieDomain; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|