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\ArgumentFactory; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory; |
16
|
|
|
use Addiks\SymfonyGenerics\Arguments\Argument; |
17
|
|
|
use Addiks\SymfonyGenerics\Arguments\EntityArgument; |
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
final class EntityArgumentFactory implements ArgumentFactory |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ObjectManager |
26
|
|
|
*/ |
27
|
|
|
private $objectManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ArgumentFactory |
31
|
|
|
*/ |
32
|
|
|
private $argumentFactory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ObjectManager $objectManager, |
36
|
|
|
ArgumentFactory $argumentFactory |
37
|
|
|
) { |
38
|
|
|
$this->objectManager = $objectManager; |
39
|
|
|
$this->argumentFactory = $argumentFactory; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function understandsString(string $source): bool |
43
|
|
|
{ |
44
|
|
|
return strpos($source, '#') > 0; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function understandsArray(array $source): bool |
48
|
|
|
{ |
49
|
|
|
return isset($source['entity-class']) && isset($source['entity-id']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function createArgumentFromString(string $source): Argument |
53
|
|
|
{ |
54
|
|
|
Assert::contains($source, '#'); |
55
|
|
|
|
56
|
|
|
[$entityClass, $idSource] = explode('#', $source); |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** @var Argument $id */ |
59
|
|
|
$id = $this->argumentFactory->createArgumentFromString($idSource); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return new EntityArgument($this->objectManager, $entityClass, $id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function createArgumentFromArray(array $source): Argument |
65
|
|
|
{ |
66
|
|
|
Assert::keyExists($source, 'entity-class'); |
67
|
|
|
Assert::keyExists($source, 'entity-id'); |
68
|
|
|
|
69
|
|
|
/** @var Argument $id */ |
70
|
|
|
$id = $this->argumentFactory->createArgumentFromString($source['entity-id']); |
71
|
|
|
|
72
|
|
|
return new EntityArgument($this->objectManager, $source['entity-class'], $id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
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.