1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
8
|
|
|
use Psi\Component\ContentType\FieldLoader; |
9
|
|
|
use Psi\Component\ContentType\LoadedField; |
10
|
|
|
use Psi\Component\ContentType\Standard\Storage\CollectionType; |
11
|
|
|
use Psi\Component\ContentType\Standard\Storage\DateTimeType; |
12
|
|
|
use Psi\Component\ContentType\Standard\Storage\IntegerType; |
13
|
|
|
use Psi\Component\ContentType\Standard\Storage\ObjectType; |
14
|
|
|
use Psi\Component\ContentType\Standard\Storage\ReferenceType; |
15
|
|
|
use Psi\Component\ContentType\Standard\Storage\StringType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The FieldMapper maps the correct PHPCR-ODM field for the given content-type |
19
|
|
|
* field. |
20
|
|
|
*/ |
21
|
|
|
class FieldMapper |
22
|
|
|
{ |
23
|
|
|
private $encoder; |
24
|
|
|
private $fieldLoader; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
PropertyEncoder $encoder, |
28
|
|
|
FieldLoader $fieldLoader |
29
|
|
|
) { |
30
|
|
|
$this->encoder = $encoder; |
31
|
|
|
$this->fieldLoader = $fieldLoader; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __invoke($fieldName, LoadedField $loadedField, ClassMetadata $metadata, array $extraOptions = []) |
35
|
|
|
{ |
36
|
|
|
$configuredType = $loadedField->getStorageType(); |
37
|
|
|
$type = $configuredType->getInnerType(); |
38
|
|
|
$options = array_merge([ |
39
|
|
|
'multivalue' => false, |
40
|
|
|
], $extraOptions); |
41
|
|
|
|
42
|
|
|
if ($type instanceof ObjectType) { |
43
|
|
|
$options = $loadedField->getStorageType()->getOptions(); |
44
|
|
|
$this->unrestrictChildClass($options['class'], $metadata); |
45
|
|
|
$metadata->mapChild([ |
46
|
|
|
'fieldName' => $fieldName, |
47
|
|
|
'nodeName' => $this->encoder->encode($fieldName), |
48
|
|
|
'nullable' => true, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($type instanceof CollectionType) { |
55
|
|
|
$this->mapCollectionType($fieldName, $loadedField, $metadata); |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
if ($type instanceof StringType) { |
|
|
|
|
61
|
|
|
$metadata->mapField([ |
62
|
|
|
'fieldName' => $fieldName, |
63
|
|
|
'type' => 'string', |
64
|
|
|
'nullable' => true, |
65
|
|
|
'multivalue' => $options['multivalue'], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($type instanceof IntegerType) { |
|
|
|
|
72
|
|
|
$metadata->mapField([ |
73
|
|
|
'fieldName' => $fieldName, |
74
|
|
|
'type' => 'long', |
75
|
|
|
'nullable' => true, |
76
|
|
|
'multivalue' => $options['multivalue'], |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
if ($type instanceof DateTimeType) { |
|
|
|
|
83
|
|
|
$metadata->mapField([ |
84
|
|
|
'fieldName' => $fieldName, |
85
|
|
|
'type' => 'date', |
86
|
|
|
'nullable' => true, |
87
|
|
|
'multivalue' => $options['multivalue'], |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($type instanceof ReferenceType) { |
|
|
|
|
94
|
|
|
$metadata->mapManyToOne([ |
95
|
|
|
'fieldName' => $fieldName, |
96
|
|
|
'strategy' => 'hard', |
97
|
|
|
|
98
|
|
|
'nullable' => true, |
99
|
|
|
'cascade' => ClassMetadata::CASCADE_ALL, |
100
|
|
|
'multivalue' => $options['multivalue'], |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new \RuntimeException(sprintf( |
107
|
|
|
'Do not know how to map field of type "%s"', |
108
|
|
|
get_class($type) |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function mapCollectionType($fieldName, LoadedField $loadedField, ClassMetadata $metadata) |
113
|
|
|
{ |
114
|
|
|
$options = $loadedField->getOptions(); |
115
|
|
|
$collectionField = $this->fieldLoader->load($options['field'], $options['field_options']); |
116
|
|
|
$storageType = $collectionField->getStorageType(); |
117
|
|
|
$innerType = $storageType->getInnerType(); |
118
|
|
|
|
119
|
|
|
if ($innerType instanceof ObjectType) { |
120
|
|
|
$options = $storageType->getOptions(); |
121
|
|
|
$this->unrestrictChildClass($options['class'], $metadata); |
122
|
|
|
|
123
|
|
|
$metadata->mapChildren([ |
124
|
|
|
'fieldName' => $fieldName, |
125
|
|
|
'fetchDepth' => 1, |
126
|
|
|
'filter' => $this->encoder->encode($fieldName) . '-*', |
127
|
|
|
'cascade' => ClassMetadata::CASCADE_ALL, |
128
|
|
|
'nullable' => true, |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
if ($innerType instanceof ReferenceType) { |
|
|
|
|
135
|
|
|
$metadata->mapManyToMany([ |
136
|
|
|
'fieldName' => $fieldName, |
137
|
|
|
'strategy' => 'hard', |
138
|
|
|
|
139
|
|
|
'nullable' => true, |
140
|
|
|
'cascade' => ClassMetadata::CASCADE_ALL, |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// assume that other types are scalars... |
147
|
|
|
$this->__invoke($fieldName, $collectionField, $metadata, [ |
148
|
|
|
'multivalue' => true, |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function unrestrictChildClass(string $class, ClassMetadata $metadata) |
153
|
|
|
{ |
154
|
|
|
if (!$class) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!$childClasses = $metadata->getChildClasses()) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$childClasses[] = $class; |
163
|
|
|
$metadata->setChildClasses($childClasses); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.