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

26 lines
642 B
PHP

<?php
namespace Encoder;
use Domain\Normalizer\Normalizer;
use Illuminate\Support\Collection;
class JsonEncoder implements StringEncoder
{
private const FLAGS = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS | JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR;
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(),
], self::FLAGS);
}
}