From 8d769d65a9914abf666933fc58206518a5d72d6f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 21 Jan 2025 10:52:20 +0100 Subject: [PATCH 1/2] Error objects: source object may refer to a header See https://jsonapi.org/format/#error-objects --- src/Core/Document/ErrorSource.php | 50 +++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Core/Document/ErrorSource.php b/src/Core/Document/ErrorSource.php index 0b84555..38f5804 100644 --- a/src/Core/Document/ErrorSource.php +++ b/src/Core/Document/ErrorSource.php @@ -31,6 +31,11 @@ class ErrorSource implements Serializable */ private ?string $parameter; + /** + * @var string|null + */ + private ?string $header; + /** * @param ErrorSource|array|null $value * @return ErrorSource @@ -60,7 +65,8 @@ public static function fromArray(array $source): self { return new self( $source['pointer'] ?? null, - $source['parameter'] ?? null + $source['parameter'] ?? null, + $source['header'] ?? null, ); } @@ -69,11 +75,13 @@ public static function fromArray(array $source): self * * @param string|null $pointer * @param string|null $parameter + * @param string|null $header */ - public function __construct(?string $pointer = null, ?string $parameter = null) + public function __construct(string $pointer = null, string $parameter = null, string $header = null) { $this->pointer = $pointer; $this->parameter = $parameter; + $this->header = $header; } /** @@ -149,12 +157,47 @@ public function withoutParameter(): self return $this; } + /** + * A string indicating which request header caused the error. + * + * @return string|null + */ + public function header(): ?string + { + return $this->header; + } + + /** + * Add a string indicating which request header caused the error. + * + * @param string|null $header + * @return $this + */ + public function setHeader(?string $header): self + { + $this->header = $header; + + return $this; + } + + /** + * Remove the source header. + * + * @return $this + */ + public function withoutHeader(): self + { + $this->header = null; + + return $this; + } + /** * @return bool */ public function isEmpty(): bool { - return empty($this->pointer) && empty($this->parameter); + return empty($this->pointer) && empty($this->parameter) && empty($this->header); } /** @@ -173,6 +216,7 @@ public function toArray() return array_filter([ 'parameter' => $this->parameter, 'pointer' => $this->pointer, + 'header' => $this->header, ]); } From f05fb5e59908b9848d2cc54cd480fe1a557020b5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 22 Jan 2025 13:07:12 +0100 Subject: [PATCH 2/2] Fix PHP 8.4 deprecation wranings. --- src/Core/Document/ErrorSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Document/ErrorSource.php b/src/Core/Document/ErrorSource.php index 38f5804..13ae81c 100644 --- a/src/Core/Document/ErrorSource.php +++ b/src/Core/Document/ErrorSource.php @@ -77,7 +77,7 @@ public static function fromArray(array $source): self * @param string|null $parameter * @param string|null $header */ - public function __construct(string $pointer = null, string $parameter = null, string $header = null) + public function __construct(?string $pointer = null, ?string $parameter = null, ?string $header = null) { $this->pointer = $pointer; $this->parameter = $parameter;