<?php

require_once 'common/bean/BeanDynamic.php';
require_once 'orm/lazy/OrmLazyAttribute.php';

/**
 * Lazy Bean.
 * 
 * This class encapsulates a <em>BeanDynamic</em>, providing lazy 
 * initialization capabilities to its attributes.
 * 
 * @author  $Author: pruna $
 * @package orm/vo
 * @version $Revision: 1.3 $
 * @created 18-Oct-2005
 * @modified $Date: 2005/12/06 02:56:45 $
 */
class OrmLazyBean extends BeanDynamic {
    
    /**
     * Returns the element to which the specified key is mapped in this 
     * identity hash map, or null if the map contains no mapping for this key.
     * If element is a OrmLazyAttribute, it's waked up before returning.
     * 
     * @access  public
     * @param   mixed     $key     Value to look for and return
     * @return  mixed
     */
    public function get($key) {
        $element = parent::get($key);
        if (ObjectInspector::isA($element, 'OrmLazyAttribute')) {
            $this->elements[$key] = $element->wakeUp();
        }
        return parent::get($key);
    }
    
    /**
     * Checks if an attribute is a OrmLazyAttribute.
     *
     * @access  default
     * @param   mixed     $key     Value to see if is a OrmLazyAttribute
     * @return  boolean
     */
    function stillLazy($key) {
        return ObjectInspector::isA(parent::get($key), 'OrmLazyAttribute');
    }
    
    /**
     * Activate all OrmLazyAttribute.
     *
     * @access  default
     * @return  void
     */
    function wakeUpAll() {
        $it = $this->getIterator();
        while ($it->hasMore()) {
            $it->next();
            $this->get($it->key());
        }
    }
    
}

?>