Skip to content

Commit

Permalink
Merge branch 'SDK-50' into 'master'
Browse files Browse the repository at this point in the history
SDK-50 Update SDK to handle new error/message format

Closes SDK-50

See merge request tools/php-sdk!44
  • Loading branch information
Robert committed Sep 9, 2021
2 parents 6daecd2 + 47aff3d commit 134dbaf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Servebolt/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
10 changes: 9 additions & 1 deletion src/Servebolt/Traits/HasErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -38,7 +46,7 @@ public function getFirstError()
public function getFirstErrorMessage()
{
if ($error = $this->getFirstError()) {
return $error->message;
return $error->title;
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/Servebolt/Traits/HasMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/ResponseObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit 134dbaf

Please sign in to comment.