<?php
namespace nur\data\template;

use nur\base;
use nur\data\expr\GenericExpr;
use nur\data\expr\IContext;

trait TTemplate {
  /** @var IContext */
  protected $context;

  function getContext(): IContext {
    return $this->context;
  }

  function setContext(IContext $context): void {
    $this->context = $context;
  }

  function applyRules(string $text, IContext $context) {
    foreach ($this->EXPRS() as $name => $expr) {
      $value = GenericExpr::with($expr, $name)->eval($context);
      if (!base::is_undef($value)) $text = str_replace($name, $value, $text);
    }
    #XXX ajouter le support des expressions conditionnelles. traiter ligne par
    # ligne s'il y a des expressions conditionnelles
    return $text;
  }
}
