<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Template;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Implements the common methods of a basic tags collector.
 *
 * @since 1.10
 */
trait TagsTrait
{
    /**
     * An associative array holding the tag name-value pairs.
     * 
     * @var array
     */
    private $templateData = [];

    /**
     * @inheritDoc
     * 
     * @see Taggable
     */
    public function getTemplateData()
    {
        return $this->templateData;
    }

    /**
     * @inheritDoc
     * 
     * @see Taggable
     */
    public function addTemplateData(array $data)
    {
        $this->templateData = array_merge($this->templateData, $data);
    }

    /**
     * @inheritDoc
     * 
     * @see Taggable
     */
    public function injectTemplateData(string $tmpl)
    {
        foreach ($this->templateData as $tag => $value)
        {
            $tmpl = str_replace("{{$tag}}", $value, $tmpl);
        }

        return $tmpl;
    }

    /**
     * @inheritDoc
     * 
     * @see Taggable
     */
    public function attachTemplateDecorator(TagsDecorator $decorator)
    {
        // build the decorator
        $decorator->build($this);

        return $this;
    }
}
