<?php
/**
 * PHPView class
 * Implements the IView interface
 * @author Jérôme Brilland
 * @version 1.0
 * @copyright (c) 2018, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */
class PHPView implements IView {
    
    /**
     *
     * @var array
     * @access protected 
     */
    protected $data = [];
    
    /**
     * 
     * @param string $directory
     * @access public
     */
    public function addTemplateDirectory( $directory ) {
        set_include_path( get_include_path() . PATH_SEPARATOR . $directory );
    }
    
    /**
     * 
     * @param string $name
     * @param mixed $value
     * @access public
     */
    public function assign( $name, $value ) {
        $this->data[$name] = $value;
    }
    
    /**
     * Render a template
     * @param string $template Template
     * @param array $data
     * @return string Rendered template
     */
    public function render( $template, $data = null ) {
        extract( array_merge( $this->data, $data ) );
        ob_start();
        require( $template );
        $str = ob_get_contents();
        ob_end_clean();
        return $str;
    }
    
}
