asrock-memory-qvl-scraper/src/Encoder/JsonEncoder.php

26 lines
642 B
PHP
Raw Normal View History

2021-04-22 19:26:09 +02:00
<?php
namespace Encoder;
use Domain\Normalizer\Normalizer;
use Illuminate\Support\Collection;
class JsonEncoder implements StringEncoder
{
2021-04-28 20:17:37 +02:00
private const FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS | JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR;
2021-04-22 19:26:09 +02:00
public function __construct(
private Normalizer $normalizer
)
{
}
public function encode(Collection $items): string
{
2021-04-28 20:17:37 +02:00
return json_encode([
'count' => $items->count(),
'items' => $items->map([$this->normalizer, 'normalize'])->values()->toArray(),
], self::FLAGS);
2021-04-22 19:26:09 +02:00
}
}