<?php

declare(strict_types=1);

namespace App\Framework\View;

use App\Framework\Core\AttributeMapper;
use App\Framework\Reflection\WrappedReflectionClass;
use App\Framework\Reflection\WrappedReflectionMethod;

final class TemplateMapper implements AttributeMapper
{
    public function getAttributeClass(): string
    {
        return Template::class;
    }

    public function map(WrappedReflectionClass|WrappedReflectionMethod $reflectionTarget, object $attributeInstance): ?array
    {
        if (! $attributeInstance instanceof Template) {
            return null;
        }

        // Template can be applied to classes or methods
        $className = $reflectionTarget instanceof WrappedReflectionClass
            ? $reflectionTarget->getFullyQualified()
            : $reflectionTarget->getDeclaringClass()->getFullyQualified();

        return [
            'class' => $className,
            'path' => $attributeInstance->path,
        ];
    }
}
