|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Mapping\Driver; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
|
23
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; |
|
24
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata; |
|
25
|
|
|
use Doctrine\ODM\MongoDB\Utility\CollectionHelper; |
|
26
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* XmlDriver is a metadata driver that enables mapping through XML files. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 1.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
class XmlDriver extends FileDriver |
|
34
|
|
|
{ |
|
35
|
|
|
const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
14 |
|
public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
41
|
|
|
{ |
|
42
|
14 |
|
parent::__construct($locator, $fileExtension); |
|
43
|
14 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritDoc} |
|
47
|
|
|
*/ |
|
48
|
8 |
|
public function loadMetadataForClass($className, ClassMetadata $class) |
|
49
|
|
|
{ |
|
50
|
|
|
/* @var $class ClassMetadataInfo */ |
|
51
|
|
|
/* @var $xmlRoot \SimpleXMLElement */ |
|
52
|
8 |
|
$xmlRoot = $this->getElement($className); |
|
53
|
8 |
|
if ( ! $xmlRoot) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
8 |
|
if ($xmlRoot->getName() == 'document') { |
|
58
|
8 |
|
if (isset($xmlRoot['repository-class'])) { |
|
59
|
8 |
|
$class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
|
60
|
|
|
} |
|
61
|
3 |
View Code Duplication |
} elseif ($xmlRoot->getName() == 'mapped-superclass') { |
|
|
|
|
|
|
62
|
2 |
|
$class->setCustomRepositoryClass( |
|
63
|
2 |
|
isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
64
|
|
|
); |
|
65
|
2 |
|
$class->isMappedSuperclass = true; |
|
|
|
|
|
|
66
|
1 |
|
} elseif ($xmlRoot->getName() == 'embedded-document') { |
|
67
|
1 |
|
$class->isEmbeddedDocument = true; |
|
|
|
|
|
|
68
|
1 |
|
} elseif ($xmlRoot->getName() == 'query-result-document') { |
|
69
|
1 |
|
$class->isQueryResultDocument = true; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
8 |
|
if (isset($xmlRoot['db'])) { |
|
72
|
4 |
|
$class->setDatabase((string) $xmlRoot['db']); |
|
73
|
|
|
} |
|
74
|
8 |
|
if (isset($xmlRoot['collection'])) { |
|
75
|
7 |
|
if (isset($xmlRoot['capped-collection'])) { |
|
76
|
|
|
$config = array('name' => (string) $xmlRoot['collection']); |
|
77
|
|
|
$config['capped'] = (bool) $xmlRoot['capped-collection']; |
|
78
|
|
|
if (isset($xmlRoot['capped-collection-max'])) { |
|
79
|
|
|
$config['max'] = (int) $xmlRoot['capped-collection-max']; |
|
80
|
|
|
} |
|
81
|
|
|
if (isset($xmlRoot['capped-collection-size'])) { |
|
82
|
|
|
$config['size'] = (int) $xmlRoot['capped-collection-size']; |
|
83
|
|
|
} |
|
84
|
|
|
$class->setCollection($config); |
|
85
|
|
|
} else { |
|
86
|
7 |
|
$class->setCollection((string) $xmlRoot['collection']); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
8 |
|
if (isset($xmlRoot['writeConcern'])) { |
|
90
|
|
|
$class->setWriteConcern((string) $xmlRoot['writeConcern']); |
|
91
|
|
|
} |
|
92
|
8 |
|
if (isset($xmlRoot['inheritance-type'])) { |
|
93
|
|
|
$inheritanceType = (string) $xmlRoot['inheritance-type']; |
|
94
|
|
|
$class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType)); |
|
95
|
|
|
} |
|
96
|
8 |
View Code Duplication |
if (isset($xmlRoot['change-tracking-policy'])) { |
|
|
|
|
|
|
97
|
2 |
|
$class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
98
|
|
|
} |
|
99
|
8 |
|
if (isset($xmlRoot->{'discriminator-field'})) { |
|
100
|
|
|
$discrField = $xmlRoot->{'discriminator-field'}; |
|
101
|
|
|
/* XSD only allows for "name", which is consistent with association |
|
102
|
|
|
* configurations, but fall back to "fieldName" for BC. |
|
103
|
|
|
*/ |
|
104
|
|
|
$class->setDiscriminatorField( |
|
105
|
|
|
isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName'] |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
8 |
View Code Duplication |
if (isset($xmlRoot->{'discriminator-map'})) { |
|
|
|
|
|
|
109
|
|
|
$map = array(); |
|
110
|
|
|
foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) { |
|
111
|
|
|
$map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
|
112
|
|
|
} |
|
113
|
|
|
$class->setDiscriminatorMap($map); |
|
114
|
|
|
} |
|
115
|
8 |
|
if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
116
|
|
|
$class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
|
117
|
|
|
} |
|
118
|
8 |
|
if (isset($xmlRoot->{'indexes'})) { |
|
119
|
2 |
|
foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
120
|
2 |
|
$this->addIndex($class, $index); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
8 |
|
if (isset($xmlRoot->{'shard-key'})) { |
|
124
|
|
|
$this->setShardKey($class, $xmlRoot->{'shard-key'}[0]); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
8 |
|
if (isset($xmlRoot['slave-okay'])) { |
|
127
|
1 |
|
$class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']); |
|
128
|
|
|
} |
|
129
|
8 |
|
if (isset($xmlRoot['read-only']) && 'true' === (string) $xmlRoot['read-only']) { |
|
130
|
|
|
$class->markReadOnly(); |
|
131
|
|
|
} |
|
132
|
8 |
|
if (isset($xmlRoot->{'read-preference'})) { |
|
133
|
|
|
$class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'})); |
|
134
|
|
|
} |
|
135
|
8 |
|
if (isset($xmlRoot->field)) { |
|
136
|
8 |
|
foreach ($xmlRoot->field as $field) { |
|
137
|
8 |
|
$mapping = array(); |
|
138
|
8 |
|
$attributes = $field->attributes(); |
|
139
|
8 |
|
foreach ($attributes as $key => $value) { |
|
140
|
8 |
|
$mapping[$key] = (string) $value; |
|
141
|
8 |
|
$booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse'); |
|
142
|
8 |
|
if (in_array($key, $booleanAttributes)) { |
|
143
|
8 |
|
$mapping[$key] = ('true' === $mapping[$key]); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
8 |
|
if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) { |
|
147
|
3 |
|
$mapping['options'] = array(); |
|
148
|
3 |
|
if (isset($field->{'id-generator-option'})) { |
|
149
|
1 |
|
foreach ($field->{'id-generator-option'} as $generatorOptions) { |
|
150
|
1 |
|
$attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
151
|
1 |
|
if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) { |
|
152
|
1 |
|
$mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
8 |
|
if (isset($attributes['not-saved'])) { |
|
159
|
|
|
$mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
8 |
|
if (isset($attributes['also-load'])) { |
|
163
|
|
|
$mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
|
164
|
8 |
|
} elseif (isset($attributes['version'])) { |
|
165
|
|
|
$mapping['version'] = ('true' === (string) $attributes['version']); |
|
166
|
8 |
|
} elseif (isset($attributes['lock'])) { |
|
167
|
|
|
$mapping['lock'] = ('true' === (string) $attributes['lock']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
8 |
|
$this->addFieldMapping($class, $mapping); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
8 |
|
if (isset($xmlRoot->{'embed-one'})) { |
|
174
|
1 |
|
foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
175
|
1 |
|
$this->addEmbedMapping($class, $embed, 'one'); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
8 |
|
if (isset($xmlRoot->{'embed-many'})) { |
|
179
|
1 |
|
foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
180
|
1 |
|
$this->addEmbedMapping($class, $embed, 'many'); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
8 |
|
if (isset($xmlRoot->{'reference-many'})) { |
|
184
|
3 |
|
foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
185
|
3 |
|
$this->addReferenceMapping($class, $reference, 'many'); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
8 |
|
if (isset($xmlRoot->{'reference-one'})) { |
|
189
|
2 |
|
foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
190
|
2 |
|
$this->addReferenceMapping($class, $reference, 'one'); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
8 |
|
if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
194
|
1 |
|
foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
195
|
1 |
|
$class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
8 |
|
if (isset($xmlRoot->{'also-load-methods'})) { |
|
199
|
1 |
|
foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
200
|
1 |
|
$class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
8 |
|
} |
|
204
|
|
|
|
|
205
|
9 |
|
private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
206
|
|
|
{ |
|
207
|
9 |
View Code Duplication |
if (isset($mapping['name'])) { |
|
|
|
|
|
|
208
|
9 |
|
$name = $mapping['name']; |
|
209
|
|
|
} elseif (isset($mapping['fieldName'])) { |
|
210
|
|
|
$name = $mapping['fieldName']; |
|
211
|
|
|
} else { |
|
212
|
|
|
throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
9 |
|
$class->mapField($mapping); |
|
216
|
|
|
|
|
217
|
|
|
// Index this field if either "index", "unique", or "sparse" are set |
|
218
|
9 |
View Code Duplication |
if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
|
|
|
|
|
|
219
|
9 |
|
return; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
1 |
|
$keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); |
|
223
|
1 |
|
$options = array(); |
|
224
|
|
|
|
|
225
|
1 |
|
if (isset($mapping['background'])) { |
|
226
|
|
|
$options['background'] = (boolean) $mapping['background']; |
|
227
|
|
|
} |
|
228
|
1 |
|
if (isset($mapping['drop-dups'])) { |
|
229
|
|
|
$options['dropDups'] = (boolean) $mapping['drop-dups']; |
|
230
|
|
|
} |
|
231
|
1 |
|
if (isset($mapping['index-name'])) { |
|
232
|
|
|
$options['name'] = (string) $mapping['index-name']; |
|
233
|
|
|
} |
|
234
|
1 |
|
if (isset($mapping['sparse'])) { |
|
235
|
1 |
|
$options['sparse'] = (boolean) $mapping['sparse']; |
|
236
|
|
|
} |
|
237
|
1 |
|
if (isset($mapping['unique'])) { |
|
238
|
1 |
|
$options['unique'] = (boolean) $mapping['unique']; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
1 |
|
$class->addIndex($keys, $options); |
|
242
|
1 |
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type) |
|
245
|
|
|
{ |
|
246
|
1 |
|
$attributes = $embed->attributes(); |
|
247
|
1 |
|
$defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
248
|
|
|
$mapping = array( |
|
249
|
1 |
|
'type' => $type, |
|
250
|
|
|
'embedded' => true, |
|
251
|
1 |
|
'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
252
|
1 |
|
'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null, |
|
253
|
1 |
|
'name' => (string) $attributes['field'], |
|
254
|
1 |
|
'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy, |
|
255
|
|
|
); |
|
256
|
1 |
|
if (isset($attributes['fieldName'])) { |
|
257
|
|
|
$mapping['fieldName'] = (string) $attributes['fieldName']; |
|
258
|
|
|
} |
|
259
|
1 |
View Code Duplication |
if (isset($embed->{'discriminator-field'})) { |
|
|
|
|
|
|
260
|
|
|
$attr = $embed->{'discriminator-field'}; |
|
261
|
|
|
$mapping['discriminatorField'] = (string) $attr['name']; |
|
262
|
|
|
} |
|
263
|
1 |
View Code Duplication |
if (isset($embed->{'discriminator-map'})) { |
|
|
|
|
|
|
264
|
|
|
foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
|
265
|
|
|
$attr = $discriminatorMapping->attributes(); |
|
266
|
|
|
$mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
1 |
|
if (isset($embed->{'default-discriminator-value'})) { |
|
270
|
|
|
$mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value']; |
|
271
|
|
|
} |
|
272
|
1 |
|
if (isset($attributes['not-saved'])) { |
|
273
|
|
|
$mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
|
274
|
|
|
} |
|
275
|
1 |
|
if (isset($attributes['also-load'])) { |
|
276
|
|
|
$mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
|
277
|
|
|
} |
|
278
|
1 |
|
$this->addFieldMapping($class, $mapping); |
|
279
|
1 |
|
} |
|
280
|
|
|
|
|
281
|
4 |
|
private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type) |
|
282
|
|
|
{ |
|
283
|
4 |
|
$cascade = array_keys((array) $reference->cascade); |
|
284
|
4 |
|
if (1 === count($cascade)) { |
|
285
|
1 |
|
$cascade = current($cascade) ?: next($cascade); |
|
286
|
|
|
} |
|
287
|
4 |
|
$attributes = $reference->attributes(); |
|
288
|
4 |
|
$defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
289
|
|
|
$mapping = array( |
|
290
|
4 |
|
'cascade' => $cascade, |
|
291
|
4 |
|
'orphanRemoval' => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false, |
|
292
|
4 |
|
'type' => $type, |
|
293
|
|
|
'reference' => true, |
|
294
|
4 |
|
'storeAs' => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF, |
|
295
|
4 |
|
'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
296
|
4 |
|
'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null, |
|
297
|
4 |
|
'name' => (string) $attributes['field'], |
|
298
|
4 |
|
'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy, |
|
299
|
4 |
|
'inversedBy' => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null, |
|
300
|
4 |
|
'mappedBy' => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null, |
|
301
|
4 |
|
'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null, |
|
302
|
4 |
|
'limit' => isset($attributes['limit']) ? (integer) $attributes['limit'] : null, |
|
303
|
4 |
|
'skip' => isset($attributes['skip']) ? (integer) $attributes['skip'] : null, |
|
304
|
|
|
'prime' => [], |
|
305
|
|
|
); |
|
306
|
|
|
|
|
307
|
4 |
|
if (isset($attributes['fieldName'])) { |
|
308
|
|
|
$mapping['fieldName'] = (string) $attributes['fieldName']; |
|
309
|
|
|
} |
|
310
|
4 |
View Code Duplication |
if (isset($reference->{'discriminator-field'})) { |
|
|
|
|
|
|
311
|
|
|
$attr = $reference->{'discriminator-field'}; |
|
312
|
|
|
$mapping['discriminatorField'] = (string) $attr['name']; |
|
313
|
|
|
} |
|
314
|
4 |
View Code Duplication |
if (isset($reference->{'discriminator-map'})) { |
|
|
|
|
|
|
315
|
|
|
foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
|
316
|
|
|
$attr = $discriminatorMapping->attributes(); |
|
317
|
|
|
$mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
4 |
|
if (isset($reference->{'default-discriminator-value'})) { |
|
321
|
|
|
$mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value']; |
|
322
|
|
|
} |
|
323
|
4 |
|
if (isset($reference->{'sort'})) { |
|
324
|
|
View Code Duplication |
foreach ($reference->{'sort'}->{'sort'} as $sort) { |
|
|
|
|
|
|
325
|
|
|
$attr = $sort->attributes(); |
|
326
|
|
|
$mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc'; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
4 |
|
if (isset($reference->{'criteria'})) { |
|
330
|
|
View Code Duplication |
foreach ($reference->{'criteria'}->{'criteria'} as $criteria) { |
|
|
|
|
|
|
331
|
|
|
$attr = $criteria->attributes(); |
|
332
|
|
|
$mapping['criteria'][(string) $attr['field']] = (string) $attr['value']; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
4 |
|
if (isset($attributes['not-saved'])) { |
|
336
|
|
|
$mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
|
337
|
|
|
} |
|
338
|
4 |
|
if (isset($attributes['also-load'])) { |
|
339
|
|
|
$mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
|
340
|
|
|
} |
|
341
|
4 |
|
if (isset($reference->{'prime'})) { |
|
342
|
1 |
|
foreach ($reference->{'prime'}->{'field'} as $field) { |
|
343
|
1 |
|
$attr = $field->attributes(); |
|
344
|
1 |
|
$mapping['prime'][] = (string) $attr['name']; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
4 |
|
$this->addFieldMapping($class, $mapping); |
|
349
|
4 |
|
} |
|
350
|
|
|
|
|
351
|
2 |
|
private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) |
|
352
|
|
|
{ |
|
353
|
2 |
|
$attributes = $xmlIndex->attributes(); |
|
354
|
|
|
|
|
355
|
2 |
|
$keys = array(); |
|
356
|
|
|
|
|
357
|
2 |
View Code Duplication |
foreach ($xmlIndex->{'key'} as $key) { |
|
|
|
|
|
|
358
|
2 |
|
$keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
2 |
|
$options = array(); |
|
362
|
|
|
|
|
363
|
2 |
|
if (isset($attributes['background'])) { |
|
364
|
|
|
$options['background'] = ('true' === (string) $attributes['background']); |
|
365
|
|
|
} |
|
366
|
2 |
|
if (isset($attributes['drop-dups'])) { |
|
367
|
|
|
$options['dropDups'] = ('true' === (string) $attributes['drop-dups']); |
|
368
|
|
|
} |
|
369
|
2 |
|
if (isset($attributes['name'])) { |
|
370
|
|
|
$options['name'] = (string) $attributes['name']; |
|
371
|
|
|
} |
|
372
|
2 |
|
if (isset($attributes['sparse'])) { |
|
373
|
|
|
$options['sparse'] = ('true' === (string) $attributes['sparse']); |
|
374
|
|
|
} |
|
375
|
2 |
|
if (isset($attributes['unique'])) { |
|
376
|
|
|
$options['unique'] = ('true' === (string) $attributes['unique']); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
2 |
View Code Duplication |
if (isset($xmlIndex->{'option'})) { |
|
|
|
|
|
|
380
|
|
|
foreach ($xmlIndex->{'option'} as $option) { |
|
381
|
|
|
$value = (string) $option['value']; |
|
382
|
|
|
if ($value === 'true') { |
|
383
|
|
|
$value = true; |
|
384
|
|
|
} elseif ($value === 'false') { |
|
385
|
|
|
$value = false; |
|
386
|
|
|
} elseif (is_numeric($value)) { |
|
387
|
|
|
$value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
|
388
|
|
|
} |
|
389
|
|
|
$options[(string) $option['name']] = $value; |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
2 |
|
if (isset($xmlIndex->{'partial-filter-expression'})) { |
|
394
|
2 |
|
$partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'}; |
|
395
|
|
|
|
|
396
|
2 |
|
if (isset($partialFilterExpressionMapping->and)) { |
|
397
|
2 |
|
foreach ($partialFilterExpressionMapping->and as $and) { |
|
398
|
2 |
|
if (! isset($and->field)) { |
|
399
|
1 |
|
continue; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
2 |
|
$partialFilterExpression = $this->getPartialFilterExpression($and->field); |
|
403
|
2 |
|
if (! $partialFilterExpression) { |
|
|
|
|
|
|
404
|
|
|
continue; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
2 |
|
$options['partialFilterExpression']['$and'][] = $partialFilterExpression; |
|
408
|
|
|
} |
|
409
|
1 |
|
} elseif (isset($partialFilterExpressionMapping->field)) { |
|
410
|
1 |
|
$partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field); |
|
411
|
|
|
|
|
412
|
1 |
|
if ($partialFilterExpression) { |
|
|
|
|
|
|
413
|
1 |
|
$options['partialFilterExpression'] = $partialFilterExpression; |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
2 |
|
$class->addIndex($keys, $options); |
|
419
|
2 |
|
} |
|
420
|
|
|
|
|
421
|
2 |
|
private function getPartialFilterExpression(\SimpleXMLElement $fields) |
|
422
|
|
|
{ |
|
423
|
2 |
|
$partialFilterExpression = []; |
|
424
|
2 |
|
foreach ($fields as $field) { |
|
425
|
2 |
|
$operator = (string) $field['operator'] ?: null; |
|
426
|
|
|
|
|
427
|
2 |
|
if (! isset($field['value'])) { |
|
428
|
1 |
|
if (! isset($field->field)) { |
|
429
|
|
|
continue; |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
1 |
|
$nestedExpression = $this->getPartialFilterExpression($field->field); |
|
433
|
1 |
|
if (! $nestedExpression) { |
|
434
|
|
|
continue; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
1 |
|
$value = $nestedExpression; |
|
438
|
|
|
} else { |
|
439
|
2 |
|
$value = trim((string) $field['value']); |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
2 |
|
if ($value === 'true') { |
|
443
|
|
|
$value = true; |
|
444
|
2 |
|
} elseif ($value === 'false') { |
|
445
|
|
|
$value = false; |
|
446
|
2 |
|
} elseif (is_numeric($value)) { |
|
447
|
1 |
|
$value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
2 |
|
$partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
2 |
|
return $partialFilterExpression; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
1 |
|
private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey) |
|
457
|
|
|
{ |
|
458
|
1 |
|
$attributes = $xmlShardkey->attributes(); |
|
459
|
|
|
|
|
460
|
1 |
|
$keys = array(); |
|
461
|
1 |
|
$options = array(); |
|
462
|
1 |
View Code Duplication |
foreach ($xmlShardkey->{'key'} as $key) { |
|
|
|
|
|
|
463
|
1 |
|
$keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc'; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
1 |
|
if (isset($attributes['unique'])) { |
|
467
|
1 |
|
$options['unique'] = ('true' === (string) $attributes['unique']); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
1 |
|
if (isset($attributes['numInitialChunks'])) { |
|
471
|
1 |
|
$options['numInitialChunks'] = (int) $attributes['numInitialChunks']; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
1 |
View Code Duplication |
if (isset($xmlShardkey->{'option'})) { |
|
|
|
|
|
|
475
|
|
|
foreach ($xmlShardkey->{'option'} as $option) { |
|
476
|
|
|
$value = (string) $option['value']; |
|
477
|
|
|
if ($value === 'true') { |
|
478
|
|
|
$value = true; |
|
479
|
|
|
} elseif ($value === 'false') { |
|
480
|
|
|
$value = false; |
|
481
|
|
|
} elseif (is_numeric($value)) { |
|
482
|
|
|
$value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
|
483
|
|
|
} |
|
484
|
|
|
$options[(string) $option['name']] = $value; |
|
485
|
|
|
} |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
1 |
|
$class->setShardKey($keys, $options); |
|
489
|
1 |
|
} |
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* Parses <read-preference> to a format suitable for the underlying driver. |
|
493
|
|
|
* |
|
494
|
|
|
* list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference}); |
|
495
|
|
|
* |
|
496
|
|
|
* @param \SimpleXMLElement $xmlReadPreference |
|
497
|
|
|
* @return array |
|
498
|
|
|
*/ |
|
499
|
|
|
private function transformReadPreference($xmlReadPreference) |
|
500
|
|
|
{ |
|
501
|
|
|
$tags = null; |
|
502
|
|
|
if (isset($xmlReadPreference->{'tag-set'})) { |
|
503
|
|
|
$tags = []; |
|
504
|
|
|
foreach ($xmlReadPreference->{'tag-set'} as $tagSet) { |
|
505
|
|
|
$set = []; |
|
506
|
|
|
foreach ($tagSet->tag as $tag) { |
|
507
|
|
|
$set[(string) $tag['name']] = (string) $tag['value']; |
|
508
|
|
|
} |
|
509
|
|
|
$tags[] = $set; |
|
510
|
|
|
} |
|
511
|
|
|
} |
|
512
|
|
|
return [(string) $xmlReadPreference['mode'], $tags]; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* {@inheritDoc} |
|
517
|
|
|
*/ |
|
518
|
8 |
|
protected function loadMappingFile($file) |
|
519
|
|
|
{ |
|
520
|
8 |
|
$result = array(); |
|
521
|
8 |
|
$xmlElement = simplexml_load_file($file); |
|
522
|
|
|
|
|
523
|
8 |
|
foreach (array('document', 'embedded-document', 'mapped-superclass', 'query-result-document') as $type) { |
|
524
|
8 |
|
if (isset($xmlElement->$type)) { |
|
525
|
8 |
|
foreach ($xmlElement->$type as $documentElement) { |
|
526
|
8 |
|
$documentName = (string) $documentElement['name']; |
|
527
|
8 |
|
$result[$documentName] = $documentElement; |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
8 |
|
return $result; |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
|
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.