<?php

declare(strict_types=1);

namespace App\Framework\Serializer\Php;

/**
 * Configuration for PHP serializer
 *
 * This class encapsulates the configuration options for the PHP serializer.
 */
final readonly class PhpSerializerConfig
{
    /**
     * Constructor
     *
     * @param bool|array $allowedClasses Controls which classes are allowed to be unserialized.
     *                                   If false, no classes are allowed.
     *                                   If true, all classes are allowed.
     *                                   If array, only the specified classes are allowed.
     * @param int $maxDepth Maximum depth of nested objects to deserialize
     */
    public function __construct(
        public bool|array $allowedClasses = false,
        public int $maxDepth = 4096
    ) {
    }

    /**
     * Creates a config with safe deserialization (only specified classes allowed)
     *
     * @param array $allowedClasses List of class names that are allowed to be unserialized
     * @param int $maxDepth Maximum depth of nested objects to deserialize
     * @return self
     */
    public static function safe(array $allowedClasses = [], int $maxDepth = 4096): self
    {
        return new self($allowedClasses, $maxDepth);
    }

    /**
     * Creates a config with unsafe deserialization (all classes allowed)
     *
     * @param int $maxDepth Maximum depth of nested objects to deserialize
     * @return self
     */
    public static function unsafe(int $maxDepth = 4096): self
    {
        return new self(true, $maxDepth);
    }

    /**
     * Returns the options array for use with unserialize()
     *
     * @return array{allowed_classes: array|bool, max_depth: int} The options array
     */
    public function getOptions(): array
    {
        return [
            'allowed_classes' => $this->allowedClasses,
            'max_depth' => $this->maxDepth,
        ];
    }
}
