1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/skeleton/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/skeleton |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Flipbox\Skeleton\Object; |
10
|
|
|
|
11
|
|
|
use Flipbox\Skeleton\Exceptions\InvalidCallException; |
12
|
|
|
use Flipbox\Skeleton\Exceptions\UnknownMethodException; |
13
|
|
|
use Flipbox\Skeleton\Exceptions\UnknownPropertyException; |
14
|
|
|
use Flipbox\Skeleton\Helpers\ArrayHelper; |
15
|
|
|
use Flipbox\Skeleton\Helpers\ObjectHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractObject implements ObjectInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param array $config |
27
|
|
|
*/ |
28
|
|
|
public function __construct($config = []) |
29
|
|
|
{ |
30
|
|
|
if (!empty($config)) { |
31
|
|
|
ObjectHelper::configure($this, $config); |
32
|
|
|
} |
33
|
|
|
$this->init(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public static function create($config = []): ObjectInterface |
40
|
|
|
{ |
41
|
|
|
// Force array |
42
|
|
|
if (!is_array($config)) { |
43
|
|
|
$config = ArrayHelper::toArray($config, [], false); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Add class to config |
47
|
|
|
$config['class'] = static::class; |
48
|
|
|
|
49
|
|
|
return ObjectHelper::create($config); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function hasProperty($name, $checkVars = true): bool |
63
|
|
|
{ |
64
|
|
|
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function canGetProperty($name, $checkVars = true): bool |
71
|
|
|
{ |
72
|
|
|
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function canSetProperty($name, $checkVars = true): bool |
79
|
|
|
{ |
80
|
|
|
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function hasMethod($name): bool |
87
|
|
|
{ |
88
|
|
|
return method_exists($this, $name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the value of an object property. |
93
|
|
|
* |
94
|
|
|
* @param $name |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws InvalidCallException |
97
|
|
|
* @throws UnknownPropertyException |
98
|
|
|
*/ |
99
|
|
|
public function __get($name) |
100
|
|
|
{ |
101
|
|
|
$getter = 'get' . $name; |
102
|
|
|
if (method_exists($this, $getter)) { |
103
|
|
|
return $this->$getter(); |
104
|
|
|
} elseif (method_exists($this, 'set' . $name)) { |
105
|
|
|
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); |
106
|
|
|
} else { |
107
|
|
|
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sets value of an object property. |
113
|
|
|
* |
114
|
|
|
* @param $name |
115
|
|
|
* @param $value |
116
|
|
|
* @throws InvalidCallException |
117
|
|
|
* @throws UnknownPropertyException |
118
|
|
|
*/ |
119
|
|
|
public function __set($name, $value) |
120
|
|
|
{ |
121
|
|
|
$setter = 'set' . $name; |
122
|
|
|
if (method_exists($this, $setter)) { |
123
|
|
|
$this->$setter($value); |
124
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
125
|
|
|
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); |
126
|
|
|
} else { |
127
|
|
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Checks if a property is set, i.e. defined and not null. |
133
|
|
|
* |
134
|
|
|
* @param $name |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function __isset($name) |
138
|
|
|
{ |
139
|
|
|
$getter = 'get' . $name; |
140
|
|
|
if (method_exists($this, $getter)) { |
141
|
|
|
return $this->$getter() !== null; |
142
|
|
|
} else { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Sets an object property to null. |
149
|
|
|
* |
150
|
|
|
* @param $name |
151
|
|
|
* @throws InvalidCallException |
152
|
|
|
*/ |
153
|
|
|
public function __unset($name) |
154
|
|
|
{ |
155
|
|
|
$setter = 'set' . $name; |
156
|
|
|
if (method_exists($this, $setter)) { |
157
|
|
|
$this->$setter(null); |
158
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
159
|
|
|
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Calls the named method which is not a class method. |
165
|
|
|
* |
166
|
|
|
* @param $name |
167
|
|
|
* @param $params |
168
|
|
|
* @throws UnknownMethodException |
169
|
|
|
*/ |
170
|
|
|
public function __call($name, $params) |
171
|
|
|
{ |
172
|
|
|
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()"); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|