1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\model; |
5
|
|
|
|
6
|
|
|
use tkanstantsin\fileupload\config\InvalidConfigException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Container |
10
|
|
|
*/ |
11
|
|
|
class Container |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Configures an object with the initial property values. Inspired by Yii2. |
15
|
|
|
* @see \yii\BaseYii::configure |
16
|
|
|
* @param object $object the object to be configured |
17
|
|
|
* @param array $properties the property initial values given in terms of |
18
|
|
|
* name-value pairs. |
19
|
|
|
* @return object the object itself |
20
|
|
|
* @throws InvalidConfigException |
21
|
|
|
*/ |
22
|
3 |
|
public static function configure($object, $properties) |
23
|
|
|
{ |
24
|
3 |
|
foreach ($properties as $name => $value) { |
25
|
3 |
|
if (!property_exists($object, $name)) { |
26
|
1 |
|
throw new InvalidConfigException(sprintf('Property %s in class %s not found.', $name, \get_class($object))); |
27
|
|
|
} |
28
|
2 |
|
$object->$name = $value; |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
return $object; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a new object using the given configuration. Inspired by Yii2. |
36
|
|
|
* @see \yii\BaseYii::createObject |
37
|
|
|
* @param string|array $type |
38
|
|
|
* @return mixed |
39
|
|
|
* @throws InvalidConfigException |
40
|
|
|
* @throws \ReflectionException |
41
|
|
|
*/ |
42
|
1 |
|
public static function createObject($type) |
43
|
|
|
{ |
44
|
1 |
|
if (\is_string($type)) { |
45
|
|
|
return static::get($type); |
46
|
1 |
|
} elseif (\is_array($type)) { |
|
|
|
|
47
|
1 |
|
if (!isset($type['class'])) { |
48
|
|
|
throw new InvalidConfigException('Object configuration must be an array containing a "class" element.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$class = $type['class']; |
52
|
1 |
|
unset($type['class']); |
53
|
|
|
|
54
|
1 |
|
return static::get($class, $type); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new InvalidConfigException('Unsupported configuration type: ' . \gettype($type)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates an instance of the specified class. Inspired by Yii2 di. |
62
|
|
|
* @see \yii\di\Container::build |
63
|
|
|
* @param string $class the class name |
64
|
|
|
* @param array $config configurations to be applied to the new instance |
65
|
|
|
* @return mixed |
66
|
|
|
* @throws \ReflectionException |
67
|
|
|
* @throws InvalidConfigException |
68
|
|
|
*/ |
69
|
1 |
|
public static function get(string $class, array $config = []) |
70
|
|
|
{ |
71
|
1 |
|
$reflection = new \ReflectionClass($class); |
72
|
|
|
|
73
|
1 |
|
if (!$reflection->isInstantiable()) { |
74
|
|
|
throw new \ReflectionException(sprintf('`%s` is not instantiable.', $reflection->name)); |
75
|
|
|
} |
76
|
1 |
|
if ($reflection->implementsInterface(IConfigurable::class)) { |
77
|
|
|
// set $config as the last parameter (existing one will be overwritten) |
78
|
1 |
|
return $reflection->newInstanceArgs([$config]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$object = $reflection->newInstance(); |
82
|
|
|
static::configure($object, $config); |
83
|
|
|
|
84
|
|
|
return $object; |
85
|
|
|
} |
86
|
|
|
} |