Skip to content

Commit

Permalink
fix-parser-error-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Mar 6, 2024
1 parent 4ffed1f commit 3c067e6
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 358 deletions.
21 changes: 17 additions & 4 deletions src/ParserErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ public function __toString(): string
public function addParserErrorException(self $parserErrorException, null|int|string $key = null): self
{
if (null !== $key) {
$this->errors[$key] = array_merge_recursive($this->errors[$key] ?? [], $parserErrorException->getErrors());
$this->errors = $this->mergeErrors([$key => $parserErrorException->getErrors()], $this->errors);

return $this;
}

$this->errors = array_merge_recursive($this->errors, $parserErrorException->getErrors());
$this->errors = $this->mergeErrors($parserErrorException->getErrors(), $this->errors);

return $this;
}

public function addError(Error $error, null|int|string $key = null): self
{
if (null !== $key) {
$this->errors[$key] = array_merge($this->errors[$key] ?? [], [$error]);
$this->errors = $this->mergeErrors([$key => [$error]], $this->errors);

return $this;
}

$this->errors = array_merge($this->errors, [$error]);
$this->errors = $this->mergeErrors([$error], $this->errors);

return $this;
}
Expand All @@ -63,6 +63,19 @@ public function getApiProblemErrorMessages(): array
return $this->flatErrorsToApiProblemMessages($this->errors);
}

private function mergeErrors(array $errors, array $mergedErrors): array
{
foreach ($errors as $key => $error) {
if ($error instanceof Error) {
$mergedErrors[] = $error;
} else {
$mergedErrors[$key] = $this->mergeErrors($error, $mergedErrors[$key] ?? []);
}
}

return $mergedErrors;
}

private function flatErrorsToApiProblemMessages(array $errors, string $path = ''): array
{
$errorsToApiProblemMessages = [];
Expand Down
Loading

0 comments on commit 3c067e6

Please sign in to comment.