<?php
/**
 * @package dsn
 */

require_once 'common/bean/Bean.php';

/**
 * Chain of responsibility for DSN's IHandlers.
 * 
 * Manage a list of handlers for <em>DSN</em> objects, and look for the
 * proper one following the chain of responsibility pattern.
 * 
 * @author  $Author: pruna $
 * @package dsn
 * @version $Revision: 1.1 $
 * @created 05-May-2006
 * @modified $Date: 2005/12/06 02:56:45 $
 */
class DsnHandler extends Bean {
    
    const HANDLERS = 'handlers';
    
    /**
     * Constructor. Creates default attributes for collection.
     *
     * @access    public
     * @param     ICollection    $collection    Initial elements
     */
    public function __construct($collection = null) {
        parent::__construct(array(
            self::HANDLERS => array(),
        ));
        $this->setAll($collection);
    }
    
    /**
     * Runs through chain looking for proper handler. Note that following 
     * chain of responsibility pattern, this method returns the <em>first</em> 
     * Handler that could handle the DSN, and not the <em>best</em> one.
     * Param <em>dsn</em> could be DSN object or string.
     *
     * @access    public
     * @param     mixed     $dsn   DSN that should get a handler for
     * @return    IHandler
     */
    public function getHandlerFor($dsn) {
        if (is_string($dsn)) {
            $dsn = new Dsn($dsn);
        }
        foreach ($this->getHandlers() as $handler) {
            if ($handler->canHandle($dsn)) {
                return $handler;
            }
        }
        return null;
    }
    
}

?>
