Skip to content

Commit dc4d528

Browse files
[JsonPath] Add Nothing enum to support special nothing value
1 parent ca2f278 commit dc4d528

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/Symfony/Component/JsonPath/JsonCrawler.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,12 @@ private function evaluateScalar(string $expr, mixed $context): mixed
722722
$bracketContent = substr($path, 1, -1);
723723
$result = $this->evaluateBracket($bracketContent, $context);
724724

725-
return $result ? $result[0] : self::nothing();
725+
return $result ? $result[0] : Nothing::Nothing;
726726
}
727727

728728
$results = $this->evaluateTokensOnDecodedData(JsonPathTokenizer::tokenize(new JsonPath('$'.$path)), $context);
729729

730-
return $results ? $results[0] : self::nothing();
730+
return $results ? $results[0] : Nothing::Nothing;
731731
}
732732

733733
// function calls
@@ -785,7 +785,7 @@ private function evaluateFunction(string $name, string $args, mixed $context): m
785785
\is_string($value) => mb_strlen($value),
786786
\is_array($value) => \count($value),
787787
$value instanceof \stdClass => \count(get_object_vars($value)),
788-
default => self::nothing(),
788+
default => Nothing::Nothing,
789789
},
790790
'count' => $nodelistSize,
791791
'match' => match (true) {
@@ -796,7 +796,7 @@ private function evaluateFunction(string $name, string $args, mixed $context): m
796796
\is_string($value) && \is_string($argList[1] ?? null) => (bool) @preg_match("/{$this->transformJsonPathRegex($argList[1])}/u", $value),
797797
default => false,
798798
},
799-
'value' => 1 < $nodelistSize ? self::nothing() : (1 === $nodelistSize ? (\is_array($value) ? ($value[0] ?? null) : $value) : $value),
799+
'value' => 1 < $nodelistSize ? Nothing::Nothing : (1 === $nodelistSize ? (\is_array($value) ? ($value[0] ?? null) : $value) : $value),
800800
default => null,
801801
};
802802
}
@@ -833,8 +833,8 @@ private function compare(mixed $left, mixed $right, string $operator): bool
833833

834834
private function compareEquality(mixed $left, mixed $right): bool
835835
{
836-
$leftIsNothing = $left === self::nothing();
837-
$rightIsNothing = $right === self::nothing();
836+
$leftIsNothing = $left === Nothing::Nothing;
837+
$rightIsNothing = $right === Nothing::Nothing;
838838

839839
if (
840840
$leftIsNothing && $rightIsNothing
@@ -1103,11 +1103,6 @@ private function isValidMixedBracketExpression(string $expr): bool
11031103
return $hasFilter && $validMixed && 1 < \count($parts);
11041104
}
11051105

1106-
private static function nothing(): \stdClass
1107-
{
1108-
return self::$nothing ??= new \stdClass();
1109-
}
1110-
11111106
private function getValueIfKeyExists(mixed $value, string $key): array
11121107
{
11131108
return $this->isArrayOrObject($value) && \array_key_exists($key, $arrayValue = (array) $value) ? [$arrayValue[$key]] : [];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonPath;
13+
14+
enum Nothing
15+
{
16+
case Nothing;
17+
}

0 commit comments

Comments
 (0)