Skip to content

Commit 18d9ac7

Browse files
authored
Fix using null values in partial filter expressions (#2300)
1 parent 4984784 commit 18d9ac7

3 files changed

Lines changed: 36 additions & 29 deletions

File tree

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -505,16 +505,7 @@ private function addIndex(ClassMetadata $class, SimpleXMLElement $xmlIndex): voi
505505

506506
if (isset($xmlIndex->{'option'})) {
507507
foreach ($xmlIndex->{'option'} as $option) {
508-
$value = (string) $option['value'];
509-
if ($value === 'true') {
510-
$value = true;
511-
} elseif ($value === 'false') {
512-
$value = false;
513-
} elseif (is_numeric($value)) {
514-
$value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
515-
}
516-
517-
$options[(string) $option['name']] = $value;
508+
$options[(string) $option['name']] = $this->convertXMLElementValue((string) $option['value']);
518509
}
519510
}
520511

@@ -564,15 +555,7 @@ private function getPartialFilterExpression(SimpleXMLElement $fields): array
564555

565556
$value = $nestedExpression;
566557
} else {
567-
$value = trim((string) $field['value']);
568-
}
569-
570-
if ($value === 'true') {
571-
$value = true;
572-
} elseif ($value === 'false') {
573-
$value = false;
574-
} elseif (is_numeric($value)) {
575-
$value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
558+
$value = $this->convertXMLElementValue((string) $field['value']);
576559
}
577560

578561
$partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
@@ -581,6 +564,37 @@ private function getPartialFilterExpression(SimpleXMLElement $fields): array
581564
return $partialFilterExpression;
582565
}
583566

567+
/**
568+
* Converts XML strings to scalar values.
569+
*
570+
* Special strings "false", "true", and "null" are converted to their
571+
* respective values. Numeric strings are cast to int or float depending on
572+
* whether they contain decimal separators or not.
573+
*
574+
* @return scalar|null
575+
*/
576+
private function convertXMLElementValue(string $value)
577+
{
578+
$value = trim($value);
579+
580+
switch ($value) {
581+
case 'true':
582+
return true;
583+
584+
case 'false':
585+
return false;
586+
587+
case 'null':
588+
return null;
589+
}
590+
591+
if (! is_numeric($value)) {
592+
return $value;
593+
}
594+
595+
return preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
596+
}
597+
584598
private function setShardKey(ClassMetadata $class, SimpleXMLElement $xmlShardkey): void
585599
{
586600
$attributes = $xmlShardkey->attributes();
@@ -601,16 +615,7 @@ private function setShardKey(ClassMetadata $class, SimpleXMLElement $xmlShardkey
601615

602616
if (isset($xmlShardkey->{'option'})) {
603617
foreach ($xmlShardkey->{'option'} as $option) {
604-
$value = (string) $option['value'];
605-
if ($value === 'true') {
606-
$value = true;
607-
} elseif ($value === 'false') {
608-
$value = false;
609-
} elseif (is_numeric($value)) {
610-
$value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
611-
}
612-
613-
$options[(string) $option['name']] = $value;
618+
$options[(string) $option['name']] = $this->convertXMLElementValue((string) $option['value']);
614619
}
615620
}
616621

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/AbstractDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ public function testPartialFilterExpressions()
307307
'partialFilterExpression' => [
308308
'version' => ['$gt' => 1],
309309
'discr' => ['$eq' => 'default'],
310+
'parent' => ['$eq' => null],
310311
],
311312
],
312313
],

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.PartialFilterDocument.dcm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<partial-filter-expression>
1414
<field name="version" value="1" operator="gt" />
1515
<field name="discr" operator="eq" value="default" />
16+
<field name="parent" operator="eq" value="null" />
1617
</partial-filter-expression>
1718
</index>
1819
<index>

0 commit comments

Comments
 (0)