From f3fedcc0c8b24f6e86f7cec16f06709cfd488819 Mon Sep 17 00:00:00 2001 From: ratthanan Date: Sun, 21 Mar 2021 07:48:15 +0700 Subject: [PATCH 1/3] Actionable error message for large json documents --- src/Constraint/JsonValueMatchesMany.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Constraint/JsonValueMatchesMany.php b/src/Constraint/JsonValueMatchesMany.php index 777762a..4d80eb4 100644 --- a/src/Constraint/JsonValueMatchesMany.php +++ b/src/Constraint/JsonValueMatchesMany.php @@ -17,6 +17,9 @@ class JsonValueMatchesMany extends Constraint /** @var JsonValueMatches[] */ private $constraints = array(); + /** @var string[] */ + private $failedConstraints = array(); + /** * JsonValueMatchesMany constructor. * @@ -59,11 +62,23 @@ function (Constraint $constraint) { */ protected function matches($other): bool { + $result = true; foreach ($this->constraints as $constraint) { if (!$constraint->evaluate($other, '', true)) { - return false; + $result = false; + $this->failedConstraints[] = $constraint->toString(); } } - return true; + return $result; + } + + /** + * Returns a string representation of matches that evaluate to false. + * + * @return string + */ + protected function additionalFailureDescription($other): string + { + return "\n" . implode("\n", $this->failedConstraints); } } From dcea800f21e10d01817942c8e78e8a65edf57d75 Mon Sep 17 00:00:00 2001 From: hibikiledo Date: Mon, 29 Mar 2021 13:33:12 +0700 Subject: [PATCH 2/3] Build additional error description statelessly --- src/Constraint/JsonValueMatchesMany.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Constraint/JsonValueMatchesMany.php b/src/Constraint/JsonValueMatchesMany.php index 4d80eb4..9d51569 100644 --- a/src/Constraint/JsonValueMatchesMany.php +++ b/src/Constraint/JsonValueMatchesMany.php @@ -17,9 +17,6 @@ class JsonValueMatchesMany extends Constraint /** @var JsonValueMatches[] */ private $constraints = array(); - /** @var string[] */ - private $failedConstraints = array(); - /** * JsonValueMatchesMany constructor. * @@ -62,14 +59,12 @@ function (Constraint $constraint) { */ protected function matches($other): bool { - $result = true; foreach ($this->constraints as $constraint) { if (!$constraint->evaluate($other, '', true)) { - $result = false; - $this->failedConstraints[] = $constraint->toString(); + return false; } } - return $result; + return true; } /** @@ -79,6 +74,15 @@ protected function matches($other): bool */ protected function additionalFailureDescription($other): string { - return "\n" . implode("\n", $this->failedConstraints); + /** @var string[] */ + $failedConstraints = array(); + + foreach ($this->constraints as $constraint) { + if (!$constraint->evaluate($other, '', true)) { + $failedConstraints[] = $constraint->toString(); + } + } + + return "\n" . implode("\n", $failedConstraints); } } From 403c4a7bc9c61bb77d81b81eedf5193f8393ca4a Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Mon, 29 Mar 2021 08:45:55 +0200 Subject: [PATCH 3/3] Update src/Constraint/JsonValueMatchesMany.php --- src/Constraint/JsonValueMatchesMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Constraint/JsonValueMatchesMany.php b/src/Constraint/JsonValueMatchesMany.php index 9d51569..7d52da8 100644 --- a/src/Constraint/JsonValueMatchesMany.php +++ b/src/Constraint/JsonValueMatchesMany.php @@ -69,7 +69,7 @@ protected function matches($other): bool /** * Returns a string representation of matches that evaluate to false. - * + * * @return string */ protected function additionalFailureDescription($other): string