Tree.php 4.18 KB
Newer Older
zYne's avatar
zYne committed
1 2
<?php
/*
romanb's avatar
romanb committed
3
 *  $Id: Tree.php 4699 2008-07-20 20:13:24Z romanb $
zYne's avatar
zYne committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
zYne's avatar
zYne committed
20 21 22 23 24
 */                   
/**
 * Doctrine_Tree
 *
 * @package     Doctrine
25
 * @subpackage  Tree
zYne's avatar
zYne committed
26
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
27
 * @link        www.phpdoctrine.org
zYne's avatar
zYne committed
28
 * @since       1.0
romanb's avatar
romanb committed
29
 * @version     $Revision: 4699 $
zYne's avatar
zYne committed
30
 * @author      Joe Simms <joe.simms@websites4.com>
31
 * @todo Move to NestedSet behavior.
zYne's avatar
zYne committed
32 33 34 35 36 37 38 39 40 41 42 43
 */
class Doctrine_Tree
{
    /**
     * @param object $table   reference to associated Doctrine_Table instance
     */
    protected $table;

    /**
     * @param array $options
     */
    protected $options = array();
44 45
    
    protected $_baseComponent;
zYne's avatar
zYne committed
46 47 48 49 50 51 52

    /**
     * constructor, creates tree with reference to table and any options
     *
     * @param object $table                     instance of Doctrine_Table
     * @param array $options                    options
     */
53
    public function __construct($table, $options)
zYne's avatar
zYne committed
54 55 56
    {
        $this->table = $table;
        $this->options = $options;
57 58 59
        $this->_baseComponent = $table->getComponentName();
        $class = $this->_baseComponent;
        if ($table->getOption('inheritanceMap')) {
60
            $subclasses = $table->getSubclasses();
61 62 63 64 65 66
            while (in_array($class, $subclasses)) {
                $class = get_parent_class($class);
            }
            $this->_baseComponent = $class;
        }
        //echo $this->_baseComponent;
zYne's avatar
zYne committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    /**
     * Used to define table attributes required for the given implementation
     *
     * @throws Doctrine_Tree_Exception          if table attributes have not been defined
     */
    public function setTableDefinition()
    {
        throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
    }

    /**
     * this method is used for setting up relations and attributes and should be used by specific implementations
     *
     */
    public function setUp()
    {
    }

    /**
     * factory method to return tree instance based upon chosen implementation
     *
     * @param object $table                     instance of Doctrine_Table
     * @param string $impName                   implementation (NestedSet, AdjacencyList, MaterializedPath)
     * @param array $options                    options
     * @return object $options                  instance of Doctrine_Node
     * @throws Doctrine_Exception               if class does not extend Doctrine_Tree
     */
96
    public static function factory($table, $implName, $options = array())
zYne's avatar
zYne committed
97 98
    {
        $class = 'Doctrine_Tree_' . $implName;
99
        if ( ! class_exists($class)) {
zYne's avatar
zYne committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
        }
        return new $class($table, $options);
    }

    /**
     * gets tree attribute value
     *        
     */     
    public function getAttribute($name)
    {
      return isset($this->options[$name]) ? $this->options[$name] : null;
    }

    /**
     * sets tree attribute value
     *
     * @param mixed            
     */
    public function setAttribute($name, $value)
    {
      $this->options[$name] = $value;
    }
123

124 125 126 127 128 129 130
    /**
     * Returns the base tree component.
     */
    public function getBaseComponent()
    {
        return $this->_baseComponent;
    }
131
}