-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/change-formatter-interface'
- Loading branch information
Showing
17 changed files
with
183 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ php: | |
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
before_script: | ||
- composer install -n | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Middlewares\ErrorFormatter; | ||
|
||
use Middlewares\Utils\Factory; | ||
use Middlewares\Utils\HttpErrorException; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Throwable; | ||
|
||
abstract class AbstractFormatter implements FormatterInterface | ||
{ | ||
/** @var ResponseFactoryInterface */ | ||
protected $responseFactory; | ||
|
||
/** @var StreamFactoryInterface */ | ||
protected $streamFactory; | ||
|
||
/** @var string[] */ | ||
protected $contentTypes = []; | ||
|
||
public function __construct( | ||
ResponseFactoryInterface $responseFactory = null, | ||
StreamFactoryInterface $streamFactory = null | ||
) { | ||
$this->responseFactory = $responseFactory ?? Factory::getResponseFactory(); | ||
$this->streamFactory = $streamFactory ?? Factory::getStreamFactory(); | ||
} | ||
|
||
public function isValid(Throwable $error, ServerRequestInterface $request): bool | ||
{ | ||
return $this->getContentType($request) ? true : false; | ||
} | ||
|
||
abstract protected function format(Throwable $error): string; | ||
|
||
public function handle(Throwable $error, ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$response = $this->responseFactory->createResponse($this->errorStatus($error)); | ||
$body = $this->streamFactory->createStream($this->format($error)); | ||
$response = $response->withBody($body); | ||
|
||
$contentType = $this->getContentType($request); | ||
|
||
return $response->withHeader('Content-Type', $contentType ? $contentType : $this->contentTypes[0]); | ||
} | ||
|
||
protected function errorStatus(Throwable $error): int | ||
{ | ||
if ($error instanceof HttpErrorException) { | ||
return $error->getCode(); | ||
} | ||
|
||
if (method_exists($error, 'getStatusCode')) { | ||
return $error->getStatusCode(); | ||
} | ||
|
||
return 500; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
protected function getContentType(ServerRequestInterface $request) | ||
{ | ||
$accept = $request->getHeaderLine('Accept'); | ||
|
||
foreach ($this->contentTypes as $type) { | ||
if (stripos($accept, $type) !== false) { | ||
return $type; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
declare(strict_types = 1); | ||
|
||
namespace Middlewares\ErrorFormatter; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Throwable; | ||
|
||
interface FormatterInterface | ||
{ | ||
/** | ||
* Get supported content types | ||
* | ||
* @return string[] | ||
* Check whether the error can be handled by this formatter | ||
*/ | ||
public function contentTypes(): array; | ||
public function isValid(Throwable $error, ServerRequestInterface $request): bool; | ||
|
||
/** | ||
* Format an error as a string | ||
* Create a response with this error | ||
*/ | ||
public function format(Throwable $error): string; | ||
public function handle(Throwable $error, ServerRequestInterface $request): ResponseInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
declare(strict_types = 1); | ||
|
||
namespace Middlewares\ErrorFormatter; | ||
|
||
use Throwable; | ||
|
||
class GifFormatter extends AbstractImageFormatter | ||
{ | ||
public function contentTypes(): array | ||
{ | ||
return [ | ||
'image/gif', | ||
]; | ||
} | ||
protected $contentTypes = [ | ||
'image/gif', | ||
]; | ||
|
||
public function format(Throwable $error): string | ||
protected function format(Throwable $error): string | ||
{ | ||
ob_start(); | ||
imagegif($this->createImage($error)); | ||
return ob_get_clean(); | ||
return (string) ob_get_clean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
declare(strict_types = 1); | ||
|
||
namespace Middlewares\ErrorFormatter; | ||
|
||
use Throwable; | ||
|
||
class JpegFormatter extends AbstractImageFormatter | ||
{ | ||
public function contentTypes(): array | ||
{ | ||
return [ | ||
'image/jpeg', | ||
]; | ||
} | ||
protected $contentTypes = [ | ||
'image/jpeg', | ||
]; | ||
|
||
public function format(Throwable $error): string | ||
protected function format(Throwable $error): string | ||
{ | ||
ob_start(); | ||
imagejpeg($this->createImage($error)); | ||
return ob_get_clean(); | ||
return (string) ob_get_clean(); | ||
} | ||
} |
Oops, something went wrong.