1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\FluidSchema; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use function addslashes; |
8
|
|
|
use function array_map; |
9
|
|
|
use function implode; |
10
|
|
|
use function is_array; |
11
|
|
|
use function is_numeric; |
12
|
|
|
use function is_string; |
13
|
|
|
use function str_replace; |
14
|
|
|
use function var_export; |
15
|
|
|
|
16
|
|
|
class DoctrineAnnotationDumper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Write a string representing the parameter passed in. |
20
|
|
|
* @param mixed $item |
21
|
|
|
*/ |
22
|
|
|
public static function exportValues($item): string |
23
|
|
|
{ |
24
|
|
|
if ($item === null) { |
25
|
|
|
return ''; |
26
|
|
|
} |
27
|
|
|
if ($item === []) { |
28
|
|
|
return '({})'; |
29
|
|
|
} |
30
|
|
|
return '('.self::innerExportValues($item, true).')'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static function innerExportValues($item, bool $first): string |
34
|
|
|
{ |
35
|
|
|
if ($item === null) { |
36
|
|
|
return 'null'; |
37
|
|
|
} |
38
|
|
|
if (is_string($item)) { |
39
|
|
|
return '"'.str_replace('"', '""', $item).'"'; |
40
|
|
|
} |
41
|
|
|
if (is_numeric($item)) { |
42
|
|
|
return $item; |
43
|
|
|
} |
44
|
|
|
if (is_bool($item)) { |
45
|
|
|
return $item ? 'true' : 'false'; |
46
|
|
|
} |
47
|
|
|
if (is_array($item)) { |
48
|
|
|
if (self::isAssoc($item)) { |
49
|
|
|
if ($first) { |
50
|
|
|
array_walk($item, function(&$value, $key) { |
51
|
|
|
$value = $key.' = '.self::innerExportValues($value, false); |
52
|
|
|
}); |
53
|
|
|
} else { |
54
|
|
|
array_walk($item, function(&$value, $key) { |
55
|
|
|
$value = '"'.addslashes($key).'":'.self::innerExportValues($value, false); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
$result = implode(', ', $item); |
59
|
|
|
if (!$first) { |
60
|
|
|
$result = '{'.$result.'}'; |
61
|
|
|
} |
62
|
|
|
return $result; |
63
|
|
|
} else { |
64
|
|
|
array_walk($item, function(&$value, $key) { |
|
|
|
|
65
|
|
|
$value = self::innerExportValues($value, false); |
66
|
|
|
}); |
67
|
|
|
$result = implode(', ', $item); |
68
|
|
|
if (!$first) { |
69
|
|
|
$result = '{'.$result.'}'; |
70
|
|
|
} |
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
throw new \RuntimeException('Cannot serialize value in Doctrine annotation.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private static function isAssoc(array $arr): bool |
78
|
|
|
{ |
79
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.