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