Initial commit
This commit is contained in:
30
src/Encoder/JsonEncoder.php
Normal file
30
src/Encoder/JsonEncoder.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Encoder;
|
||||
|
||||
use Domain\Normalizer\Normalizer;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class JsonEncoder implements StringEncoder
|
||||
{
|
||||
public function __construct(
|
||||
private Normalizer $normalizer
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function encode(Collection $items): string
|
||||
{
|
||||
return json_encode(
|
||||
[
|
||||
'count' => $items->count(),
|
||||
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
|
||||
],
|
||||
JSON_UNESCAPED_SLASHES
|
||||
| JSON_UNESCAPED_UNICODE
|
||||
| JSON_UNESCAPED_LINE_TERMINATORS
|
||||
| JSON_BIGINT_AS_STRING
|
||||
| JSON_THROW_ON_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user