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
|
|
|
*/ |
12
|
|
|
function resource_pretty_print(ResourceInterface $resource) |
13
|
|
|
{ |
14
|
|
|
$printResource = clone $resource; |
15
|
|
|
|
16
|
|
|
call_method($printResource, 'unsetTransport'); |
17
|
|
|
|
18
|
|
|
var_export($printResource); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param ResourceInterface $resource |
23
|
|
|
* @param string $method |
24
|
|
|
* @return ResourceInterface |
25
|
|
|
*/ |
26
|
|
|
function call_method(ResourceInterface $resource, string $method): ResourceInterface |
27
|
|
|
{ |
28
|
|
|
if (method_exists($resource, $method)) { |
29
|
|
|
$resource->$method(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$properties = get_properties($resource); |
33
|
|
|
call_method_properties($resource, $properties, $method); |
34
|
|
|
|
35
|
|
|
return $resource; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ResourceInterface $resource |
40
|
|
|
* @param array $properties |
41
|
|
|
* @param string $method |
42
|
|
|
*/ |
43
|
|
|
function call_method_properties(ResourceInterface $resource, array $properties, string $method) |
44
|
|
|
{ |
45
|
|
|
foreach ($properties as $property) { |
46
|
|
|
call_method_property($resource, $property->getName(), $method); |
47
|
|
|
call_method_array_property($resource, $property->getName(), $method); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ResourceInterface $resource |
53
|
|
|
* @param string $propertyName |
54
|
|
|
* @param string $method |
55
|
|
|
*/ |
56
|
|
|
function call_method_property(ResourceInterface $resource, string $propertyName, string $method) |
57
|
|
|
{ |
58
|
|
|
$property = get_property($resource, $propertyName); |
59
|
|
|
if (!($property->getValue($resource) instanceof ResourceInterface)) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$property->setValue( |
64
|
|
|
$resource, |
65
|
|
|
call_method( |
66
|
|
|
$property->getValue($resource), |
67
|
|
|
$method |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ResourceInterface $resource |
74
|
|
|
* @param string $propertyName |
75
|
|
|
* @param string $method |
76
|
|
|
*/ |
77
|
|
|
function call_method_array_property(ResourceInterface $resource, string $propertyName, string $method) |
78
|
|
|
{ |
79
|
|
|
$property = get_property($resource, $propertyName); |
80
|
|
|
if (!is_array($property->getValue($resource))) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
$newCollection = []; |
84
|
|
|
|
85
|
|
|
foreach ($property->getValue($resource) as $key => $value) { |
86
|
|
|
if (!($value instanceof ResourceInterface)) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$newCollection[$key] = call_method($value, $method); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$property->setValue($resource, $newCollection); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ResourceInterface $resource |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
function get_properties(ResourceInterface $resource): array |
101
|
|
|
{ |
102
|
1 |
|
$class = new ReflectionClass($resource); |
103
|
1 |
|
return $class->getProperties(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ResourceInterface $resource |
108
|
|
|
* @param string $property |
109
|
|
|
* @return ReflectionProperty |
110
|
|
|
*/ |
111
|
|
|
function get_property(ResourceInterface $resource, string $property) |
112
|
|
|
{ |
113
|
1 |
|
$class = new ReflectionClass($resource); |
114
|
1 |
|
$prop = $class->getProperty($property); |
115
|
1 |
|
$prop->setAccessible(true); |
116
|
1 |
|
return $prop; |
117
|
|
|
} |
118
|
|
|
|