|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\model; |
|
5
|
|
|
|
|
6
|
|
|
use tkanstantsin\fileupload\config\InvalidConfigException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class BaseObject |
|
10
|
|
|
*/ |
|
11
|
|
|
class BaseObject implements IConfigurable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Util constructor. |
|
15
|
|
|
* @param array $config |
|
16
|
|
|
* @throws InvalidConfigException |
|
17
|
|
|
*/ |
|
18
|
3 |
|
public function __construct(array $config = []) |
|
19
|
|
|
{ |
|
20
|
3 |
|
Container::configure($this, $config); |
|
21
|
2 |
|
$this->init(); |
|
22
|
2 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initialize object |
|
26
|
|
|
* E.g. instantiate some variables or check correct data |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function init(): void |
|
29
|
|
|
{ |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the value of an object property. |
|
34
|
|
|
* |
|
35
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
36
|
|
|
* will be implicitly called when executing `$value = $object->property;`. |
|
37
|
|
|
* @param string $name the property name |
|
38
|
|
|
* @return mixed the property value |
|
39
|
|
|
* @throws \RuntimeException |
|
40
|
|
|
* @see __set() |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __get($name) |
|
43
|
|
|
{ |
|
44
|
|
|
$getter = 'get' . $name; |
|
45
|
|
|
if (method_exists($this, $getter)) { |
|
46
|
|
|
return $this->$getter(); |
|
47
|
|
|
} elseif (method_exists($this, 'set' . $name)) { |
|
48
|
|
|
throw new \RuntimeException('Getting write-only property: ' . \get_class($this) . '::' . $name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new \RuntimeException('Getting unknown property: ' . \get_class($this) . '::' . $name); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets value of an object property. |
|
56
|
|
|
* |
|
57
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
58
|
|
|
* will be implicitly called when executing `$object->property = $value;`. |
|
59
|
|
|
* @param string $name the property name or the event name |
|
60
|
|
|
* @param mixed $value the property value |
|
61
|
|
|
* @throws \RuntimeException |
|
62
|
|
|
* @see __get() |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function __set($name, $value) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$setter = 'set' . $name; |
|
67
|
1 |
|
if (method_exists($this, $setter)) { |
|
68
|
1 |
|
$this->$setter($value); |
|
69
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
|
70
|
|
|
throw new \RuntimeException('Setting read-only property: ' . \get_class($this) . '::' . $name); |
|
71
|
|
|
} else { |
|
72
|
|
|
throw new \RuntimeException('Setting unknown property: ' . \get_class($this) . '::' . $name); |
|
73
|
|
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Checks if a property is set, i.e. defined and not null. |
|
78
|
|
|
* |
|
79
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
80
|
|
|
* will be implicitly called when executing `isset($object->property)`. |
|
81
|
|
|
* |
|
82
|
|
|
* Note that if the property is not defined, false will be returned. |
|
83
|
|
|
* @param string $name the property name or the event name |
|
84
|
|
|
* @return bool whether the named property is set (not null). |
|
85
|
|
|
* @see http://php.net/manual/en/function.isset.php |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __isset($name): bool |
|
88
|
|
|
{ |
|
89
|
|
|
$getter = 'get' . $name; |
|
90
|
|
|
if (method_exists($this, $getter)) { |
|
91
|
|
|
return $this->$getter() !== null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Sets an object property to null. |
|
99
|
|
|
* |
|
100
|
|
|
* Do not call this method directly as it is a PHP magic method that |
|
101
|
|
|
* will be implicitly called when executing `unset($object->property)`. |
|
102
|
|
|
* |
|
103
|
|
|
* Note that if the property is not defined, this method will do nothing. |
|
104
|
|
|
* If the property is read-only, it will throw an exception. |
|
105
|
|
|
* @param string $name the property name |
|
106
|
|
|
* @throws \RuntimeException |
|
107
|
|
|
* @see http://php.net/manual/en/function.unset.php |
|
108
|
|
|
*/ |
|
109
|
|
|
public function __unset($name) |
|
110
|
|
|
{ |
|
111
|
|
|
$setter = 'set' . $name; |
|
112
|
|
|
if (method_exists($this, $setter)) { |
|
113
|
|
|
$this->$setter(null); |
|
114
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
|
115
|
|
|
throw new \RuntimeException('Unsetting read-only property: ' . \get_class($this) . '::' . $name); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |