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
|
|
|
$indent = str_repeat("\t", $indentLevel); |
17
|
|
|
$propertyIndent = str_repeat("\t", $indentLevel + 1); |
18
|
|
|
$arrayIndent = str_repeat("\t", $indentLevel + 2); |
19
|
|
|
|
20
|
|
|
if ($resourceIndent) { |
21
|
|
|
echo $indent; |
22
|
|
|
} |
23
|
|
|
echo get_class($resource), PHP_EOL; |
24
|
|
|
|
25
|
|
|
foreach (get_properties($resource) as $property) { |
26
|
|
|
echo $propertyIndent, $property->getName(), ': '; |
27
|
|
|
|
28
|
|
|
$propertyValue = get_property($resource, $property->getName())->getValue($resource); |
29
|
|
|
|
30
|
|
|
if ($propertyValue instanceof ResourceInterface) { |
31
|
|
|
resource_pretty_print($propertyValue, $indentLevel + 1); |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (is_array($propertyValue)) { |
36
|
|
|
echo '[', PHP_EOL; |
37
|
|
|
foreach ($propertyValue as $arrayKey => $arrayValue) { |
38
|
|
|
if (!($arrayValue instanceof ResourceInterface)) { |
39
|
|
|
echo $arrayIndent, $arrayKey, ': ', $arrayValue, PHP_EOL; |
40
|
|
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
resource_pretty_print($arrayValue, $indentLevel + 2, true); |
44
|
|
|
} |
45
|
|
|
echo $propertyIndent, ']', PHP_EOL; |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
echo $propertyValue, PHP_EOL; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ResourceInterface $resource |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
function get_properties(ResourceInterface $resource): array |
58
|
|
|
{ |
59
|
|
|
$class = new ReflectionClass($resource); |
60
|
|
|
return $class->getProperties(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ResourceInterface $resource |
65
|
|
|
* @param string $property |
66
|
|
|
* @return ReflectionProperty |
67
|
|
|
*/ |
68
|
|
|
function get_property(ResourceInterface $resource, string $property) |
69
|
|
|
{ |
70
|
|
|
$class = new ReflectionClass($resource); |
71
|
|
|
$prop = $class->getProperty($property); |
72
|
|
|
$prop->setAccessible(true); |
73
|
|
|
return $prop; |
74
|
|
|
} |
75
|
|
|
|