|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\IdFieldTrait; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\IntegerIdFieldTrait; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\NonBinaryUuidFieldTrait; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\UuidFieldTrait; |
|
10
|
|
|
|
|
11
|
|
|
class ReplaceEntityIdFieldProcess implements ProcessInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public const FIND_USE_STATEMENT = 'use DSM\Fields\Traits\PrimaryKey\IdFieldTrait;'; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $idTraitFqn; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Specify the IdFieldTrait fully qualified name, eg UuidFieldTrait::class |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $idTraitFqn |
|
24
|
|
|
* |
|
25
|
|
|
* @return ReplaceEntityIdFieldProcess |
|
26
|
|
|
*/ |
|
27
|
|
|
public function setIdTraitFqn(string $idTraitFqn): self |
|
28
|
|
|
{ |
|
29
|
|
|
$this->idTraitFqn = $idTraitFqn; |
|
30
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function run(File\FindReplace $findReplace): void |
|
35
|
|
|
{ |
|
36
|
|
|
if (null === $this->idTraitFqn) { |
|
37
|
|
|
throw new \RuntimeException('you must set the IdTraitFqn'); |
|
38
|
|
|
} |
|
39
|
|
|
$findReplace->findReplace(self::FIND_USE_STATEMENT, $this->getUseStatement()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the use statement to replace it with |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
private function getUseStatement(): string |
|
48
|
|
|
{ |
|
49
|
|
|
switch ($this->idTraitFqn) { |
|
50
|
|
|
case IdFieldTrait::class: |
|
51
|
|
|
$useStatement = 'use DSM\Fields\Traits\PrimaryKey\IdFieldTrait;'; |
|
52
|
|
|
break; |
|
53
|
|
|
case IntegerIdFieldTrait::class: |
|
54
|
|
|
$useStatement = 'use DSM\Fields\Traits\PrimaryKey\IntegerIdFieldTrait;'; |
|
55
|
|
|
break; |
|
56
|
|
|
case NonBinaryUuidFieldTrait::class: |
|
57
|
|
|
$useStatement = 'use DSM\Fields\Traits\PrimaryKey\NonBinaryUuidFieldTrait;'; |
|
58
|
|
|
break; |
|
59
|
|
|
case UuidFieldTrait::class: |
|
60
|
|
|
$useStatement = 'use DSM\Fields\Traits\PrimaryKey\UuidFieldTrait;'; |
|
61
|
|
|
break; |
|
62
|
|
|
default: |
|
63
|
|
|
throw new \LogicException('Unknown trait selected ' . $this->idTraitFqn); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $useStatement; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|