<?php
namespace nur\sery\wip\web\content;

use nur\sery\A;
use nur\sery\php\content\c;
use nur\sery\php\content\IContent;

class Tag implements IContent {
  function __construct(string $tag, $content=null) {
    $this->tag = $tag;
    $content = c::q($content);
    $this->content = $content;
  }

  protected string $tag;
  protected iterable $content;

  function add($content): self {
    if (!is_array($this->content)) {
      # si c'est un itérable, l'inclure avec un merge statique, afin de pouvoir
      # rajouter des éléments
      $this->content = [[[], $this->content]];
    }
    A::merge($this->content, c::q($content));
    return $this;
  }

  function getContent($object_or_class=null): iterable {
    return [
      "<$this->tag>",
      ...c::resolve($this->content, $object_or_class),
      "</$this->tag>",
    ];
  }
}
