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\Helpers; |
10
|
|
|
|
11
|
|
|
use Flipbox\Skeleton\Exceptions\InvalidConfigurationException; |
12
|
|
|
use Flipbox\Skeleton\Object\ObjectInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Flipbox Factory <[email protected]> |
16
|
|
|
* @since 2.0.0 |
17
|
|
|
*/ |
18
|
|
|
class ObjectHelper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Returns the public member variables of an object. |
22
|
|
|
* |
23
|
|
|
* @param ObjectInterface $object |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public static function getObjectVars(ObjectInterface $object) |
27
|
|
|
{ |
28
|
|
|
return get_object_vars($object); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Configures an object with the initial property values. |
33
|
|
|
* |
34
|
|
|
* @param ObjectInterface $object |
35
|
|
|
* @param $properties |
36
|
|
|
* @return ObjectInterface |
37
|
|
|
*/ |
38
|
|
|
public static function configure(ObjectInterface $object, $properties) |
39
|
|
|
{ |
40
|
|
|
// Populate model attributes |
41
|
|
|
if (!empty($properties)) { |
42
|
|
|
// To array |
43
|
|
|
if (!is_array($properties)) { |
44
|
|
|
$properties = ArrayHelper::toArray($properties, [], false); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($properties as $name => $value) { |
48
|
|
|
if ($object->canSetProperty($name)) { |
49
|
|
|
$object->$name = $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $object; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new object |
58
|
|
|
* |
59
|
|
|
* @param $config |
60
|
|
|
* @param null $instanceOf |
61
|
|
|
* @return ObjectInterface |
62
|
|
|
* @throws InvalidConfigurationException |
63
|
|
|
*/ |
64
|
|
|
public static function create($config, $instanceOf = null) |
65
|
|
|
{ |
66
|
|
|
// Get class from config |
67
|
|
|
$class = static::checkConfig($config, $instanceOf); |
68
|
|
|
|
69
|
|
|
// New object |
70
|
|
|
return new $class($config); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks the config for a valid class |
75
|
|
|
* |
76
|
|
|
* @param $config |
77
|
|
|
* @param null $instanceOf |
78
|
|
|
* @param bool $removeClass |
79
|
|
|
* @return null|string |
80
|
|
|
* @throws InvalidConfigurationException |
81
|
|
|
*/ |
82
|
|
|
public static function checkConfig(&$config, $instanceOf = null, $removeClass = true) |
83
|
|
|
{ |
84
|
|
|
// Get class from config |
85
|
|
|
$class = static::getClassFromConfig($config, $removeClass); |
86
|
|
|
|
87
|
|
|
// Make sure we have a valid class |
88
|
|
|
if ($instanceOf && !is_subclass_of($class, $instanceOf)) { |
|
|
|
|
89
|
|
|
throw new InvalidConfigurationException( |
90
|
|
|
sprintf( |
91
|
|
|
"The class '%s' must be an instance of '%s'", |
92
|
|
|
(string)$class, |
93
|
|
|
(string)$instanceOf |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $class; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a class from a config |
103
|
|
|
* |
104
|
|
|
* @param $config |
105
|
|
|
* @param bool $removeClass |
106
|
|
|
* @return string |
107
|
|
|
* @throws InvalidConfigurationException |
108
|
|
|
*/ |
109
|
|
|
public static function getClassFromConfig(&$config, $removeClass = false) |
110
|
|
|
{ |
111
|
|
|
// Find class |
112
|
|
|
$class = static::findClassFromConfig($config, $removeClass); |
113
|
|
|
|
114
|
|
|
if (empty($class)) { |
115
|
|
|
throw new InvalidConfigurationException( |
116
|
|
|
sprintf( |
117
|
|
|
"The configuration must specify a 'class' property: '%s'", |
118
|
|
|
JsonHelper::encode($config) |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $class; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Find a class from a config |
128
|
|
|
* |
129
|
|
|
* @param $config |
130
|
|
|
* @param bool $removeClass |
131
|
|
|
* @return null|string |
132
|
|
|
*/ |
133
|
|
|
public static function findClassFromConfig(&$config, $removeClass = false) |
134
|
|
|
{ |
135
|
|
|
// Normalize the config |
136
|
|
|
if (is_string($config)) { |
137
|
|
|
$class = $config; |
138
|
|
|
$config = ''; |
139
|
|
|
} elseif (is_object($config)) { |
140
|
|
|
return get_class($config); |
141
|
|
|
} else { |
142
|
|
|
// Force Array |
143
|
|
|
if (!is_array($config)) { |
144
|
|
|
$config = ArrayHelper::toArray($config, [], false); |
145
|
|
|
} |
146
|
|
|
if ($removeClass) { |
147
|
|
|
if (!$class = ArrayHelper::remove($config, 'class')) { |
148
|
|
|
$class = ArrayHelper::remove($config, 'type'); |
149
|
|
|
} |
150
|
|
|
} else { |
151
|
|
|
$class = ArrayHelper::getValue( |
152
|
|
|
$config, |
153
|
|
|
'class', |
154
|
|
|
ArrayHelper::getValue($config, 'type') |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
return $class; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|