<?php
/**
 * A class for replacing all occurrences in a string with another.
 */
class filter_replace implements filter
{
    /**
     * @var string/array The searched occurrence.
     */
    protected $search;
    
    /**
     * @var string/array The replacement.
     */
    protected $replace;
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    /**
     * @param string/array $search The searched occurrence.
     * @param string/array (optional) $replace The replacement.
     */
    public function __construct($search, $replace = '')
    {
        $this->search = $search;
        $this->replace = $replace;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Filters a value.
     * 
     * @param string $value The value.
     * @param string/array (optional) $search The search occurrence.
     * @param string/array (optional) $replace The replacement.
     * @return string The filtered value.
     */
    public function filter($value, $search = null, $replace = null)
    {
        if ($search === null)
        {
            $search = $this->search;
        }
        
        if ($replace === null)
        {
            $replace = $this->replace;
        }
        
        return str_replace($search, $replace, $value);
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
