<?php
/**
 * An abstract class for form decorators.
 *
 * This class provides a way to create dynamic form decorators.
 */
class form_decorator_dynamic implements form_decorator
{
    /**
     * @var string The error decorator.
     */
    protected $error;
    
    /**
     * @var string The label decorator.
     */
    protected $label;
    
    /**
     * @var string The field decorator.
     */
    protected $field;
    
    /**
     * @var string The entry decorator.
     */
    protected $entry;
    
    /**
     * @var string The set decorator.
     */
    protected $set;

    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    /**
     * @param string (optional) $error The error decorator.
     * @param string (optional) $label The label decorator.
     * @param string (optional) $field The field decorator.
     * @param string (optional) $entry The entry decorator.
     * @param string (optional) $set The set decorator.
     */
    public function __construct($error = '{error}', $label = '{label}', $field = '{field}', $entry = '{error}{label}{field}', $set = '{set}')
    {
        $this->error = $error;
        $this->label = $label;
        $this->field = $field;
        $this->entry = $entry;
        $this->set = $set;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    /**
     * Decorates an error.
     *
     * @param string $error The error.
     * @param string (optional) $decorator The decorator.
     * @return string The decorated error.
     */
    public function render_error($error, $decorator = null)
    {
        return str_replace('{error}', $error, ($decorator !== null) ? $decorator : $this->error);
    }
    
    /**
     * Decorates a label.
     *
     * @param string $label The label.
     * @param string (optional) $decorator The decorator.
     * @return string The decorated label.
     */
    public function render_label($label, $decorator = null)
    {
        return str_replace('{label}', $label, ($decorator !== null) ? $decorator : $this->label);
    }
    
    /**
     * Decorates a field.
     *
     * @param string $field The field.
     * @param string (optional) $decorator The decorator.
     * @return string The decorated field.
     */
    public function render_field($field, $decorator = null)
    {
        return str_replace('{field}', $field, ($decorator !== null) ? $decorator : $this->field);
    }
    
    /**
     * Decorates an entry.
     *
     * @param string $error The error.
     * @param string $label The label.
     * @param array $field The field.
     * @param string (optional) $decorator The decorator.
     * @return string The decorated entry.
     */
    public function render_entry($error, $label, $field, $decorator = null)
    {
        return str_replace(array('{error}', '{label}', '{field}'), array($error, $label, $field), ($decorator !== null) ? $decorator : $this->entry);
    }
    
    /**
     * Decorates a set.
     *
     * @param string $set The set.
     * @param string (optional) $decorator The decorator.
     * @return string The decorated set.
     */
    public function render_set($set, $decorator = null)
    {
        return str_replace('{set}', $set, ($decorator !== null) ? $decorator : $this->set);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
