1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageBase; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
5 |
|
abstract class AbstractStructBase implements StructInterface, JsonSerializable |
12
|
|
|
{ |
13
|
5 |
|
/** |
14
|
|
|
* Returns the properties of this object |
15
|
|
|
* @return mixed[] |
16
|
|
|
*/ |
17
|
|
|
public function jsonSerialize(): array |
18
|
|
|
{ |
19
|
|
|
return \get_object_vars($this); |
20
|
|
|
} |
21
|
|
|
|
22
|
15 |
|
/** |
23
|
|
|
* Generic method called when an object has been exported with var_export() functions |
24
|
15 |
|
* It allows to return an object instantiated with the values |
25
|
15 |
|
* @param array $array the exported values |
26
|
15 |
|
* @return self |
27
|
15 |
|
*/ |
28
|
6 |
|
public static function __set_state(array $array): StructInterface |
29
|
10 |
|
{ |
30
|
|
|
$reflection = new ReflectionClass(get_called_class()); |
31
|
|
|
$object = $reflection->newInstance(); |
32
|
|
|
foreach ($array as $name => $value) { |
33
|
|
|
$object->setPropertyValue($name, $value); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $object; |
37
|
|
|
} |
38
|
65 |
|
|
39
|
|
|
/** |
40
|
65 |
|
* Generic method setting value |
41
|
65 |
|
* @throws InvalidArgumentException |
42
|
65 |
|
* @param string $name property name to set |
43
|
26 |
|
* @param mixed $value property value to use |
44
|
5 |
|
* @return self |
45
|
|
|
* @internal |
46
|
65 |
|
*/ |
47
|
|
|
public function setPropertyValue(string $name, $value): self |
48
|
|
|
{ |
49
|
|
|
$setMethod = 'set' . ucfirst($name); |
50
|
|
|
if (method_exists($this, $setMethod)) { |
51
|
|
|
$this->$setMethod($value); |
52
|
|
|
} else { |
53
|
|
|
throw new InvalidArgumentException(sprintf('Setter does not exist for "%s" property', $name)); |
54
|
65 |
|
} |
55
|
|
|
|
56
|
65 |
|
return $this; |
57
|
65 |
|
} |
58
|
60 |
|
|
59
|
|
|
/** |
60
|
5 |
|
* Generic method getting value |
61
|
|
|
* @throws InvalidArgumentException |
62
|
|
|
* @param string $name property name to get |
63
|
|
|
* @return mixed |
64
|
|
|
* @internal |
65
|
|
|
*/ |
66
|
10 |
|
public function getPropertyValue(string $name) |
67
|
|
|
{ |
68
|
10 |
|
$getMethod = 'get' . ucfirst($name); |
69
|
|
|
if (method_exists($this, $getMethod)) { |
70
|
|
|
return $this->$getMethod(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new InvalidArgumentException(sprintf('Getter does not exist for "%s" property', $name)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Default string representation of current object. Don't want to expose any sensible data |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function __toString(): string |
81
|
|
|
{ |
82
|
|
|
return get_called_class(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|