diff --git a/src/Servebolt/Response.php b/src/Servebolt/Response.php index da01028..9b032b3 100644 --- a/src/Servebolt/Response.php +++ b/src/Servebolt/Response.php @@ -211,7 +211,7 @@ private function parseMessages() : void return; } if (isset($this->responseBody->messages) && is_array($this->responseBody->messages)) { - $this->messages = $this->responseBody->messages; + $this->setMessages($this->responseBody->messages); } } @@ -221,7 +221,7 @@ private function parseErrors() : void return; } if (isset($this->responseBody->errors) && is_array($this->responseBody->errors)) { - $this->errors = $this->responseBody->errors; + $this->setErrors($this->responseBody->errors); } } } diff --git a/src/Servebolt/Traits/HasErrors.php b/src/Servebolt/Traits/HasErrors.php index 734ae2b..1459a92 100644 --- a/src/Servebolt/Traits/HasErrors.php +++ b/src/Servebolt/Traits/HasErrors.php @@ -7,6 +7,14 @@ trait HasErrors private $errors = []; + /** + * @param array $errors + */ + private function setErrors($errors) : void + { + $this->errors = $errors; + } + public function hasErrors() : bool { return ! empty($this->errors); @@ -38,7 +46,7 @@ public function getFirstError() public function getFirstErrorMessage() { if ($error = $this->getFirstError()) { - return $error->message; + return $error->title; } } diff --git a/src/Servebolt/Traits/HasMessages.php b/src/Servebolt/Traits/HasMessages.php index c722296..1049235 100644 --- a/src/Servebolt/Traits/HasMessages.php +++ b/src/Servebolt/Traits/HasMessages.php @@ -7,6 +7,14 @@ trait HasMessages private $messages = []; + /** + * @param array $messages + */ + private function setMessages($messages) : void + { + $this->messages = $messages; + } + public function hasMessages() : bool { return count($this->messages) > 0; @@ -22,6 +30,16 @@ public function getMessages() : array return $this->messages; } + /** + * @return null|string + */ + public function getFirstMessageString() + { + if ($message = $this->getFirstMessage()) { + return $message->message; + } + } + /** * @return null|object */ diff --git a/tests/Unit/ResponseObjectTest.php b/tests/Unit/ResponseObjectTest.php index a71b56f..e80f86c 100644 --- a/tests/Unit/ResponseObjectTest.php +++ b/tests/Unit/ResponseObjectTest.php @@ -32,6 +32,7 @@ public function testResponseWithErrorMessages() $this->assertEquals($errorMessages, $responseObject->getErrors()); $this->assertEquals(current($errorMessages), $responseObject->getFirstError()); $this->assertEquals('Invalid input', $responseObject->getFirstError()->title); + $this->assertEquals('Invalid input', $responseObject->getFirstErrorMessage()); $this->assertEquals('Invalid URL www.servebolt.nl.', $responseObject->getFirstError()->detail); $this->assertEquals('1115', $responseObject->getFirstError()->code); $this->assertEquals((object) [], $responseObject->getFirstError()->source); @@ -53,6 +54,7 @@ public function testResponseWithMessages() $this->assertTrue($responseObject->hasMessages()); $this->assertEquals($messages, $responseObject->getMessages()); $this->assertEquals(current($messages), $responseObject->getFirstMessage()); + $this->assertEquals('This is a notification about something', $responseObject->getFirstMessageString()); $this->assertEquals('This is a notification about something', $responseObject->getFirstMessage()->message); }