1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @param ResourceInterface $resource |
11
|
|
|
* @param int $indentLevel |
12
|
|
|
* @param bool $resourceIndent |
13
|
|
|
*/ |
14
|
|
|
function resource_pretty_print(ResourceInterface $resource, int $indentLevel = 0, bool $resourceIndent = false) |
15
|
|
|
{ |
16
|
1 |
|
$indent = str_repeat("\t", $indentLevel); |
17
|
1 |
|
$propertyIndent = str_repeat("\t", $indentLevel + 1); |
18
|
1 |
|
$arrayIndent = str_repeat("\t", $indentLevel + 2); |
19
|
|
|
|
20
|
1 |
|
if ($resourceIndent) { |
21
|
1 |
|
echo $indent; |
22
|
|
|
} |
23
|
1 |
|
echo get_class($resource), PHP_EOL; |
24
|
|
|
|
25
|
1 |
|
foreach (get_properties($resource) as $property) { |
26
|
1 |
|
echo $propertyIndent, $property->getName(), ': '; |
27
|
|
|
|
28
|
1 |
|
$propertyValue = get_property($resource, $property->getName())->getValue($resource); |
29
|
|
|
|
30
|
1 |
|
if ($propertyValue instanceof ResourceInterface) { |
31
|
1 |
|
resource_pretty_print($propertyValue, $indentLevel + 1); |
32
|
1 |
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
if (is_array($propertyValue)) { |
36
|
1 |
|
echo '[', PHP_EOL; |
37
|
1 |
|
foreach ($propertyValue as $arrayKey => $arrayValue) { |
38
|
1 |
|
if ($arrayValue instanceof ResourceInterface) { |
39
|
1 |
|
resource_pretty_print($arrayValue, $indentLevel + 2, true); |
40
|
1 |
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
echo $arrayIndent, $arrayKey, ': '; |
44
|
|
|
|
45
|
|
|
if (is_array($arrayValue)) { |
46
|
|
|
array_pretty_print($arrayValue, $arrayIndent + 2); |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
echo $arrayValue, PHP_EOL; |
51
|
|
|
} |
52
|
1 |
|
echo $propertyIndent, ']', PHP_EOL; |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
echo $propertyValue, PHP_EOL; |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $array |
62
|
|
|
* @param int $indentLevel |
63
|
|
|
*/ |
64
|
|
|
function array_pretty_print(array $array, int $indentLevel = 0) |
65
|
|
|
{ |
66
|
1 |
|
$indent = str_repeat("\t", $indentLevel); |
67
|
1 |
|
$propertyIndent = str_repeat("\t", $indentLevel + 1); |
68
|
1 |
|
echo '[', PHP_EOL; |
69
|
1 |
|
foreach ($array as $key => $value) { |
70
|
1 |
|
echo $propertyIndent, $key, ': '; |
71
|
1 |
|
if (is_array($value)) { |
72
|
1 |
|
array_pretty_print($value, $indentLevel + 1); |
73
|
1 |
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
echo $value, PHP_EOL; |
77
|
|
|
} |
78
|
1 |
|
echo $indent, ']', PHP_EOL; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ResourceInterface $resource |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
function get_properties(ResourceInterface $resource): array |
86
|
|
|
{ |
87
|
2 |
|
$class = new ReflectionClass($resource); |
88
|
2 |
|
return $class->getProperties(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ResourceInterface $resource |
93
|
|
|
* @param string $property |
94
|
|
|
* @return ReflectionProperty |
95
|
|
|
*/ |
96
|
|
|
function get_property(ResourceInterface $resource, string $property) |
97
|
|
|
{ |
98
|
2 |
|
$class = new ReflectionClass($resource); |
99
|
2 |
|
$prop = $class->getProperty($property); |
100
|
2 |
|
$prop->setAccessible(true); |
101
|
2 |
|
return $prop; |
102
|
|
|
} |
103
|
|
|
|