<?php
namespace Core;

class Component
{
    protected string $id = '';
    protected string $class = '';
    protected string $text = '';
    protected array $attributes = [];

    public function id(string $id): static
    {
        $this->id = $id;
        return $this;
    }

    public function class(string $class): static
    {
        $this->class = $class;
        return $this;
    }

    public function text(string $text): static
    {
        $this->text = $text;
        return $this;
    }

    public function attr(string $key, string $value): static
    {
        $this->attributes[$key] = $value;
        return $this;
    }

    protected function attrs(): string
    {
        $attrs = [];

        if ($this->id) $attrs[] = "id=\"{$this->id}\"";
        if ($this->class) $attrs[] = "class=\"{$this->class}\"";

        foreach ($this->attributes as $key => $value) {
            $escaped = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
            $attrs[] = "$key=\"$escaped\"";
        }


        return $attrs ? ' ' . implode(' ', $attrs) : '';
    }
}
