|
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 object|string $object |
|
17
|
|
|
*/ |
|
18
|
3 |
|
public static function getShortClassName($object): string |
|
19
|
|
|
{ |
|
20
|
3 |
|
$reflect = new ReflectionClass($object); |
|
21
|
|
|
|
|
22
|
3 |
|
return $reflect->getShortName(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Print a list of files if non empty |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function printFiles(string $title, array $files): void |
|
29
|
|
|
{ |
|
30
|
|
|
if (!$files) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
echo $title . PHP_EOL . PHP_EOL; |
|
35
|
|
|
|
|
36
|
|
|
foreach ($files as $file) { |
|
37
|
|
|
echo ' ' . escapeshellarg($file) . PHP_EOL; |
|
38
|
|
|
} |
|
39
|
|
|
echo PHP_EOL; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Replace EntityID model and don't touch other values |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $data mix of objects and scalar values |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public static function entityIdToModel(?array $data): ?array |
|
48
|
|
|
{ |
|
49
|
1 |
|
if ($data === null) { |
|
|
|
|
|
|
50
|
1 |
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
foreach ($data as &$value) { |
|
54
|
1 |
|
if ($value instanceof EntityID) { |
|
55
|
1 |
|
$value = $value->getEntity(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $data; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Replace object by their ID in the array and don't touch other values |
|
64
|
|
|
* |
|
65
|
|
|
* Support both AbstractModel and EntityID. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $data mix of objects and scalar values |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public static function modelToId(?array $data): ?array |
|
70
|
|
|
{ |
|
71
|
1 |
|
if ($data === null) { |
|
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
foreach ($data as &$value) { |
|
76
|
1 |
|
if ($value instanceof Model || $value instanceof EntityID) { |
|
77
|
1 |
|
$value = $value->getId(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public static function unique(array $array): array |
|
85
|
|
|
{ |
|
86
|
|
|
$result = []; |
|
87
|
|
|
foreach ($array as $value) { |
|
88
|
|
|
if (!in_array($value, $result, true)) { |
|
89
|
|
|
$result[] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|