<?php

abstract class BasevzUIWidget extends sfWidget implements ArrayAccess
{
  protected
    $parameters   = array(),
    $slots_stack  = array(),
    $slots        = array();
    
    
	public function __construct()
	{
	  $this->parameters = func_get_args();
		parent::__construct();
	}
  
  protected function configure($options = array(), $attributes = array())
  {
    $this->setup();
    call_user_func_array(array($this, 'construct'), $this->parameters);
  }
	
	protected function getRenderer()
	{
		return sfContext::getInstance()->getUir();
	}
	
	abstract public function getHtml();
  abstract protected function setup();
	
	public function __toString()
	{
		$this->attachAssets();
    return $this->getHtml();
	}
  
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    
  }
	
	protected function attachAssets()
	{
    $response = sfContext::getInstance()->getResponse();
		foreach ($this->getStylesheets() as $css) {
			$response->addStylesheet($css);			
		}
		foreach ($this->getJavascripts() as $js) {
			$response->addJavascript($js);			
		}
	}
  
  #bof parameters
  public function setParameters(array $parameters)
  {
    $this->parameters = $parameters;
    return $this; 
  }
  
  public function getParameters()
  {
    return $this->parameters;  
  }
  
  public function setParameter($key, $value)
  {
    $this->parameters[$key] = $value;
    return $this;
  }
  
  public function getParameter($key, $default = null)
  {
    if ($this->hasParameter($key)) {
      return $this->parameters[$key];    
    }
    return $default;
  }
  
  public function hasParameter($key)
  {
    return isset($this->parameters[$key]); 
  }
  
  public function removeParameter($key)
  {
    unset($this->parameters[$key]);
  }
  #eof parameters
  
  #bof array access
  public function offsetExists($key)
  {
    return $this->hasParameter($key);
  }

  public function offsetGet($key)
  {
    return $this->getParameter($key);
  }

  public function offsetSet($key, $value)
  {
    $this->setParameter($key, $value);
  }

  public function offsetUnset($key)
  {
    $this->removeParameter($key);
  }
  #eof array access
	
	#options
	
	public function addOptions(array $options)
	{
		$this->options = array_merge($this->options, $options);
	}
  
  public function setupOptions(array $options = array())
  {
    foreach ($options as $key => $value)
    {
      $this->setOption($key, $value);
    }
    
    return $this;
  }
	
	public function removeOption($name)
	{
		if ($this->hasOption($name)) {
			unset($this->options[$name]);
		}
	}
  
  public function addRequiredOptions($requiredOptions)
  {
    $this->requiredOptions = array_merge($this->requiredOptions, $requiredOptions);
  }
  
  protected function extractOptionsFrom(array &$array, array $options)
  {
    foreach ($options as $key)
    {
      if (isset($array[$key])) {
        $this->setOption($key, $array[$key]);
      }
      
      unset($array[$key]);
    }
    
    return $array;
  }
  
  /**
   * bof slots management
   */
  public function startCapture($name) #sau ob_start cu ob_stop
  {
    if (isset($this->slots[$name]))
    {
      throw new sfException(sprintf('Capture already started for %s slot', $name));
    }
    
    $this->slots_stack[]  = $name;
    $this->slots[$name]   = '';
    
    ob_start();
    ob_implicit_flush(0);
  }
  
  public function stopCapture()
  {
    if (!count($this->slots_stack))
    {
      throw new sfException('No slot started.');
    }
    
    $content = ob_get_clean();
    
    $this->slots[array_pop($this->slots_stack)] = $content;
    
    return $content;
  }
  
  public function getSlot($name)
  {
    if (isset($this->slots[$name]))
    {
      return $this->slots[$name];
    }
  }
  
  public function getCurrentSlot()
  {
    if ($nr = count($this->slots_stack))
    {
      return $this->slots_stack[$nr - 1];
    }
  }
  
  public function setSlot($name, $value)
  {
    $this->slots[$name] = $value;
  }
  
  public function hasSlot($name)
  {
    return isset($this->slots[$name]);
  }
  
  public function getSlots()
  {
    return $this->slots;
  }
  /**
   * eof slots management
   */
  
}

