<?php
namespace nur\v\base;

use nur\v\model\IPageContainer;
use nur\v\model\IPlugin;
use Throwable;

/**
 * Trait TComponent: implémentation réutilisable de IComponent
 */
trait TComponent {
  #############################################################################
  # IChildComponent

  /** @var IPageContainer */
  protected $container;

  function initContainer(IPageContainer $container): void {
    $this->container = $container;
  }

  function addPlugin($plugin, ?string $name=null): IPlugin {
    return $this->container->addPlugin($plugin, $name);
  }

  function haveError(): bool {
    return $this->container->haveError();
  }

  function setError(?string $message, ?Throwable $exception=null): void {
    $this->container->setError($message, $exception);
  }

  function getError(): ?array {
    return $this->container->getError();
  }

  #############################################################################
  # IComponent

  protected $prepareDone = false;
  function beforePrepare(): void {}
  function prepare(): void {}
  function afterPrepare(): void { $this->prepareDone = true; }
  function didPrepare(): bool { return $this->prepareDone; }

  protected $syncConfigDone = false;
  function beforeConfig(array &$config): void {}
  function config(array &$config): void {}
  function afterConfig(): void { $this->syncConfigDone = true; }
  function didConfig(): bool { return $this->syncConfigDone; }
  function configGet(string $name) {
    return $this->container->getConfig()[$name];
  }

  protected $setupDone = false;
  function beforeSetup(): void {}
  function setup() {}
  function afterSetup(): void { $this->setupDone = true; }
  function didSetup(): bool { return $this->setupDone; }

  function haveContent(): bool { return true; }

  protected $teardownDone = false;
  function beforeTeardown(): void {}
  function teardown(): void {}
  function afterTeardown(): void { $this->teardownDone = true; }
  function didTeardown(): bool { return $this->teardownDone; }
}
