<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Rules\Repository;

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

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