1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Arguments; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
17
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
18
|
|
|
|
19
|
|
|
final class EntityArgument implements Argument |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $entityClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Argument |
29
|
|
|
*/ |
30
|
|
|
private $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ObjectManager |
34
|
|
|
*/ |
35
|
|
|
private $objectManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ObjectRepository|null |
39
|
|
|
*/ |
40
|
|
|
private $repository; |
41
|
|
|
|
42
|
|
|
/** @var array<string, mixed> */ |
43
|
|
|
private static $constantMap = array( |
44
|
|
|
'true' => true, |
45
|
|
|
'false' => false, |
46
|
|
|
'null' => null, |
47
|
|
|
); |
48
|
|
|
|
49
|
3 |
|
public function __construct( |
50
|
|
|
ObjectManager $objectManager, |
51
|
|
|
string $entityClass, |
52
|
|
|
Argument $id |
53
|
|
|
) { |
54
|
3 |
|
$this->entityClass = $entityClass; |
55
|
3 |
|
$this->id = $id; |
56
|
3 |
|
$this->objectManager = $objectManager; |
57
|
3 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function resolve() |
60
|
|
|
{ |
61
|
|
|
/** @var string $entityId */ |
62
|
1 |
|
$entityId = $this->id->resolve(); |
63
|
|
|
|
64
|
1 |
|
if (preg_match("/^\[([a-zA-Z0-9_-]+)\=(.*)\]$/is", $entityId, $matches)) { |
65
|
|
|
[, $column, $value] = $matches; |
|
|
|
|
66
|
|
|
|
67
|
|
|
if (isset(self::$constantMap[strtolower($value)])) { |
|
|
|
|
68
|
|
|
$value = self::$constantMap[strtolower($value)]; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->repository()->findOneBy([$column => $value]); |
|
|
|
|
72
|
|
|
|
73
|
|
|
} else { |
74
|
1 |
|
return $this->objectManager->find( |
75
|
1 |
|
$this->entityClass, |
76
|
1 |
|
$entityId |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function repository(): ObjectRepository |
82
|
|
|
{ |
83
|
|
|
if (is_null($this->repository)) { |
84
|
|
|
$this->repository = $this->objectManager->getRepository($this->entityClass); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->repository; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.