|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\Manager; |
|
14
|
|
|
|
|
15
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Exception\UnsupportedObjectClassException; |
|
17
|
|
|
use function Sauls\Component\Helper\define_object; |
|
18
|
|
|
|
|
19
|
|
|
class ObjectManager implements ManagerInterface, NamedManagerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
public const DEFAULT_OBJECT_MANAGER_NAME = 'default.object_manager'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $objectClass; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var EventDispatcherInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $eventDispatcher; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $properties |
|
42
|
|
|
* @return object |
|
43
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create(array $properties = []): object |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->modify(new $this->objectClass, $properties); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param object $object |
|
52
|
|
|
* @param array $properties |
|
53
|
|
|
* @return object |
|
54
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function modify(object $object, array $properties): object |
|
57
|
|
|
{ |
|
58
|
|
|
$this->checkObjectIntegrity($object); |
|
59
|
|
|
|
|
60
|
|
|
return define_object($object, $properties); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function checkObjectIntegrity(object $object): void |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->notSupported($object)) { |
|
66
|
|
|
throw new UnsupportedObjectClassException( |
|
67
|
|
|
sprintf( |
|
68
|
|
|
'Object manager of `%s` object class does not support given `%s` class', |
|
69
|
|
|
$this->objectClass, |
|
70
|
|
|
\get_class($object) |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function classIsSupported(string $class): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return false === empty($this->objectClass) && \is_a($class, $this->objectClass, true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function objectIsSupported(object $object): bool |
|
82
|
|
|
{ |
|
83
|
|
|
return $object instanceof $this->objectClass; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function notSupported($value): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return false === $this->supports($value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param mixed|object|string $value |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function supports($value): bool |
|
96
|
|
|
{ |
|
97
|
|
|
if (\is_string($value)) { |
|
98
|
|
|
return $this->classIsSupported($value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (\is_object($value)) { |
|
102
|
|
|
return $this->objectIsSupported($value); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getName(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return self::DEFAULT_OBJECT_MANAGER_NAME; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function setObjectClass(string $class): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->objectClass = $class; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|