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