31 lines
696 B
PHP
31 lines
696 B
PHP
<?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
|
|
);
|
|
}
|
|
}
|