<?php


namespace AutoAdmin\Bundle\Seo\Schema\Model;


use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Graph;
use function count;

class Schema
{
    protected array $representations = [];

    public function getRepresentations(): array
    {
        return $this->representations;
    }

    public function setRepresentations(array $representations): void
    {
        $this->representations = $representations;
    }

    public function hasRepresentations(): bool
    {
        return count($this->representations) > 0;
    }

    public function addRepresentation(BaseType $schema): self
    {
        $type = get_class($schema);
        $this->representations[$type] = $schema;

        return $this;
    }

    public function hasType(string $type): bool
    {
        return isset($this->representations[$type]);
    }

    public function getType(string $type): ?array
    {
        return $this->representations[$type] ?? null;
    }

    public function toScript(): string
    {
        $graph = new Graph();

        /** @var BaseType $representation */
        foreach ($this->representations as $representation) {
            $graph->add($representation);
        }

        return $graph->toScript();
    }
}