<?php

namespace Mtc\Cms\Support;

/**
 * Server-side guard against corrupt pagedata values reaching the database.
 *
 * The content manager saves pages by serialising the admin panel DOM in the
 * browser (see gatherPagedataValuesFromPanel in content_manager/js/functions.js).
 * If the real value input for an image/file field is not found, a loose
 * fallback selector can pick up the hidden pagedata[ID][type] input instead,
 * submitting the field's own type name ("image"/"file") as its value and
 * wiping the stored filename. This happened to every image on the live
 * homepage on 16 July 2026.
 *
 * Stored image/file values are always either empty (cleared) or a
 * timestamp-prefixed filename produced by uploadifive.php, so a value exactly
 * equal to the field type can never be legitimate.
 */
class PagedataGuard
{
    private const FILE_TYPES = ['image', 'file'];

    /**
     * True when a submitted value for the given field type is the
     * serialisation artefact described above and must not be persisted.
     */
    public static function isCorruptFileValue(?string $type, mixed $value): bool
    {
        return in_array($type, self::FILE_TYPES, true) && $value === $type;
    }
}
