Skip to content

Commit d179da2

Browse files
author
Oscar Otero
committed
use all formatters by default
1 parent 4cab415 commit d179da2

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [3.0.0] - 2019-11-29
99
### Changed
1010
- Merged `JpegFormatter`, `GifFormatter` and `PngFormatter` in one `ImageFormatter`.
11+
- By default, all formatters are used.
1112

1213
### Removed
1314
- Support for PHP 7.0 and 7.1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $response = $dispatcher->dispatch($request);
5858

5959
## Usage
6060

61-
Add the [formatters](src/Formatter) to be used (instances of `Middlewares\ErrorFormatter\FormatterInterface`).
61+
Add the [formatters](src/Formatter) to be used (instances of `Middlewares\ErrorFormatter\FormatterInterface`). If no formatters are provided, use all available.
6262

6363
```php
6464
$errorHandler = new ErrorHandler([

src/ErrorHandler.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Middlewares;
55

66
use Middlewares\ErrorFormatter\FormatterInterface;
7-
use Middlewares\ErrorFormatter\PlainFormatter;
87
use Psr\Http\Message\ResponseInterface;
98
use Psr\Http\Message\ServerRequestInterface;
109
use Psr\Http\Server\MiddlewareInterface;
@@ -21,8 +20,19 @@ class ErrorHandler implements MiddlewareInterface
2120
*
2221
* @param FormatterInterface[] $formatters
2322
*/
24-
public function __construct(array $formatters = [])
23+
public function __construct(array $formatters = null)
2524
{
25+
if (empty($formatters)) {
26+
$formatters = [
27+
new ErrorFormatter\PlainFormatter(),
28+
new ErrorFormatter\HtmlFormatter(),
29+
new ErrorFormatter\ImageFormatter(),
30+
new ErrorFormatter\JsonFormatter(),
31+
new ErrorFormatter\SvgFormatter(),
32+
new ErrorFormatter\XmlFormatter(),
33+
];
34+
}
35+
2636
$this->addFormatters(...$formatters);
2737
}
2838

@@ -49,7 +59,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4959
}
5060
}
5161

52-
$default = current($this->formatters);
62+
$default = reset($this->formatters);
5363

5464
return $default->handle($error, $request);
5565
}

0 commit comments

Comments
 (0)