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