1 | <?php |
||
19 | final class EntityArgument implements Argument |
||
20 | { |
||
21 | |||
22 | /** @var class-string */ |
||
23 | private string $entityClass; |
||
|
|||
24 | |||
25 | private Argument $id; |
||
26 | |||
27 | private ObjectManager $objectManager; |
||
28 | |||
29 | private ?ObjectRepository $repository; |
||
30 | |||
31 | /** @var array<string, mixed> */ |
||
32 | private static $constantMap = array( |
||
33 | 'true' => true, |
||
34 | 'false' => false, |
||
35 | 'null' => null, |
||
36 | ); |
||
37 | |||
38 | /** @param class-string $entityClass */ |
||
39 | public function __construct( |
||
40 | ObjectManager $objectManager, |
||
41 | string $entityClass, |
||
42 | Argument $id |
||
43 | ) { |
||
44 | $this->entityClass = $entityClass; |
||
45 | $this->id = $id; |
||
46 | $this->objectManager = $objectManager; |
||
47 | } |
||
48 | |||
49 | 3 | public function resolve() |
|
50 | { |
||
51 | /** @var string $entityId */ |
||
52 | $entityId = $this->id->resolve(); |
||
53 | |||
54 | 3 | if (preg_match("/^\[([a-zA-Z0-9_-]+)\=(.*)\]$/is", $entityId, $matches)) { |
|
55 | 3 | [, $column, $value] = $matches; |
|
56 | 3 | ||
57 | 3 | if (isset(self::$constantMap[strtolower($value)])) { |
|
58 | $value = self::$constantMap[strtolower($value)]; |
||
59 | 1 | } |
|
60 | |||
61 | return $this->repository()->findOneBy([$column => $value]); |
||
62 | 1 | ||
63 | } else { |
||
64 | 1 | return $this->objectManager->find( |
|
65 | $this->entityClass, |
||
66 | $entityId |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | private function repository(): ObjectRepository |
||
72 | { |
||
73 | if (is_null($this->repository)) { |
||
74 | 1 | $this->repository = $this->objectManager->getRepository($this->entityClass); |
|
75 | 1 | } |
|
76 | 1 | ||
77 | return $this->repository; |
||
78 | } |
||
79 | |||
80 | } |
||
81 |