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