1 | <?php |
||
21 | final class EntityArgumentFactory implements ArgumentFactory |
||
22 | { |
||
23 | |||
24 | private ObjectManager $objectManager; |
||
|
|||
25 | |||
26 | private ArgumentFactory $argumentFactory; |
||
27 | |||
28 | public function __construct( |
||
29 | ObjectManager $objectManager, |
||
30 | ArgumentFactory $argumentFactory |
||
31 | ) { |
||
32 | $this->objectManager = $objectManager; |
||
33 | $this->argumentFactory = $argumentFactory; |
||
34 | 15 | } |
|
35 | |||
36 | public function understandsString(string $source): bool |
||
37 | { |
||
38 | 15 | return 1 === preg_match('/^[a-zA-Z0-9_\\\\]+\\#.+/is', $source); |
|
39 | 15 | } |
|
40 | 15 | ||
41 | public function understandsArray(array $source): bool |
||
42 | 8 | { |
|
43 | return isset($source['entity-class']) && isset($source['entity-id']); |
||
44 | 8 | } |
|
45 | |||
46 | public function createArgumentFromString(string $source): Argument |
||
47 | 3 | { |
|
48 | Assert::true($this->understandsString($source)); |
||
49 | 3 | ||
50 | /** @var class-string $entityClass */ |
||
51 | [$entityClass, $idSource] = explode('#', $source); |
||
52 | 4 | ||
53 | /** @var Argument $id */ |
||
54 | 4 | $id = $this->argumentFactory->createArgumentFromString($idSource); |
|
55 | |||
56 | 1 | return new EntityArgument($this->objectManager, $entityClass, $id); |
|
57 | } |
||
58 | |||
59 | 1 | public function createArgumentFromArray(array $source): Argument |
|
60 | { |
||
61 | 1 | Assert::keyExists($source, 'entity-class'); |
|
62 | Assert::keyExists($source, 'entity-id'); |
||
63 | |||
64 | 4 | /** @var class-string $entityClass */ |
|
65 | $entityClass = $source['entity-class']; |
||
66 | 4 | ||
67 | 2 | /** @var Argument $id */ |
|
68 | $id = $this->argumentFactory->createArgumentFromString($source['entity-id']); |
||
69 | |||
70 | 1 | return new EntityArgument($this->objectManager, $entityClass, $id); |
|
71 | } |
||
72 | 1 | ||
73 | } |
||
74 |