<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Projection;

final class ProjectionSerializer
{
    public function encode(ProjectionRuntimeSnapshot $snapshot): string
    {
        return json_encode($snapshot->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ?: '{}';
    }

    public function decode(string $json): ProjectionRuntimeSnapshot
    {
        $data = json_decode($json, true);
        return ProjectionRuntimeSnapshot::fromArray(is_array($data) ? $data : []);
    }
}
