diff --git a/README.md b/README.md index a446304..b8fbc25 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,29 @@ PSR7 Response implementation for the [Problem Details for HTTP APIs (RFC7807)](h To report a single error, all you need to do is pass in the mandatory parameters and you'll be fine. -**Using the constructor** +**Straightforward usage (recommended)** + +This is probably the fastest way and it's really convenient as it hides the presenter and creating the instances from you. ```php - - + Input values do not match the requirements 500 User data is not valid. diff --git a/src/ApiProblem/ApiProblem.php b/src/ApiProblem/ApiProblem.php index 9bec65c..cbb428c 100644 --- a/src/ApiProblem/ApiProblem.php +++ b/src/ApiProblem/ApiProblem.php @@ -10,6 +10,7 @@ namespace NilPortugues\Api\Problem; +use Exception; use NilPortugues\Assert\Assert; /** @@ -117,14 +118,14 @@ public function __construct($status, $detail, $title = '', $type = '', array $ad } /** - * @param \Exception $exception - * @param string $title - * @param string $type - * @param array $additionalDetails + * @param Exception $exception + * @param string $title + * @param string $type + * @param array $additionalDetails * * @return ApiProblem */ - public static function fromException(\Exception $exception, $title = '', $type = '', array $additionalDetails = []) + public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = []) { $eCode = $exception->getCode(); $code = (isset($eCode) && is_int($eCode)) ? $eCode : 500; diff --git a/src/ApiProblem/ApiProblemResponse.php b/src/ApiProblem/ApiProblemResponse.php index 6144628..dfe80cf 100644 --- a/src/ApiProblem/ApiProblemResponse.php +++ b/src/ApiProblem/ApiProblemResponse.php @@ -10,12 +10,12 @@ namespace NilPortugues\Api\Problem; +use Exception; use GuzzleHttp\Psr7\Response; +use NilPortugues\Api\Problem\Presenter\JsonPresenter; use NilPortugues\Api\Problem\Presenter\Presenter; +use NilPortugues\Api\Problem\Presenter\XmlPresenter; -/** - * Class ApiProblemResponse. - */ class ApiProblemResponse extends Response { /** @@ -41,4 +41,66 @@ protected function responseHeader(Presenter $presenter) { return ($presenter->format() === 'xml') ? 'application/problem+xml' : 'application/problem+json'; } + + /** + * @param $status + * @param $detail + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function json($status, $detail, $title = '', $type = '', array $additionalDetails = []) + { + return new self(new JsonPresenter(new ApiProblem($status, $detail, $title, $type, $additionalDetails))); + } + + /** + * @param Exception $exception + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function fromExceptionToJson( + Exception $exception, + $title = '', + $type = '', + array $additionalDetails = [] + ) { + return new self(new JsonPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); + } + + /** + * @param $status + * @param $detail + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function xml($status, $detail, $title = '', $type = '', array $additionalDetails = []) + { + return new self(new XmlPresenter(new ApiProblem($status, $detail, $title, $type, $additionalDetails))); + } + + /** + * @param Exception $exception + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function fromExceptionToXml( + Exception $exception, + $title = '', + $type = '', + array $additionalDetails = [] + ) { + return new self(new XmlPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); + } } diff --git a/src/ApiProblem/Presenter/XmlPresenter.php b/src/ApiProblem/Presenter/XmlPresenter.php index 47f534e..9732d07 100644 --- a/src/ApiProblem/Presenter/XmlPresenter.php +++ b/src/ApiProblem/Presenter/XmlPresenter.php @@ -36,7 +36,7 @@ public function contents() return << - + $flattenedLines XML; diff --git a/tests/ApiProblem/ApiProblemResponseTest.php b/tests/ApiProblem/ApiProblemResponseTest.php index a425e07..355e174 100644 --- a/tests/ApiProblem/ApiProblemResponseTest.php +++ b/tests/ApiProblem/ApiProblemResponseTest.php @@ -37,7 +37,81 @@ public function testItWillCreateXmlResponse() $body = << - + +Not Found +404 +User with id 5 not found. +http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html + +XML; + + $this->assertEquals($response->getBody()->getContents(), $body); + $this->assertEquals($response->getStatusCode(), 404); + $this->assertEquals($response->getHeaderLine('Content-type'), 'application/problem+xml'); + } + + public function testToJson() + { + $response = ApiProblemResponse::json(404, 'User with id 5 not found.', 'Not Found'); + + $body = <<assertEquals($response->getBody()->getContents(), $body); + $this->assertEquals($response->getStatusCode(), 404); + $this->assertEquals($response->getHeaderLine('Content-type'), 'application/problem+json'); + } + + public function testToJsonFromException() + { + $exception = new \Exception('User with id 5 not found.', 404); + $response = ApiProblemResponse::fromExceptionToJson($exception); + + $body = <<assertEquals($response->getBody()->getContents(), $body); + $this->assertEquals($response->getStatusCode(), 404); + $this->assertEquals($response->getHeaderLine('Content-type'), 'application/problem+json'); + } + + public function testToXml() + { + $response = ApiProblemResponse::xml(404, 'User with id 5 not found.', 'Not Found'); + + $body = << + +Not Found +404 +User with id 5 not found. + +XML; + + $this->assertEquals($response->getBody()->getContents(), $body); + $this->assertEquals($response->getStatusCode(), 404); + $this->assertEquals($response->getHeaderLine('Content-type'), 'application/problem+xml'); + } + + public function testToXmlFromException() + { + $exception = new \Exception('User with id 5 not found.', 404); + $response = ApiProblemResponse::fromExceptionToXml($exception); + + $body = << + Not Found 404 User with id 5 not found. diff --git a/tests/ApiProblem/Presenter/XmlPresenterTest.php b/tests/ApiProblem/Presenter/XmlPresenterTest.php index 924b978..a460a04 100644 --- a/tests/ApiProblem/Presenter/XmlPresenterTest.php +++ b/tests/ApiProblem/Presenter/XmlPresenterTest.php @@ -35,7 +35,7 @@ public function testItCanWriteXmlFromNestedArray() { $expected = << - + Input values do not match the requirements 500 User data is not valid. @@ -56,7 +56,7 @@ public function testItCanCastObjectToString() { $expected = << - + Input values do not match the requirements 500 User data is not valid.