<?php

namespace Sitecake\Log\Formatter;

/**
 * Callable formatter that replaces filesystem paths in error output.
 *
 * Rewritten to remove the Monolog\Formatter\HtmlFormatter dependency.
 * Now a plain callable (invokable class): format(array $error): string
 */
class PathReplaceFormatter
{
    /** @var array<string, string> search => replacement */
    protected array $replacements;

    public function __construct(array $replacements = [])
    {
        $this->replacements = $replacements;
    }

    /**
     * Format an error data array into a human-readable string with paths replaced.
     *
     * @param array $error  Keys: error, code, description, file, line, …
     * @return string
     */
    public function __invoke(array $error): string
    {
        $text = sprintf(
            '[%s] %s (%s): %s in %s on line %s',
            date('Y-m-d H:i:s'),
            $error['error']       ?? 'Error',
            $error['code']        ?? 0,
            $error['description'] ?? '',
            $error['file']        ?? '',
            $error['line']        ?? ''
        );

        return $this->replace($text);
    }

    protected function replace(string $content): string
    {
        foreach ($this->replacements as $search => $replacement) {
            $content = str_replace($search, $replacement, $content);
        }
        return $content;
    }
}
