diff --git a/src/Schema/JSONDocument.php b/src/Schema/JSONDocument.php index 38075e2..13f267c 100644 --- a/src/Schema/JSONDocument.php +++ b/src/Schema/JSONDocument.php @@ -9,7 +9,7 @@ class JSONDocument { private bool $hasError = false; - /** @var null|callable(string, null|array):void */ + /** @var null|callable(string, int, null|array):void */ private $errorCallback = null; public function __construct( @@ -55,6 +55,7 @@ public function set(string $key, mixed $value):void { * properties may be set once an error is set. * * @param string $message The message to be set + * @param int $statusCode The HTTP response status code associated with the error * @param null|array $context An optional array containing error context to be returned * @param string $property Optional property name for the error message * @param string $contextProperty Optional property name for the error context array @@ -63,6 +64,7 @@ public function set(string $key, mixed $value):void { */ public function error( string $message, + int $statusCode = 400, ?array $context = null, string $property = "error", string $contextProperty = "errorContext" @@ -80,7 +82,7 @@ public function error( $this->hasError = true; if($this->errorCallback !== null) { - call_user_func($this->errorCallback, $message, $context); + call_user_func($this->errorCallback, $message, $statusCode, $context); } } diff --git a/test/phpunit/Schema/JSONDocumentTest.php b/test/phpunit/Schema/JSONDocumentTest.php index 464f1e0..05ed890 100644 --- a/test/phpunit/Schema/JSONDocumentTest.php +++ b/test/phpunit/Schema/JSONDocumentTest.php @@ -166,8 +166,8 @@ public function testError_disallowsSetAfterError():void { public function testSetErrorCallback():void { $calls = []; - $callback = function(string $message, ?array $context = null)use(&$calls):void { - array_push($calls, [$message, $context]); + $callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void { + array_push($calls, [$message, $statusCode, $context]); }; $sut = new JSONDocument(); @@ -176,20 +176,38 @@ public function testSetErrorCallback():void { $sut->error("Test"); self::assertCount(1, $calls); self::assertSame("Test", $calls[0][0]); - self::assertNull($calls[0][1]); + self::assertSame(400, $calls[0][1]); + self::assertNull($calls[0][2]); } public function testSetErrorCallback_context():void { $calls = []; - $callback = function(string $message, ?array $context = null)use(&$calls):void { - array_push($calls, [$message, $context]); + $callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void { + array_push($calls, [$message, $statusCode, $context]); }; $sut = new JSONDocument(); $sut->setErrorCallback($callback); - $sut->error("Test", ["example" => "message"]); + $sut->error("Test", context: ["example" => "message"]); self::assertCount(1, $calls); - self::assertSame(["example" => "message"], $calls[0][1]); + self::assertSame(400, $calls[0][1]); + self::assertSame(["example" => "message"], $calls[0][2]); + } + + public function testSetErrorCallback_statusCode():void { + $calls = []; + $callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void { + array_push($calls, [$message, $statusCode, $context]); + }; + + $sut = new JSONDocument(); + $sut->setErrorCallback($callback); + + $sut->error("Test", 422); + self::assertCount(1, $calls); + self::assertSame("Test", $calls[0][0]); + self::assertSame(422, $calls[0][1]); + self::assertNull($calls[0][2]); } }