<?php

declare(strict_types=1);

final class MyronJsonLdEncoder
{
    public function safeEncode(mixed $data): string|false
    {
        try {
            $encoded = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
        } catch (Throwable) {
            return false;
        }

        return preg_replace('#</\s*script\s*>#i', '<\\/script>', $encoded) ?? false;
    }
}
