1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
use function ucfirst; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This is used to pull out various methods from the doctrine meta data. It is based on the assumption that the methods |
14
|
|
|
* follow the ${methodType}${$propertyName} convention, with add methods using the singular version of the property |
15
|
|
|
*/ |
16
|
|
|
class RelationshipHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Use this to get the getter for the property, can be used with all relations |
20
|
|
|
* |
21
|
|
|
* @param array $mapping |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
8 |
|
public function getGetterFromDoctrineMapping(array $mapping): string |
26
|
|
|
{ |
27
|
8 |
|
$this->assertMappingLevel($mapping); |
28
|
8 |
|
$property = $this->getUppercaseProperty($mapping); |
29
|
|
|
|
30
|
8 |
|
return "get${property}"; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Use this to get the setter for the property, can be used with all relations |
35
|
|
|
* |
36
|
|
|
* @param array $mapping |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
8 |
|
public function getSetterFromDoctrineMapping(array $mapping): string |
41
|
|
|
{ |
42
|
8 |
|
$this->assertMappingLevel($mapping); |
43
|
8 |
|
$property = $this->getUppercaseProperty($mapping); |
44
|
|
|
|
45
|
8 |
|
return "set${property}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Use this to get the adder for the property, can only be used with *_TO_MANY relations |
50
|
|
|
* |
51
|
|
|
* @param array $mapping |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
6 |
|
public function getAdderFromDoctrineMapping(array $mapping): string |
56
|
|
|
{ |
57
|
6 |
|
$this->assertMappingLevel($mapping); |
58
|
6 |
|
$property = $this->getUppercaseProperty($mapping); |
59
|
6 |
|
if ($this->isPlural($mapping) === false) { |
60
|
2 |
|
throw new InvalidArgumentException("$property is singular, so doesn't have an add method"); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return 'add' . MappingHelper::singularize($property); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* User this to get the remover for the property, can only be used with *_TO_MANY relations |
68
|
|
|
* |
69
|
|
|
* @param array $mapping |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
6 |
|
public function getRemoverFromDoctrineMapping(array $mapping): string |
74
|
|
|
{ |
75
|
6 |
|
$this->assertMappingLevel($mapping); |
76
|
6 |
|
$property = $this->getUppercaseProperty($mapping); |
77
|
6 |
|
if ($this->isPlural($mapping) === false) { |
78
|
2 |
|
throw new InvalidArgumentException("$property is singular, so doesn't have an add method"); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
return 'remove' . MappingHelper::singularize($property); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Use this to check if the relationship is plural (*_TO_MANY) or singular (*_TO_ONE) |
86
|
|
|
* |
87
|
|
|
* @param array $mapping |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
22 |
|
public function isPlural(array $mapping): bool |
92
|
|
|
{ |
93
|
22 |
|
$this->assertMappingLevel($mapping); |
94
|
22 |
|
$type = $mapping['type']; |
95
|
22 |
|
switch ($type) { |
96
|
|
|
case ClassMetadataInfo::ONE_TO_ONE: |
97
|
|
|
case ClassMetadataInfo::MANY_TO_ONE: |
98
|
8 |
|
return false; |
99
|
|
|
case ClassMetadataInfo::ONE_TO_MANY: |
100
|
|
|
case ClassMetadataInfo::MANY_TO_MANY: |
101
|
12 |
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
throw new InvalidArgumentException("Unknown relationship of $type"); |
105
|
|
|
} |
106
|
|
|
|
107
|
28 |
|
public function getFieldName(array $mapping): string |
108
|
|
|
{ |
109
|
28 |
|
$this->assertMappingLevel($mapping); |
110
|
|
|
|
111
|
28 |
|
return $mapping['fieldName']; |
112
|
|
|
} |
113
|
|
|
|
114
|
28 |
|
private function getUppercaseProperty(array $mapping): string |
115
|
|
|
{ |
116
|
28 |
|
return ucfirst($this->getFieldName($mapping)); |
117
|
|
|
} |
118
|
|
|
|
119
|
38 |
|
private function assertMappingLevel(array $mapping): void |
120
|
|
|
{ |
121
|
38 |
|
if (!isset($mapping['type'])) { |
122
|
|
|
throw new InvalidArgumentException('Could not find the type key, are you using the correct mapping array'); |
123
|
|
|
} |
124
|
38 |
|
} |
125
|
|
|
} |
126
|
|
|
|