1
0
mirror of synced 2025-01-30 12:01:44 +03:00

updated NestedSet to use Zend coding standards. still a few problems with return values using () that need to be looked at.

This commit is contained in:
david 2007-01-06 13:18:51 +00:00
parent 6786ce0ced
commit 2933cd2dd1
21 changed files with 1310 additions and 1253 deletions

View File

@ -71,12 +71,13 @@ class Doctrine_Node implements IteratorAggregate
* @param array $options options
* @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;
if(!class_exists($class))
if (!class_exists($class)) {
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
}
return new $class($record, $options);
}
@ -96,7 +97,8 @@ class Doctrine_Node implements IteratorAggregate
*
* @return object instance of Doctrine_Record
*/
public function getRecord() {
public function getRecord()
{
return $this->record;
}
@ -106,7 +108,8 @@ class Doctrine_Node implements IteratorAggregate
* @param string $type type of iterator (Pre | Post | Level)
* @param array $options options
*/
public function traverse($type = 'Pre', $options = array()) {
public function traverse($type = 'Pre', $options = array())
{
return $this->getIterator($type, $options);
}
@ -118,13 +121,16 @@ class Doctrine_Node implements IteratorAggregate
*/
public function getIterator($type = null, $options = null)
{
if ($type === null)
if ($type === null) {
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
}
if ($options === null)
if ($options === null) {
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
}
$iteratorClass = 'Doctrine_Node_'.$this->record->getTable()->getTreeImplName().'_'.ucfirst(strtolower($type)).'OrderIterator';
$implName = $this->record->getTable()->getTreeImplName();
$iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
return new $iteratorClass($this->record, $options);
}
@ -134,7 +140,8 @@ class Doctrine_Node implements IteratorAggregate
*
* @param int
*/
public function setIteratorType($type) {
public function setIteratorType($type)
{
$this->iteratorType = $type;
}
@ -143,7 +150,8 @@ class Doctrine_Node implements IteratorAggregate
*
* @param int
*/
public function setIteratorOptions($options) {
public function setIteratorOptions($options)
{
$this->iteratorOptions = $options;
}
} // END class
}

View File

@ -29,5 +29,6 @@
* @version $Revision$
* @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
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator {}
class Doctrine_Node_AdjacencyList_LevelOrderIterator implements Iterator
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator {}
class Doctrine_Node_AdjacencyList_PostOrderIterator implements Iterator
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator {}
class Doctrine_Node_AdjacencyList_PreOrderIterator implements Iterator
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_Exception extends Doctrine_Exception { }
class Doctrine_Node_Exception extends Doctrine_Exception
{}

View File

@ -29,5 +29,6 @@
* @version $Revision$
* @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
{}

View File

@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
private $curNode = null;
public function __construct($node, $opts) {
public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented');
}
public function rewind() {
public function rewind()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function valid() {
public function valid()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function current() {
public function current()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function key() {
public function key()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function next() {
public function next()
{
throw new Doctrine_Exception('Not yet implemented');
}
}

View File

@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
private $curNode = null;
public function __construct($node, $opts) {
public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented');
}
public function rewind() {
public function rewind()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function valid() {
public function valid()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function current() {
public function current()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function key() {
public function key()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function next() {
public function next()
{
throw new Doctrine_Exception('Not yet implemented');
}
}

View File

@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
private $curNode = null;
public function __construct($node, $opts) {
public function __construct($node, $opts)
{
throw new Doctrine_Exception('Not yet implemented');
}
public function rewind() {
public function rewind()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function valid() {
public function valid()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function current() {
public function current()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function key() {
public function key()
{
throw new Doctrine_Exception('Not yet implemented');
}
public function next() {
public function next()
{
throw new Doctrine_Exception('Not yet implemented');
}
}

View File

@ -81,8 +81,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$q = $this->record->getTable()->createQuery();
$result = $q->where('rgt = ?', $this->getLeftValue() - 1)->execute()->getFirst();
if(!$result)
if (!$result) {
$result = $this->record->getTable()->create();
}
return $result;
}
@ -97,8 +98,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$q = $this->record->getTable()->createQuery();
$result = $q->where('lft = ?', $this->getRightValue() + 1)->execute()->getFirst();
if(!$result)
if (!$result) {
$result = $this->record->getTable()->create();
}
return $result;
}
@ -112,13 +114,11 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
{
$parent = $this->getParent();
$siblings = array();
if($parent->exists())
{
foreach($parent->getNode()->getChildren() as $child)
{
if($this->isEqualTo($child) && !$includeNode)
if ($parent->exists()) {
foreach($parent->getNode()->getChildren() as $child) {
if ($this->isEqualTo($child) && !$includeNode) {
continue;
}
$siblings[] = $child;
}
}
@ -136,8 +136,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$q = $this->record->getTable()->createQuery();
$result = $q->where('lft = ?', $this->getLeftValue() + 1)->execute()->getFirst();
if(!$result)
if (!$result) {
$result = $this->record->getTable()->create();
}
return $result;
}
@ -152,8 +153,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$q = $this->record->getTable()->createQuery();
$result = $q->where('rgt = ?', $this->getRightValue() - 1)->execute()->getFirst();
if(!$result)
if (!$result) {
$result = $this->record->getTable()->create();
}
return $result;
}
@ -192,8 +194,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
->execute()
->getFirst();
if(!$parent)
if (!$parent) {
$parent = $this->record->getTable()->create();
}
return $parent;
}
@ -225,12 +228,12 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
{
$path = array();
$ancestors = $this->getAncestors();
foreach($ancestors as $ancestor)
{
foreach ($ancestors as $ancestor) {
$path[] = $ancestor->toString();
}
if($includeRecord)
if ($includeRecord) {
$path[] = $this->getRecord()->toString();
}
return implode($seperator, $path);
}
@ -245,8 +248,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$count = 0;
$children = $this->getChildren();
while($children->next())
{
while ($children->next()) {
$count++;
}
return $count;
@ -270,12 +272,14 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
public function insertAsParentOf(Doctrine_Record $dest)
{
// cannot insert a node that has already has a place within the tree
if($this->isValidNode())
if ($this->isValidNode()) {
return false;
}
// cannot insert as parent of root
if($dest->getNode()->isRoot())
if ($dest->getNode()->isRoot()) {
return false;
}
$this->shiftRLValues($dest->getNode()->getLeftValue(), 1);
$this->shiftRLValues($dest->getNode()->getRightValue() + 2, 1);
@ -295,8 +299,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
public function insertAsPrevSiblingOf(Doctrine_Record $dest)
{
// cannot insert a node that has already has a place within the tree
if($this->isValidNode())
if ($this->isValidNode()) {
return false;
}
$newLeft = $dest->getNode()->getLeftValue();
$newRight = $dest->getNode()->getLeftValue() + 1;
@ -319,8 +324,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
public function insertAsNextSiblingOf(Doctrine_Record $dest)
{
// cannot insert a node that has already has a place within the tree
if($this->isValidNode())
if ($this->isValidNode()) {
return false;
}
$newLeft = $dest->getNode()->getRightValue() + 1;
$newRight = $dest->getNode()->getRightValue() + 2;
@ -342,8 +348,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
public function insertAsFirstChildOf(Doctrine_Record $dest)
{
// cannot insert a node that has already has a place within the tree
if($this->isValidNode())
if ($this->isValidNode()) {
return false;
}
$newLeft = $dest->getNode()->getLeftValue() + 1;
$newRight = $dest->getNode()->getLeftValue() + 2;
@ -365,8 +372,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
public function insertAsLastChildOf(Doctrine_Record $dest)
{
// cannot insert a node that has already has a place within the tree
if($this->isValidNode())
if ($this->isValidNode()) {
return false;
}
$newLeft = $dest->getNode()->getRightValue();
$newRight = $dest->getNode()->getRightValue() + 1;
@ -452,6 +460,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/
public function isEqualTo(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()));
}
@ -462,6 +471,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/
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()));
}
@ -472,6 +482,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/
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()));
}
@ -497,7 +508,8 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
$componentName = $this->record->getTable()->getComponentName();
$coll = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", array($this->getLeftValue(), $this->getRightValue()))->execute();
$params = array($this->getLeftValue(), $this->getRightValue());
$coll = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->execute();
$coll->delete();
$first = $this->getRightValue() + 1;
@ -552,8 +564,8 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
/**
* adds '$delta' to all Left and Right values that are >= '$first'. '$delta' can also be negative.
*
* @param int $first First node to be shifted
* @param int $delta Value to be shifted by, can be negative
* @param int $first first node to be shifted
* @param int $delta value to be shifted by, can be negative
*/
private function shiftRLValues($first, $delta)
{
@ -579,9 +591,9 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
* adds '$delta' to all Left and Right values that are >= '$first' and <= '$last'.
* '$delta' can also be negative.
*
* @param int $first First node to be shifted (L value)
* @param int $last Last node to be shifted (L value)
* @param int $delta Value to be shifted by, can be negative
* @param int $first first node to be shifted (L value)
* @param int $last last node to be shifted (L value)
* @param int $delta value to be shifted by, can be negative
*/
private function shiftRLRange($first, $last, $delta)
{
@ -650,8 +662,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
*/
public function getLevel()
{
if(!isset($this->level))
{
if (!isset($this->level)) {
$q = $this->record->getTable()->createQuery();
$coll = $q->where('lft < ? AND rgt > ?', array($this->getLeftValue(), $this->getRightValue()))
->execute();

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator {}
class Doctrine_Node_NestedSet_LevelOrderIterator implements Iterator
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator {}
class Doctrine_Node_NestedSet_PostOrderIterator implements Iterator
{}

View File

@ -60,19 +60,17 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
*/
protected $count;
public function __construct($record, $opts) {
public function __construct($record, $opts)
{
$componentName = $record->getTable()->getComponentName();
$q = $record->getTable()->createQuery();
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');
}
else
{
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", array($record->get('lft'), $record->get('rgt')))->orderBy('lft asc');
$params = array($record->get('lft'), $record->get('rgt'));
if (isset($opts['include_record']) && $opts['include_record']) {
$query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy('lft asc');
} else {
$query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy('lft asc');
}
$this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
@ -94,7 +92,8 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
*
* @return void
*/
public function rewind() {
public function rewind()
{
$this->index = -1;
$this->key = null;
}
@ -104,29 +103,34 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
*
* @return integer
*/
public function key() {
public function key()
{
return $this->key;
}
/**
* returns the current record
*
* @return Doctrine_Record
*/
public function current() {
public function current()
{
$record = $this->collection->get($this->key);
$record->getNode()->setLevel($this->level);
return $record;
}
/**
* advances the internal pointer
*
* @return void
*/
public function next() {
while($current = $this->advanceIndex())
public function next()
{
if($this->maxLevel && ($this->level > $this->maxLevel))
while ($current = $this->advanceIndex()) {
if ($this->maxLevel && ($this->level > $this->maxLevel)) {
continue;
}
return $current;
}
@ -137,28 +141,30 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
/**
* @return boolean whether or not the iteration will continue
*/
public function valid() {
public function valid()
{
return ($this->index < $this->count);
}
public function count() {
public function count()
{
return $this->count;
}
private function updateLevel() {
if(!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0))
private function updateLevel()
{
if (!(isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0)) {
$left = $this->collection->get($this->key)->getNode()->getLeftValue();
$this->level += $this->prevLeft - $left + 2;
$this->prevLeft = $left;
}
}
private function advanceIndex() {
private function advanceIndex()
{
$this->index++;
$i = $this->index;
if(isset($this->keys[$i]))
{
if (isset($this->keys[$i])) {
$this->key = $this->keys[$i];
$this->updateLevel();
return $this->current();

View File

@ -42,7 +42,7 @@ class Doctrine_Tree
protected $options;
/**
* contructor, creates tree with reference to table and any options
* constructor, creates tree with reference to table and any options
*
* @param object $table instance of Doctrine_Table
* @param array $options options
@ -56,6 +56,7 @@ class Doctrine_Tree
/**
* Used to define table attributes required for the given implementation
*
* @throws Doctrine_Tree_Exception if table attributes have not been defined
*/
public function setTableDefinition()
{
@ -68,7 +69,6 @@ class Doctrine_Tree
*/
public function setUp()
{
}
/**
@ -78,12 +78,14 @@ class Doctrine_Tree
* @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
*/
public static function factory(&$table, $implName, $options = array())
{
$class = 'Doctrine_Tree_' . $implName;
if(!class_exists($class))
if (!class_exists($class)) {
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
}
return new $class($table, $options);
}
} // END class
}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @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
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Tree_Exception extends Doctrine_Exception { }
class Doctrine_Tree_Exception extends Doctrine_Exception
{}

View File

@ -29,4 +29,5 @@
* @version $Revision$
* @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
{}

View File

@ -49,8 +49,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
*/
public function createRoot(Doctrine_Record $record = null)
{
if(!$record)
{
if (!$record) {
$record = $this->table->create();
}
@ -73,8 +72,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
->execute()->getFirst();
// if no record is returned, create record
if(!$root)
{
if (!$root) {
$root = $this->table->create();
}
@ -83,6 +81,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
return $root;
}
/**
* optimised method to returns iterator for traversal of the entire tree from root
*
@ -101,13 +100,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
$root = $tree->getFirst();
// if no record is returned, create record
if(!$root)
{
if (!$root) {
$root = $this->table->create();
}
if($root->exists())
{
if ($root->exists()) {
// set level to prevent additional query
$root->getNode()->setLevel(0);
@ -123,7 +120,10 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
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
*
@ -134,10 +134,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
public function fetchBranch($pk, $options = array())
{
$record = $this->table->find($pk);
if($record->exists())
{
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?
}
}