Commit 2933cd2d authored by david's avatar david

updated NestedSet to use Zend coding standards. still a few problems with...

updated NestedSet to use Zend coding standards.  still a few problems with return values using () that need to be looked at.
parent 6786ce0c
...@@ -31,119 +31,127 @@ ...@@ -31,119 +31,127 @@
*/ */
class Doctrine_Node implements IteratorAggregate class Doctrine_Node implements IteratorAggregate
{ {
/** /**
* @param object $record reference to associated Doctrine_Record instance * @param object $record reference to associated Doctrine_Record instance
*/ */
protected $record; protected $record;
/** /**
* @param array $options * @param array $options
*/ */
protected $options; protected $options;
/** /**
* @param string $iteratorType (Pre | Post | Level) * @param string $iteratorType (Pre | Post | Level)
*/ */
protected $iteratorType; protected $iteratorType;
/** /**
* @param array $iteratorOptions * @param array $iteratorOptions
*/ */
protected $iteratorOptions; protected $iteratorOptions;
/** /**
* contructor, creates node with reference to record and any options * contructor, creates node with reference to record and any options
* *
* @param object $record instance of Doctrine_Record * @param object $record instance of Doctrine_Record
* @param array $options options * @param array $options options
*/ */
public function __construct(&$record, $options) public function __construct(&$record, $options)
{ {
$this->record = $record; $this->record = $record;
$this->options = $options; $this->options = $options;
} }
/** /**
* factory method to return node instance based upon chosen implementation * factory method to return node instance based upon chosen implementation
* *
* @param object $record instance of Doctrine_Record * @param object $record instance of Doctrine_Record
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath) * @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
* @param array $options options * @param array $options options
* @return object $options instance of Doctrine_Node * @return object $options instance of Doctrine_Node
*/ */
public static function factory(&$record, $implName, $options = array()) { public static function factory(&$record, $implName, $options = array())
{
$class = 'Doctrine_Node_'.$implName; $class = 'Doctrine_Node_' . $implName;
if(!class_exists($class)) if (!class_exists($class)) {
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node"); throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
}
return new $class($record, $options);
} return new $class($record, $options);
}
/**
* setter for record attribute /**
* * setter for record attribute
* @param object $record instance of Doctrine_Record *
*/ * @param object $record instance of Doctrine_Record
public function setRecord(&$record) */
{ public function setRecord(&$record)
$this->record = $record; {
} $this->record = $record;
}
/**
* getter for record attribute /**
* * getter for record attribute
* @return object instance of Doctrine_Record *
*/ * @return object instance of Doctrine_Record
public function getRecord() { */
return $this->record; public function getRecord()
} {
return $this->record;
/** }
* convenience function for getIterator
* /**
* @param string $type type of iterator (Pre | Post | Level) * convenience function for getIterator
* @param array $options options *
*/ * @param string $type type of iterator (Pre | Post | Level)
public function traverse($type = 'Pre', $options = array()) { * @param array $options options
return $this->getIterator($type, $options); */
} public function traverse($type = 'Pre', $options = array())
{
/** return $this->getIterator($type, $options);
* get iterator }
*
* @param string $type type of iterator (Pre | Post | Level) /**
* @param array $options options * get iterator
*/ *
public function getIterator($type = null, $options = null) * @param string $type type of iterator (Pre | Post | Level)
{ * @param array $options options
if ($type === null) */
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre'); public function getIterator($type = null, $options = null)
{
if ($options === null) if ($type === null) {
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array()); $type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
}
$iteratorClass = 'Doctrine_Node_'.$this->record->getTable()->getTreeImplName().'_'.ucfirst(strtolower($type)).'OrderIterator';
if ($options === null) {
return new $iteratorClass($this->record, $options); $options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
} }
/** $implName = $this->record->getTable()->getTreeImplName();
* sets node's iterator type $iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
*
* @param int return new $iteratorClass($this->record, $options);
*/ }
public function setIteratorType($type) {
$this->iteratorType = $type; /**
} * sets node's iterator type
*
/** * @param int
* sets node's iterator options */
* public function setIteratorType($type)
* @param int {
*/ $this->iteratorType = $type;
public function setIteratorOptions($options) { }
$this->iteratorOptions = $options;
} /**
} // END class * sets node's iterator options
\ No newline at end of file *
* @param int
*/
public function setIteratorOptions($options)
{
$this->iteratorOptions = $options;
}
}
...@@ -29,5 +29,6 @@ ...@@ -29,5 +29,6 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface { } class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface
{}
\ No newline at end of file
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator {} class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator
{}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator {} class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator
{}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator {} class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator
{}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_Exception extends Doctrine_Exception { } class Doctrine_Node_Exception extends Doctrine_Exception
{}
...@@ -31,237 +31,237 @@ ...@@ -31,237 +31,237 @@
*/ */
interface Doctrine_Node_Interface { interface Doctrine_Node_Interface {
/** /**
* test if node has previous sibling * test if node has previous sibling
* *
* @return bool * @return bool
*/ */
public function hasPrevSibling(); public function hasPrevSibling();
/** /**
* test if node has next sibling * test if node has next sibling
* *
* @return bool * @return bool
*/ */
public function hasNextSibling(); public function hasNextSibling();
/** /**
* test if node has children * test if node has children
* *
* @return bool * @return bool
*/ */
public function hasChildren(); public function hasChildren();
/** /**
* test if node has parent * test if node has parent
* *
* @return bool * @return bool
*/ */
public function hasParent(); public function hasParent();
/** /**
* gets record of prev sibling or empty record * gets record of prev sibling or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getPrevSibling(); public function getPrevSibling();
/** /**
* gets record of next sibling or empty record * gets record of next sibling or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getNextSibling(); public function getNextSibling();
/** /**
* gets siblings for node * gets siblings for node
* *
* @return array array of sibling Doctrine_Record objects * @return array array of sibling Doctrine_Record objects
*/ */
public function getSiblings($includeNode = false); public function getSiblings($includeNode = false);
/** /**
* gets record of first child or empty record * gets record of first child or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getFirstChild(); public function getFirstChild();
/** /**
* gets record of last child or empty record * gets record of last child or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getLastChild(); public function getLastChild();
/** /**
* gets children for node (direct descendants only) * gets children for node (direct descendants only)
* *
* @return array array of sibling Doctrine_Record objects * @return array array of sibling Doctrine_Record objects
*/ */
public function getChildren(); public function getChildren();
/** /**
* gets descendants for node (direct descendants only) * gets descendants for node (direct descendants only)
* *
* @return iterator iterator to traverse descendants from node * @return iterator iterator to traverse descendants from node
*/ */
public function getDescendants(); public function getDescendants();
/** /**
* gets record of parent or empty record * gets record of parent or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getParent(); public function getParent();
/** /**
* gets ancestors for node * gets ancestors for node
* *
* @return object Doctrine_Collection * @return object Doctrine_Collection
*/ */
public function getAncestors(); public function getAncestors();
/** /**
* gets path to node from root, uses record::toString() method to get node names * gets path to node from root, uses record::toString() method to get node names
* *
* @param string $seperator path seperator * @param string $seperator path seperator
* @param bool $includeNode whether or not to include node at end of path * @param bool $includeNode whether or not to include node at end of path
* @return string string representation of path * @return string string representation of path
*/ */
public function getPath($seperator = ' > ', $includeNode = false); public function getPath($seperator = ' > ', $includeNode = false);
/** /**
* gets level (depth) of node in the tree * gets level (depth) of node in the tree
* *
* @return int * @return int
*/ */
public function getLevel(); public function getLevel();
/** /**
* gets number of children (direct descendants) * gets number of children (direct descendants)
* *
* @return int * @return int
*/ */
public function getNumberChildren(); public function getNumberChildren();
/** /**
* gets number of descendants (children and their children) * gets number of descendants (children and their children)
* *
* @return int * @return int
*/ */
public function getNumberDescendants(); public function getNumberDescendants();
/** /**
* inserts node as parent of dest record * inserts node as parent of dest record
* *
* @return bool * @return bool
*/ */
public function insertAsParentOf(Doctrine_Record $dest); public function insertAsParentOf(Doctrine_Record $dest);
/** /**
* inserts node as previous sibling of dest record * inserts node as previous sibling of dest record
* *
* @return bool * @return bool
*/ */
public function insertAsPrevSiblingOf(Doctrine_Record $dest); public function insertAsPrevSiblingOf(Doctrine_Record $dest);
/** /**
* inserts node as next sibling of dest record * inserts node as next sibling of dest record
* *
* @return bool * @return bool
*/ */
public function insertAsNextSiblingOf(Doctrine_Record $dest); public function insertAsNextSiblingOf(Doctrine_Record $dest);
/** /**
* inserts node as first child of dest record * inserts node as first child of dest record
* *
* @return bool * @return bool
*/ */
public function insertAsFirstChildOf(Doctrine_Record $dest); public function insertAsFirstChildOf(Doctrine_Record $dest);
/** /**
* inserts node as first child of dest record * inserts node as first child of dest record
* *
* @return bool * @return bool
*/ */
public function insertAsLastChildOf(Doctrine_Record $dest); public function insertAsLastChildOf(Doctrine_Record $dest);
/** /**
* moves node as prev sibling of dest record * moves node as prev sibling of dest record
* *
*/ */
public function moveAsPrevSiblingOf(Doctrine_Record $dest); public function moveAsPrevSiblingOf(Doctrine_Record $dest);
/** /**
* moves node as next sibling of dest record * moves node as next sibling of dest record
* *
*/ */
public function moveAsNextSiblingOf(Doctrine_Record $dest); public function moveAsNextSiblingOf(Doctrine_Record $dest);
/** /**
* moves node as first child of dest record * moves node as first child of dest record
* *
*/ */
public function moveAsFirstChildOf(Doctrine_Record $dest); public function moveAsFirstChildOf(Doctrine_Record $dest);
/** /**
* moves node as last child of dest record * moves node as last child of dest record
* *
*/ */
public function moveAsLastChildOf(Doctrine_Record $dest); public function moveAsLastChildOf(Doctrine_Record $dest);
/** /**
* adds node as last child of record * adds node as last child of record
* *
*/ */
public function addChild(Doctrine_Record $record); public function addChild(Doctrine_Record $record);
/** /**
* determines if node is leaf * determines if node is leaf
* *
* @return bool * @return bool
*/ */
public function isLeaf(); public function isLeaf();
/** /**
* determines if node is root * determines if node is root
* *
* @return bool * @return bool
*/ */
public function isRoot(); public function isRoot();
/** /**
* determines if node is equal to subject node * determines if node is equal to subject node
* *
* @return bool * @return bool
*/ */
public function isEqualTo(Doctrine_Record $subj); public function isEqualTo(Doctrine_Record $subj);
/** /**
* determines if node is child of subject node * determines if node is child of subject node
* *
* @return bool * @return bool
*/ */
public function isDescendantOf(Doctrine_Record $subj); public function isDescendantOf(Doctrine_Record $subj);
/** /**
* determines if node is child of or sibling to subject node * determines if node is child of or sibling to subject node
* *
* @return bool * @return bool
*/ */
public function isDescendantOfOrEqualTo(Doctrine_Record $subj); public function isDescendantOfOrEqualTo(Doctrine_Record $subj);
/** /**
* determines if node is valid * determines if node is valid
* *
* @return bool * @return bool
*/ */
public function isValidNode(); public function isValidNode();
/** /**
* deletes node and it's descendants * deletes node and it's descendants
* *
*/ */
public function delete(); public function delete();
} }
\ No newline at end of file
...@@ -29,5 +29,6 @@ ...@@ -29,5 +29,6 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface { } class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface
{}
\ No newline at end of file
...@@ -32,30 +32,36 @@ ...@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
{ {
private $topNode = null; private $topNode = null;
private $curNode = null; private $curNode = null;
public function __construct($node, $opts) { public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function rewind() { public function rewind()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function valid() { public function valid()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function current() { public function current()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function key() { public function key()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function next() { public function next()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
} }
...@@ -32,30 +32,36 @@ ...@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
{ {
private $topNode = null; private $topNode = null;
private $curNode = null; private $curNode = null;
public function __construct($node, $opts) { public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function rewind() { public function rewind()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function valid() { public function valid()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function current() { public function current()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function key() { public function key()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function next() { public function next()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
} }
...@@ -32,30 +32,36 @@ ...@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
{ {
private $topNode = null; private $topNode = null;
private $curNode = null; private $curNode = null;
public function __construct($node, $opts) { public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function rewind() { public function rewind()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function valid() { public function valid()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function current() { public function current()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function key() { public function key()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
public function next() { public function next()
{
throw new Doctrine_Exception('Not yet implemented'); throw new Doctrine_Exception('Not yet implemented');
} }
} }
...@@ -31,643 +31,654 @@ ...@@ -31,643 +31,654 @@
*/ */
class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Interface class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Interface
{ {
/** /**
* test if node has previous sibling * test if node has previous sibling
* *
* @return bool * @return bool
*/ */
public function hasPrevSibling() public function hasPrevSibling()
{ {
return $this->isValidNode($this->getPrevSibling()); return $this->isValidNode($this->getPrevSibling());
} }
/** /**
* test if node has next sibling * test if node has next sibling
* *
* @return bool * @return bool
*/ */
public function hasNextSibling() public function hasNextSibling()
{ {
return $this->isValidNode($this->getNextSibling()); return $this->isValidNode($this->getNextSibling());
} }
/** /**
* test if node has children * test if node has children
* *
* @return bool * @return bool
*/ */
public function hasChildren() public function hasChildren()
{ {
return (($this->getRightValue() - $this->getLeftValue() ) >1 ); return (($this->getRightValue() - $this->getLeftValue() ) >1);
} }
/** /**
* test if node has parent * test if node has parent
* *
* @return bool * @return bool
*/ */
public function hasParent() public function hasParent()
{ {
return !$this->isRoot(); return !$this->isRoot();
} }
/** /**
* gets record of prev sibling or empty record * gets record of prev sibling or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getPrevSibling() public function getPrevSibling()
{ {
$q = $this->record->getTable()->createQuery(); $q = $this->record->getTable()->createQuery();
$result = $q->where('rgt = ?', $this->getLeftValue() - 1)->execute()->getFirst(); $result = $q->where('rgt = ?', $this->getLeftValue() - 1)->execute()->getFirst();
if(!$result) if (!$result) {
$result = $this->record->getTable()->create(); $result = $this->record->getTable()->create();
}
return $result;
} return $result;
}
/**
* gets record of next sibling or empty record /**
* * gets record of next sibling or empty record
* @return object Doctrine_Record *
*/ * @return object Doctrine_Record
public function getNextSibling() */
{ public function getNextSibling()
$q = $this->record->getTable()->createQuery(); {
$result = $q->where('lft = ?', $this->getRightValue() + 1)->execute()->getFirst(); $q = $this->record->getTable()->createQuery();
$result = $q->where('lft = ?', $this->getRightValue() + 1)->execute()->getFirst();
if(!$result)
$result = $this->record->getTable()->create(); if (!$result) {
$result = $this->record->getTable()->create();
return $result; }
}
return $result;
/** }
* gets siblings for node
* /**
* @return array array of sibling Doctrine_Record objects * gets siblings for node
*/ *
public function getSiblings($includeNode = false) * @return array array of sibling Doctrine_Record objects
{ */
$parent = $this->getParent(); public function getSiblings($includeNode = false)
$siblings = array(); {
if($parent->exists()) $parent = $this->getParent();
{ $siblings = array();
foreach($parent->getNode()->getChildren() as $child) if ($parent->exists()) {
{ foreach($parent->getNode()->getChildren() as $child) {
if($this->isEqualTo($child) && !$includeNode) if ($this->isEqualTo($child) && !$includeNode) {
continue; continue;
}
$siblings[] = $child; $siblings[] = $child;
} }
} }
return $siblings; return $siblings;
} }
/** /**
* gets record of first child or empty record * gets record of first child or empty record
* *
* @return object Doctrine_Record * @return object Doctrine_Record
*/ */
public function getFirstChild() public function getFirstChild()
{ {
$q = $this->record->getTable()->createQuery(); $q = $this->record->getTable()->createQuery();
$result = $q->where('lft = ?', $this->getLeftValue() + 1)->execute()->getFirst(); $result = $q->where('lft = ?', $this->getLeftValue() + 1)->execute()->getFirst();
if(!$result) if (!$result) {
$result = $this->record->getTable()->create(); $result = $this->record->getTable()->create();
}
return $result;
} return $result;
}
/**
* gets record of last child or empty record /**
* * gets record of last child or empty record
* @return object Doctrine_Record *
*/ * @return object Doctrine_Record
public function getLastChild() */
{ public function getLastChild()
$q = $this->record->getTable()->createQuery(); {
$result = $q->where('rgt = ?', $this->getRightValue() - 1)->execute()->getFirst(); $q = $this->record->getTable()->createQuery();
$result = $q->where('rgt = ?', $this->getRightValue() - 1)->execute()->getFirst();
if(!$result)
$result = $this->record->getTable()->create(); if (!$result) {
$result = $this->record->getTable()->create();
return $result; }
}
return $result;
/** }
* gets children for node (direct descendants only)
* /**
* @return array array of sibling Doctrine_Record objects * gets children for node (direct descendants only)
*/ *
public function getChildren() * @return array array of sibling Doctrine_Record objects
{ */
return $this->getIterator('Pre', array('depth' => 1)); public function getChildren()
} {
return $this->getIterator('Pre', array('depth' => 1));
/** }
* gets descendants for node (direct descendants only)
* /**
* @return iterator iterator to traverse descendants from node * gets descendants for node (direct descendants only)
*/ *
public function getDescendants() * @return iterator iterator to traverse descendants from node
{ */
return $this->getIterator(); public function getDescendants()
} {
return $this->getIterator();
/** }
* gets record of parent or empty record
* /**
* @return object Doctrine_Record * gets record of parent or empty record
*/ *
public function getParent() * @return object Doctrine_Record
{ */
$q = $this->record->getTable()->createQuery(); public function getParent()
{
$parent = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue())) $q = $this->record->getTable()->createQuery();
->orderBy('rgt asc')
->execute() $parent = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
->getFirst(); ->orderBy('rgt asc')
->execute()
if(!$parent) ->getFirst();
$parent = $this->record->getTable()->create();
if (!$parent) {
return $parent; $parent = $this->record->getTable()->create();
} }
/** return $parent;
* gets ancestors for node }
*
* @return object Doctrine_Collection /**
*/ * gets ancestors for node
public function getAncestors() *
{ * @return object Doctrine_Collection
$q = $this->record->getTable()->createQuery(); */
public function getAncestors()
$ancestors = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue())) {
->orderBy('lft asc') $q = $this->record->getTable()->createQuery();
->execute();
$ancestors = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
return $ancestors; ->orderBy('lft asc')
} ->execute();
/** return $ancestors;
* gets path to node from root, uses record::toString() method to get node names }
*
* @param string $seperator path seperator /**
* @param bool $includeNode whether or not to include node at end of path * gets path to node from root, uses record::toString() method to get node names
* @return string string representation of path *
*/ * @param string $seperator path seperator
public function getPath($seperator = ' > ', $includeRecord = false) * @param bool $includeNode whether or not to include node at end of path
{ * @return string string representation of path
$path = array(); */
$ancestors = $this->getAncestors(); public function getPath($seperator = ' > ', $includeRecord = false)
foreach($ancestors as $ancestor) {
{ $path = array();
$path[] = $ancestor->toString(); $ancestors = $this->getAncestors();
} foreach ($ancestors as $ancestor) {
if($includeRecord) $path[] = $ancestor->toString();
$path[] = $this->getRecord()->toString(); }
if ($includeRecord) {
return implode($seperator, $path); $path[] = $this->getRecord()->toString();
} }
/** return implode($seperator, $path);
* gets number of children (direct descendants) }
*
* @return int /**
*/ * gets number of children (direct descendants)
public function getNumberChildren() *
{ * @return int
$count = 0; */
$children = $this->getChildren(); public function getNumberChildren()
{
while($children->next()) $count = 0;
{ $children = $this->getChildren();
$count++;
} while ($children->next()) {
return $count; $count++;
} }
return $count;
/** }
* gets number of descendants (children and their children)
* /**
* @return int * gets number of descendants (children and their children)
*/ *
public function getNumberDescendants() * @return int
{ */
return ($this->getRightValue() - $this->getLeftValue() - 1) / 2; public function getNumberDescendants()
} {
return ($this->getRightValue() - $this->getLeftValue() - 1) / 2;
/** }
* inserts node as parent of dest record
* /**
* @return bool * inserts node as parent of dest record
*/ *
public function insertAsParentOf(Doctrine_Record $dest) * @return bool
{ */
// cannot insert a node that has already has a place within the tree public function insertAsParentOf(Doctrine_Record $dest)
if($this->isValidNode()) {
return false; // cannot insert a node that has already has a place within the tree
if ($this->isValidNode()) {
// cannot insert as parent of root return false;
if($dest->getNode()->isRoot()) }
return false;
// cannot insert as parent of root
$this->shiftRLValues($dest->getNode()->getLeftValue(), 1); if ($dest->getNode()->isRoot()) {
$this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1); return false;
}
$newLeft = $dest->getNode()->getLeftValue();
$newRight = $dest->getNode()->getRightValue() + 2; $this->shiftRLValues($dest->getNode()->getLeftValue(), 1);
$this->insertNode($newLeft, $newRight); $this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1);
return true; $newLeft = $dest->getNode()->getLeftValue();
} $newRight = $dest->getNode()->getRightValue() + 2;
$this->insertNode($newLeft, $newRight);
/**
* inserts node as previous sibling of dest record return true;
* }
* @return bool
*/ /**
public function insertAsPrevSiblingOf(Doctrine_Record $dest) * inserts node as previous sibling of dest record
{ *
// cannot insert a node that has already has a place within the tree * @return bool
if($this->isValidNode()) */
return false; public function insertAsPrevSiblingOf(Doctrine_Record $dest)
{
$newLeft = $dest->getNode()->getLeftValue(); // cannot insert a node that has already has a place within the tree
$newRight = $dest->getNode()->getLeftValue() + 1; if ($this->isValidNode()) {
return false;
$this->shiftRLValues($newLeft, 2); }
$this->insertNode($newLeft, $newRight);
$newLeft = $dest->getNode()->getLeftValue();
// update destination left/right values to prevent a refresh $newRight = $dest->getNode()->getLeftValue() + 1;
// $dest->getNode()->setLeftValue($dest->getNode()->getLeftValue() + 2);
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2); $this->shiftRLValues($newLeft, 2);
$this->insertNode($newLeft, $newRight);
return true;
} // update destination left/right values to prevent a refresh
// $dest->getNode()->setLeftValue($dest->getNode()->getLeftValue() + 2);
/** // $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
* inserts node as next sibling of dest record
* return true;
* @return bool }
*/
public function insertAsNextSiblingOf(Doctrine_Record $dest) /**
{ * inserts node as next sibling of dest record
// cannot insert a node that has already has a place within the tree *
if($this->isValidNode()) * @return bool
return false; */
public function insertAsNextSiblingOf(Doctrine_Record $dest)
$newLeft = $dest->getNode()->getRightValue() + 1; {
$newRight = $dest->getNode()->getRightValue() + 2; // cannot insert a node that has already has a place within the tree
if ($this->isValidNode()) {
$this->shiftRLValues($newLeft, 2); return false;
$this->insertNode($newLeft, $newRight); }
// update destination left/right values to prevent a refresh $newLeft = $dest->getNode()->getRightValue() + 1;
// no need, node not affected $newRight = $dest->getNode()->getRightValue() + 2;
return true; $this->shiftRLValues($newLeft, 2);
} $this->insertNode($newLeft, $newRight);
/** // update destination left/right values to prevent a refresh
* inserts node as first child of dest record // no need, node not affected
*
* @return bool return true;
*/ }
public function insertAsFirstChildOf(Doctrine_Record $dest)
{ /**
// cannot insert a node that has already has a place within the tree * inserts node as first child of dest record
if($this->isValidNode()) *
return false; * @return bool
*/
$newLeft = $dest->getNode()->getLeftValue() + 1; public function insertAsFirstChildOf(Doctrine_Record $dest)
$newRight = $dest->getNode()->getLeftValue() + 2; {
// cannot insert a node that has already has a place within the tree
$this->shiftRLValues($newLeft, 2); if ($this->isValidNode()) {
$this->insertNode($newLeft, $newRight); return false;
}
// update destination left/right values to prevent a refresh
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2); $newLeft = $dest->getNode()->getLeftValue() + 1;
$newRight = $dest->getNode()->getLeftValue() + 2;
return true;
} $this->shiftRLValues($newLeft, 2);
$this->insertNode($newLeft, $newRight);
/**
* inserts node as last child of dest record // update destination left/right values to prevent a refresh
* // $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
* @return bool
*/ return true;
public function insertAsLastChildOf(Doctrine_Record $dest) }
{
// cannot insert a node that has already has a place within the tree /**
if($this->isValidNode()) * inserts node as last child of dest record
return false; *
* @return bool
$newLeft = $dest->getNode()->getRightValue(); */
$newRight = $dest->getNode()->getRightValue() + 1; public function insertAsLastChildOf(Doctrine_Record $dest)
{
$this->shiftRLValues($newLeft, 2); // cannot insert a node that has already has a place within the tree
$this->insertNode($newLeft, $newRight); if ($this->isValidNode()) {
return false;
// update destination left/right values to prevent a refresh }
// $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
$newLeft = $dest->getNode()->getRightValue();
return true; $newRight = $dest->getNode()->getRightValue() + 1;
}
$this->shiftRLValues($newLeft, 2);
/** $this->insertNode($newLeft, $newRight);
* moves node as prev sibling of dest record
* // update destination left/right values to prevent a refresh
*/ // $dest->getNode()->setRightValue($dest->getNode()->getRightValue() + 2);
public function moveAsPrevSiblingOf(Doctrine_Record $dest)
{ return true;
$this->updateNode($dest->getNode()->getLeftValue()); }
}
/**
/** * moves node as prev sibling of dest record
* moves node as next sibling of dest record *
* */
*/ public function moveAsPrevSiblingOf(Doctrine_Record $dest)
public function moveAsNextSiblingOf(Doctrine_Record $dest) {
{ $this->updateNode($dest->getNode()->getLeftValue());
$this->updateNode($dest->getNode()->getRightValue() + 1); }
}
/**
/** * moves node as next sibling of dest record
* moves node as first child of dest record *
* */
*/ public function moveAsNextSiblingOf(Doctrine_Record $dest)
public function moveAsFirstChildOf(Doctrine_Record $dest) {
{ $this->updateNode($dest->getNode()->getRightValue() + 1);
$this->updateNode($dest->getNode()->getLeftValue() + 1); }
}
/**
/** * moves node as first child of dest record
* moves node as last child of dest record *
* */
*/ public function moveAsFirstChildOf(Doctrine_Record $dest)
public function moveAsLastChildOf(Doctrine_Record $dest) {
{ $this->updateNode($dest->getNode()->getLeftValue() + 1);
$this->updateNode($dest->getNode()->getRightValue()); }
}
/**
/** * moves node as last child of dest record
* adds node as last child of record *
* */
*/ public function moveAsLastChildOf(Doctrine_Record $dest)
public function addChild(Doctrine_Record $record) {
{ $this->updateNode($dest->getNode()->getRightValue());
$record->getNode()->insertAsLastChildOf($this->getRecord()); }
}
/**
/** * adds node as last child of record
* determines if node is leaf *
* */
* @return bool public function addChild(Doctrine_Record $record)
*/ {
public function isLeaf() $record->getNode()->insertAsLastChildOf($this->getRecord());
{ }
return (($this->getRightValue()-$this->getLeftValue())==1);
} /**
* determines if node is leaf
/** *
* determines if node is root * @return bool
* */
* @return bool public function isLeaf()
*/ {
public function isRoot() return (($this->getRightValue() - $this->getLeftValue()) == 1);
{ }
return ($this->getLeftValue()==1);
} /**
* determines if node is root
/** *
* determines if node is equal to subject node * @return bool
* */
* @return bool public function isRoot()
*/ {
public function isEqualTo(Doctrine_Record $subj) return ($this->getLeftValue() == 1);
{ }
return (($this->getLeftValue()==$subj->getNode()->getLeftValue()) and ($this->getRightValue()==$subj->getNode()->getRightValue()));
} /**
* determines if node is equal to subject node
/** *
* determines if node is child of subject node * @return bool
* */
* @return bool public function isEqualTo(Doctrine_Record $subj)
*/ {
public function isDescendantOf(Doctrine_Record $subj) // TODO: break this out to reduce line length <120 (Zend)
{ return (($this->getLeftValue()==$subj->getNode()->getLeftValue()) and ($this->getRightValue()==$subj->getNode()->getRightValue()));
return (($this->getLeftValue()>$subj->getNode()->getLeftValue()) and ($this->getRightValue()<$subj->getNode()->getRightValue())); }
}
/**
/** * determines if node is child of subject node
* determines if node is child of or sibling to subject node *
* * @return bool
* @return bool */
*/ public function isDescendantOf(Doctrine_Record $subj)
public function isDescendantOfOrEqualTo(Doctrine_Record $subj) {
{ // TODO: break this out to reduce line length <120 (Zend)
return (($this->getLeftValue()>=$subj->getNode()->getLeftValue()) and ($this->getRightValue()<=$subj->getNode()->getRightValue())); return (($this->getLeftValue()>$subj->getNode()->getLeftValue()) and ($this->getRightValue()<$subj->getNode()->getRightValue()));
} }
/** /**
* determines if node is valid * determines if node is child of or sibling to subject node
* *
* @return bool * @return bool
*/ */
public function isValidNode() public function isDescendantOfOrEqualTo(Doctrine_Record $subj)
{ {
return ($this->getRightValue() > $this->getLeftValue()); // TODO: break this out to reduce line length <120 (Zend)
} return (($this->getLeftValue()>=$subj->getNode()->getLeftValue()) and ($this->getRightValue()<=$subj->getNode()->getRightValue()));
}
/**
* deletes node and it's descendants /**
* * determines if node is valid
*/ *
public function delete() * @return bool
{ */
// TODO: add the setting whether or not to delete descendants or relocate children public function isValidNode()
{
$q = $this->record->getTable()->createQuery(); return ($this->getRightValue() > $this->getLeftValue());
}
$componentName = $this->record->getTable()->getComponentName();
/**
$coll = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($this->getLeftValue(), $this->getRightValue()))->execute(); * deletes node and it's descendants
$coll->delete(); *
*/
$first = $this->getRightValue() + 1; public function delete()
$delta = $this->getLeftValue() - $this->getRightValue() - 1; {
$this->shiftRLValues($first, $delta); // TODO: add the setting whether or not to delete descendants or relocate children
return true; $q = $this->record->getTable()->createQuery();
}
$componentName = $this->record->getTable()->getComponentName();
/**
* sets node's left and right values and save's it $params = array($this->getLeftValue(), $this->getRightValue());
* $coll = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->execute();
* @param int $destLeft node left value $coll->delete();
* @param int $destRight node right value
*/ $first = $this->getRightValue() + 1;
private function insertNode($destLeft = 0, $destRight = 0) $delta = $this->getLeftValue() - $this->getRightValue() - 1;
{ $this->shiftRLValues($first, $delta);
$this->setLeftValue($destLeft);
$this->setRightValue($destRight); return true;
$this->record->save(); }
}
/**
/** * sets node's left and right values and save's it
* move node's and its children to location $destLeft and updates rest of tree *
* * @param int $destLeft node left value
* @param int $destLeft destination left value * @param int $destRight node right value
*/ */
private function updateNode($destLeft) private function insertNode($destLeft = 0, $destRight = 0)
{ {
$left = $this->getLeftValue(); $this->setLeftValue($destLeft);
$right = $this->getRightValue(); $this->setRightValue($destRight);
$this->record->save();
$treeSize = $right - $left + 1; }
$this->shiftRLValues($destLeft, $treeSize); /**
* move node's and its children to location $destLeft and updates rest of tree
if($left >= $destLeft){ // src was shifted too? *
$left += $treeSize; * @param int $destLeft destination left value
$right += $treeSize; */
} private function updateNode($destLeft)
{
// now there's enough room next to target to move the subtree $left = $this->getLeftValue();
$this->shiftRLRange($left, $right, $destLeft - $left); $right = $this->getRightValue();
// correct values after source $treeSize = $right - $left + 1;
$this->shiftRLValues($right + 1, -$treeSize);
$this->shiftRLValues($destLeft, $treeSize);
$this->record->save();
$this->record->refresh(); if ($left >= $destLeft) { // src was shifted too?
} $left += $treeSize;
$right += $treeSize;
/** }
* adds '$delta' to all Left and Right values that are >= '$first'. '$delta' can also be negative.
* // now there's enough room next to target to move the subtree
* @param int $first First node to be shifted $this->shiftRLRange($left, $right, $destLeft - $left);
* @param int $delta Value to be shifted by, can be negative
*/ // correct values after source
private function shiftRLValues($first, $delta) $this->shiftRLValues($right + 1, -$treeSize);
{
$qLeft = $this->record->getTable()->createQuery(); $this->record->save();
$qRight = $this->record->getTable()->createQuery(); $this->record->refresh();
}
// TODO: Wrap in transaction
/**
// shift left columns * adds '$delta' to all Left and Right values that are >= '$first'. '$delta' can also be negative.
$resultLeft = $qLeft->update($this->record->getTable()->getComponentName()) *
->set('lft', "lft + $delta") * @param int $first first node to be shifted
->where('lft >= ?', $first) * @param int $delta value to be shifted by, can be negative
->execute(); */
private function shiftRLValues($first, $delta)
// shift right columns {
$resultRight = $qRight->update($this->record->getTable()->getComponentName()) $qLeft = $this->record->getTable()->createQuery();
->set('rgt', "rgt + $delta") $qRight = $this->record->getTable()->createQuery();
->where('rgt >= ?', $first)
->execute(); // TODO: Wrap in transaction
}
// shift left columns
/** $resultLeft = $qLeft->update($this->record->getTable()->getComponentName())
* adds '$delta' to all Left and Right values that are >= '$first' and <= '$last'. ->set('lft', "lft + $delta")
* '$delta' can also be negative. ->where('lft >= ?', $first)
* ->execute();
* @param int $first First node to be shifted (L value)
* @param int $last Last node to be shifted (L value) // shift right columns
* @param int $delta Value to be shifted by, can be negative $resultRight = $qRight->update($this->record->getTable()->getComponentName())
*/ ->set('rgt', "rgt + $delta")
private function shiftRLRange($first, $last, $delta) ->where('rgt >= ?', $first)
{ ->execute();
$qLeft = $this->record->getTable()->createQuery(); }
$qRight = $this->record->getTable()->createQuery();
/**
// TODO : Wrap in transaction * adds '$delta' to all Left and Right values that are >= '$first' and <= '$last'.
* '$delta' can also be negative.
// shift left column values *
$result = $qLeft->update($this->record->getTable()->getComponentName()) * @param int $first first node to be shifted (L value)
->set('lft', "lft + $delta") * @param int $last last node to be shifted (L value)
->where('lft >= ? AND lft <= ?', array($first, $last)) * @param int $delta value to be shifted by, can be negative
->execute(); */
private function shiftRLRange($first, $last, $delta)
// shift right column values {
$result = $qRight->update($this->record->getTable()->getComponentName()) $qLeft = $this->record->getTable()->createQuery();
->set('rgt', "rgt + $delta") $qRight = $this->record->getTable()->createQuery();
->where('rgt >= ? AND rgt <= ?', array($first, $last))
->execute(); // TODO : Wrap in transaction
}
// shift left column values
/** $result = $qLeft->update($this->record->getTable()->getComponentName())
* gets record's left value ->set('lft', "lft + $delta")
* ->where('lft >= ? AND lft <= ?', array($first, $last))
* @return int ->execute();
*/
public function getLeftValue() // shift right column values
{ $result = $qRight->update($this->record->getTable()->getComponentName())
return $this->record->get('lft'); ->set('rgt', "rgt + $delta")
} ->where('rgt >= ? AND rgt <= ?', array($first, $last))
->execute();
/** }
* sets record's left value
* /**
* @param int * gets record's left value
*/ *
public function setLeftValue($lft) * @return int
{ */
$this->record->set('lft', $lft); public function getLeftValue()
} {
return $this->record->get('lft');
/** }
* gets record's right value
* /**
* @return int * sets record's left value
*/ *
public function getRightValue() * @param int
{ */
return $this->record->get('rgt'); public function setLeftValue($lft)
} {
$this->record->set('lft', $lft);
/** }
* sets record's right value
* /**
* @param int * gets record's right value
*/ *
public function setRightValue($rgt) * @return int
{ */
$this->record->set('rgt', $rgt); public function getRightValue()
} {
return $this->record->get('rgt');
/** }
* gets level (depth) of node in the tree
* /**
* @return int * sets record's right value
*/ *
public function getLevel() * @param int
{ */
if(!isset($this->level)) public function setRightValue($rgt)
{ {
$q = $this->record->getTable()->createQuery(); $this->record->set('rgt', $rgt);
$coll = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue())) }
->execute();
$this->level = $coll->count() ? $coll->count() : 0; /**
} * gets level (depth) of node in the tree
*
return $this->level; * @return int
} */
public function getLevel()
/** {
* sets node's level if (!isset($this->level)) {
* $q = $this->record->getTable()->createQuery();
* @param int $coll = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
*/ ->execute();
public function setLevel($level) $this->level = $coll->count() ? $coll->count() : 0;
{ }
$this->level = $level;
} return $this->level;
} }
\ No newline at end of file
/**
* sets node's level
*
* @param int
*/
public function setLevel($level)
{
$this->level = $level;
}
}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator {} class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator
{}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator {} class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator
{}
...@@ -30,9 +30,9 @@ ...@@ -30,9 +30,9 @@
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
{ {
/** /**
* @var Doctrine_Collection $collection * @var Doctrine_Collection $collection
*/ */
protected $collection; protected $collection;
/** /**
...@@ -59,34 +59,32 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -59,34 +59,32 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
* @var integer $count * @var integer $count
*/ */
protected $count; protected $count;
public function __construct($record, $opts) { public function __construct($record, $opts)
{
$componentName = $record->getTable()->getComponentName(); $componentName = $record->getTable()->getComponentName();
$q = $record->getTable()->createQuery(); $q = $record->getTable()->createQuery();
if(isset($opts['include_record']) && $opts['include_record']) $params = array($record->get('lft'), $record->get('rgt'));
{ if (isset($opts['include_record']) && $opts['include_record']) {
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($record->get('lft'), $record->get('rgt')))->orderBy('lft asc'); $query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy('lft asc');
} } else {
else $query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy('lft asc');
{ }
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", array($record->get('lft'), $record->get('rgt')))->orderBy('lft asc');
} $this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
$this->options = $opts;
$this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0; $this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
$this->options = $opts; $this->keys = $this->collection->getKeys();
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute(); $this->count = $this->collection->count();
$this->keys = $this->collection->getKeys(); $this->index = -1;
$this->count = $this->collection->count(); $this->level = $record->getNode()->getLevel();
$this->index = -1; $this->prevLeft = $record->getNode()->getLeftValue();
$this->level = $record->getNode()->getLevel();
$this->prevLeft = $record->getNode()->getLeftValue(); echo $this->maxDepth;
// clear the table identity cache
echo $this->maxDepth; $record->getTable()->clear();
// clear the table identity cache
$record->getTable()->clear();
} }
/** /**
...@@ -94,7 +92,8 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -94,7 +92,8 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
* *
* @return void * @return void
*/ */
public function rewind() { public function rewind()
{
$this->index = -1; $this->index = -1;
$this->key = null; $this->key = null;
} }
...@@ -104,66 +103,73 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -104,66 +103,73 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
* *
* @return integer * @return integer
*/ */
public function key() { public function key()
{
return $this->key; return $this->key;
} }
/** /**
* returns the current record * returns the current record
* *
* @return Doctrine_Record * @return Doctrine_Record
*/ */
public function current() { public function current()
{
$record = $this->collection->get($this->key); $record = $this->collection->get($this->key);
$record->getNode()->setLevel($this->level); $record->getNode()->setLevel($this->level);
return $record; return $record;
} }
/** /**
* advances the internal pointer * advances the internal pointer
* *
* @return void * @return void
*/ */
public function next() { public function next()
while($current = $this->advanceIndex()) {
{ while ($current = $this->advanceIndex()) {
if($this->maxLevel && ($this->level > $this->maxLevel)) if ($this->maxLevel && ($this->level > $this->maxLevel)) {
continue; continue;
}
return $current;
} return $current;
}
return false;
return false;
} }
/** /**
* @return boolean whether or not the iteration will continue * @return boolean whether or not the iteration will continue
*/ */
public function valid() { public function valid()
{
return ($this->index < $this->count); return ($this->index < $this->count);
} }
public function count() { public function count()
{
return $this->count; return $this->count;
} }
private function updateLevel() { private function updateLevel()
if(!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0)) {
{ if (!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0)) {
$left = $this->collection->get($this->key)->getNode()->getLeftValue(); $left = $this->collection->get($this->key)->getNode()->getLeftValue();
$this->level += $this->prevLeft - $left + 2; $this->level += $this->prevLeft - $left + 2;
$this->prevLeft = $left; $this->prevLeft = $left;
} }
} }
private function advanceIndex() { private function advanceIndex()
{
$this->index++; $this->index++;
$i = $this->index; $i = $this->index;
if(isset($this->keys[$i])) if (isset($this->keys[$i])) {
{
$this->key = $this->keys[$i]; $this->key = $this->keys[$i];
$this->updateLevel(); $this->updateLevel();
return $this->current(); return $this->current();
} }
return false; return false;
} }
} }
...@@ -29,61 +29,63 @@ ...@@ -29,61 +29,63 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Tree class Doctrine_Tree
{ {
/** /**
* @param object $table reference to associated Doctrine_Table instance * @param object $table reference to associated Doctrine_Table instance
*/ */
protected $table; protected $table;
/**
* @param array $options
*/
protected $options;
/** /**
* contructor, creates tree with reference to table and any options * @param array $options
* */
* @param object $table instance of Doctrine_Table protected $options;
* @param array $options options
*/
public function __construct($table, $options)
{
$this->table = $table;
$this->options = $options;
}
/** /**
* Used to define table attributes required for the given implementation * constructor, creates tree with reference to table and any options
* *
*/ * @param object $table instance of Doctrine_Table
public function setTableDefinition() * @param array $options options
{ */
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.'); public function __construct($table, $options)
} {
$this->table = $table;
$this->options = $options;
}
/** /**
* this method is used for setting up relations and attributes and should be used by specific implementations * Used to define table attributes required for the given implementation
* *
*/ * @throws Doctrine_Tree_Exception if table attributes have not been defined
public function setUp() */
{ public function setTableDefinition()
{
} throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
}
/** /**
* factory method to return tree instance based upon chosen implementation * this method is used for setting up relations and attributes and should be used by specific implementations
* *
* @param object $table instance of Doctrine_Table */
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath) public function setUp()
* @param array $options options {
* @return object $options instance of Doctrine_Node }
*/
public static function factory( &$table, $implName, $options = array()) /**
{ * factory method to return tree instance based upon chosen implementation
$class = 'Doctrine_Tree_'.$implName; *
if(!class_exists($class)) * @param object $table instance of Doctrine_Table
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree'); * @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
return new $class($table, $options); * @param array $options options
} * @return object $options instance of Doctrine_Node
} // END class * @throws Doctrine_Exception if class does not extend Doctrine_Tree
\ No newline at end of file */
public static function factory(&$table, $implName, $options = array())
{
$class = 'Doctrine_Tree_' . $implName;
if (!class_exists($class)) {
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
}
return new $class($table, $options);
}
}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Tree_AdjacencyList extends Doctrine_Tree implements Doctrine_Tree_Interface { } class Doctrine_Tree_AdjacencyList extends Doctrine_Tree implements Doctrine_Tree_Interface
{}
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Tree_Exception extends Doctrine_Exception { } class Doctrine_Tree_Exception extends Doctrine_Exception
{}
...@@ -31,34 +31,34 @@ ...@@ -31,34 +31,34 @@
*/ */
interface Doctrine_Tree_Interface { interface Doctrine_Tree_Interface {
/** /**
* creates root node from given record or from a new record * creates root node from given record or from a new record
* *
* @param object $record instance of Doctrine_Record * @param object $record instance of Doctrine_Record
*/ */
public function createRoot(Doctrine_Record $record = null); public function createRoot(Doctrine_Record $record = null);
/** /**
* returns root node * returns root node
* *
* @return object $record instance of Doctrine_Record * @return object $record instance of Doctrine_Record
*/ */
public function findRoot(); public function findRoot();
/** /**
* optimised method to returns iterator for traversal of the entire tree from root * optimised method to returns iterator for traversal of the entire tree from root
* *
* @param array $options options * @param array $options options
* @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator * @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/ */
public function fetchTree($options = array()); public function fetchTree($options = array());
/** /**
* optimised method that returns iterator for traversal of the tree from the given record primary key * optimised method that returns iterator for traversal of the tree from the given record primary key
* *
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from * @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
* @param array $options options * @param array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator * @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/ */
public function fetchBranch($pk, $options = array()); public function fetchBranch($pk, $options = array());
} }
\ No newline at end of file
...@@ -29,4 +29,5 @@ ...@@ -29,4 +29,5 @@
* @version $Revision$ * @version $Revision$
* @author Joe Simms <joe.simms@websites4.com> * @author Joe Simms <joe.simms@websites4.com>
*/ */
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface { } class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface
\ No newline at end of file {}
...@@ -31,113 +31,114 @@ ...@@ -31,113 +31,114 @@
*/ */
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
{ {
/** /**
* used to define table attributes required for the NestetSet implementation * used to define table attributes required for the NestetSet implementation
* adds lft and rgt columns for corresponding left and right values * adds lft and rgt columns for corresponding left and right values
* *
*/ */
public function setTableDefinition() public function setTableDefinition()
{
$this->table->setColumn("lft","integer",11);
$this->table->setColumn("rgt","integer",11);
}
/**
* creates root node from given record or from a new record
*
* @param object $record instance of Doctrine_Record
*/
public function createRoot(Doctrine_Record $record = null)
{
if(!$record)
{ {
$record = $this->table->create(); $this->table->setColumn("lft","integer",11);
$this->table->setColumn("rgt","integer",11);
} }
$record->set('lft', '1'); /**
$record->set('rgt', '2'); * creates root node from given record or from a new record
$record->save(); *
* @param object $record instance of Doctrine_Record
return $record; */
} public function createRoot(Doctrine_Record $record = null)
/**
* returns root node
*
* @return object $record instance of Doctrine_Record
*/
public function findRoot()
{
$q = $this->table->createQuery();
$root = $q->where('lft = ?', 1)
->execute()->getFirst();
// if no record is returned, create record
if(!$root)
{ {
$root = $this->table->create(); if (!$record) {
$record = $this->table->create();
}
$record->set('lft', '1');
$record->set('rgt', '2');
$record->save();
return $record;
} }
// set level to prevent additional query to determine level /**
$root->getNode()->setLevel(0); * returns root node
*
return $root; * @return object $record instance of Doctrine_Record
} */
/** public function findRoot()
* optimised method to returns iterator for traversal of the entire tree from root
*
* @param array $options options
* @return object $iterator instance of Doctrine_Node_NestedSet_PreOrderIterator
*/
public function fetchTree($options = array())
{
// fetch tree
$q = $this->table->createQuery();
$tree = $q->where('lft >= ?', 1)
->orderBy('lft asc')
->execute();
$root = $tree->getFirst();
// if no record is returned, create record
if(!$root)
{ {
$root = $this->table->create(); $q = $this->table->createQuery();
$root = $q->where('lft = ?', 1)
->execute()->getFirst();
// if no record is returned, create record
if (!$root) {
$root = $this->table->create();
}
// set level to prevent additional query to determine level
$root->getNode()->setLevel(0);
return $root;
} }
if($root->exists()) /**
* optimised method to returns iterator for traversal of the entire tree from root
*
* @param array $options options
* @return object $iterator instance of Doctrine_Node_NestedSet_PreOrderIterator
*/
public function fetchTree($options = array())
{ {
// set level to prevent additional query // fetch tree
$root->getNode()->setLevel(0); $q = $this->table->createQuery();
$tree = $q->where('lft >= ?', 1)
->orderBy('lft asc')
->execute();
// default to include root node $root = $tree->getFirst();
$options = array_merge(array('include_record'=>true), $options);
// remove root node from collection if not required
if($options['include_record'] == false)
$tree->remove(0);
// set collection for iterator // if no record is returned, create record
$options['collection'] = $tree; if (!$root) {
$root = $this->table->create();
}
return $root->getNode()->traverse('Pre', $options); if ($root->exists()) {
// set level to prevent additional query
$root->getNode()->setLevel(0);
// default to include root node
$options = array_merge(array('include_record'=>true), $options);
// remove root node from collection if not required
if($options['include_record'] == false)
$tree->remove(0);
// set collection for iterator
$options['collection'] = $tree;
return $root->getNode()->traverse('Pre', $options);
}
// TODO: no default return value or exception thrown?
} }
}
/** /**
* optimised method that returns iterator for traversal of the tree from the given record primary key * optimised method that returns iterator for traversal of the tree from the given record primary key
* *
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from * @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
* @param array $options options * @param array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator * @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/ */
public function fetchBranch($pk, $options = array()) public function fetchBranch($pk, $options = array())
{
$record = $this->table->find($pk);
if($record->exists())
{ {
$options = array_merge(array('include_record'=>true), $options); $record = $this->table->find($pk);
return $record->getNode()->traverse('Pre', $options); if ($record->exists()) {
$options = array_merge(array('include_record'=>true), $options);
return $record->getNode()->traverse('Pre', $options);
}
// TODO: if record doesn't exist, throw exception or similar?
} }
} }
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment