| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Tree.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | /** |
| 22 | * Doctrine_Tree |
| 23 | * |
| 24 | * @package Doctrine |
| 25 | * @subpackage Tree |
| 26 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 27 | * @link www.phpdoctrine.org |
| 28 | * @since 1.0 |
| 29 | * @version $Revision: 2963 $ |
| 30 | * @author Joe Simms <joe.simms@websites4.com> |
| 31 | */ |
| 32 | class Doctrine_Tree |
| 33 | { |
| 34 | /** |
| 35 | * @param object $table reference to associated Doctrine_Table instance |
| 36 | */ |
| 37 | protected $table; |
| 38 | |
| 39 | /** |
| 40 | * @param array $options |
| 41 | */ |
| 42 | protected $options = array(); |
| 43 | |
| 44 | protected $_baseComponent; |
| 45 | |
| 46 | /** |
| 47 | * constructor, creates tree with reference to table and any options |
| 48 | * |
| 49 | * @param object $table instance of Doctrine_Table |
| 50 | * @param array $options options |
| 51 | */ |
| 52 | public function __construct(Doctrine_Table $table, $options) |
| 53 | { |
| 54 | $this->table = $table; |
| 55 | $this->options = $options; |
| 56 | $this->_baseComponent = $table->getComponentName(); |
| 57 | $class = $this->_baseComponent; |
| 58 | if ($table->getOption('inheritanceMap')) { |
| 59 | $subclasses = $table->getOption('subclasses'); |
| 60 | while (in_array($class, $subclasses)) { |
| 61 | $class = get_parent_class($class); |
| 62 | } |
| 63 | $this->_baseComponent = $class; |
| 64 | } |
| 65 | //echo $this->_baseComponent; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Used to define table attributes required for the given implementation |
| 70 | * |
| 71 | * @throws Doctrine_Tree_Exception if table attributes have not been defined |
| 72 | */ |
| 73 | public function setTableDefinition() |
| 74 | { |
| 75 | throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.'); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * this method is used for setting up relations and attributes and should be used by specific implementations |
| 80 | * |
| 81 | */ |
| 82 | public function setUp() |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * factory method to return tree instance based upon chosen implementation |
| 88 | * |
| 89 | * @param object $table instance of Doctrine_Table |
| 90 | * @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath) |
| 91 | * @param array $options options |
| 92 | * @return object $options instance of Doctrine_Node |
| 93 | * @throws Doctrine_Exception if class does not extend Doctrine_Tree |
| 94 | */ |
| 95 | public static function factory(Doctrine_Table $table, $implName, $options = array()) |
| 96 | { |
| 97 | $class = 'Doctrine_Tree_' . $implName; |
| 98 | if ( ! class_exists($class)) { |
| 99 | throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree'); |
| 100 | } |
| 101 | return new $class($table, $options); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * gets tree attribute value |
| 106 | * |
| 107 | */ |
| 108 | public function getAttribute($name) |
| 109 | { |
| 110 | return isset($this->options[$name]) ? $this->options[$name] : null; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * sets tree attribute value |
| 115 | * |
| 116 | * @param mixed |
| 117 | */ |
| 118 | public function setAttribute($name, $value) |
| 119 | { |
| 120 | $this->options[$name] = $value; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the base tree component. |
| 125 | */ |
| 126 | public function getBaseComponent() |
| 127 | { |
| 128 | return $this->_baseComponent; |
| 129 | } |
| 130 | } |