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
...@@ -71,12 +71,13 @@ class Doctrine_Node implements IteratorAggregate ...@@ -71,12 +71,13 @@ class Doctrine_Node implements IteratorAggregate
* @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);
} }
...@@ -96,7 +97,8 @@ class Doctrine_Node implements IteratorAggregate ...@@ -96,7 +97,8 @@ class Doctrine_Node implements IteratorAggregate
* *
* @return object instance of Doctrine_Record * @return object instance of Doctrine_Record
*/ */
public function getRecord() { public function getRecord()
{
return $this->record; return $this->record;
} }
...@@ -106,7 +108,8 @@ class Doctrine_Node implements IteratorAggregate ...@@ -106,7 +108,8 @@ class Doctrine_Node implements IteratorAggregate
* @param string $type type of iterator (Pre | Post | Level) * @param string $type type of iterator (Pre | Post | Level)
* @param array $options options * @param array $options options
*/ */
public function traverse($type = 'Pre', $options = array()) { public function traverse($type = 'Pre', $options = array())
{
return $this->getIterator($type, $options); return $this->getIterator($type, $options);
} }
...@@ -118,13 +121,16 @@ class Doctrine_Node implements IteratorAggregate ...@@ -118,13 +121,16 @@ class Doctrine_Node implements IteratorAggregate
*/ */
public function getIterator($type = null, $options = null) public function getIterator($type = null, $options = null)
{ {
if ($type === null) if ($type === null) {
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre'); $type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
}
if ($options === null) if ($options === null) {
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array()); $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); return new $iteratorClass($this->record, $options);
} }
...@@ -134,7 +140,8 @@ class Doctrine_Node implements IteratorAggregate ...@@ -134,7 +140,8 @@ class Doctrine_Node implements IteratorAggregate
* *
* @param int * @param int
*/ */
public function setIteratorType($type) { public function setIteratorType($type)
{
$this->iteratorType = $type; $this->iteratorType = $type;
} }
...@@ -143,7 +150,8 @@ class Doctrine_Node implements IteratorAggregate ...@@ -143,7 +150,8 @@ class Doctrine_Node implements IteratorAggregate
* *
* @param int * @param int
*/ */
public function setIteratorOptions($options) { public function setIteratorOptions($options)
{
$this->iteratorOptions = $options; $this->iteratorOptions = $options;
} }
} // END class }
\ 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_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface { } class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface
{}
...@@ -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
{}
...@@ -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
{}
...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator ...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
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');
} }
} }
...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator ...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
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');
} }
} }
...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator ...@@ -35,27 +35,33 @@ class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
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');
} }
} }
This diff is collapsed.
...@@ -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
{}
...@@ -60,19 +60,17 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -60,19 +60,17 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
*/ */
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->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
...@@ -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,29 +103,34 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -104,29 +103,34 @@ 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())
{ {
if($this->maxLevel && ($this->level > $this->maxLevel)) while ($current = $this->advanceIndex()) {
if ($this->maxLevel && ($this->level > $this->maxLevel)) {
continue; continue;
}
return $current; return $current;
} }
...@@ -137,28 +141,30 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator ...@@ -137,28 +141,30 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
/** /**
* @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();
......
...@@ -42,7 +42,7 @@ class Doctrine_Tree ...@@ -42,7 +42,7 @@ class Doctrine_Tree
protected $options; 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 object $table instance of Doctrine_Table
* @param array $options options * @param array $options options
...@@ -56,6 +56,7 @@ class Doctrine_Tree ...@@ -56,6 +56,7 @@ class Doctrine_Tree
/** /**
* Used to define table attributes required for the given implementation * Used to define table attributes required for the given implementation
* *
* @throws Doctrine_Tree_Exception if table attributes have not been defined
*/ */
public function setTableDefinition() public function setTableDefinition()
{ {
...@@ -68,7 +69,6 @@ class Doctrine_Tree ...@@ -68,7 +69,6 @@ class Doctrine_Tree
*/ */
public function setUp() public function setUp()
{ {
} }
/** /**
...@@ -78,12 +78,14 @@ class Doctrine_Tree ...@@ -78,12 +78,14 @@ class Doctrine_Tree
* @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
* @throws Doctrine_Exception if class does not extend Doctrine_Tree
*/ */
public static function factory( &$table, $implName, $options = array()) public static function factory(&$table, $implName, $options = array())
{ {
$class = 'Doctrine_Tree_'.$implName; $class = 'Doctrine_Tree_' . $implName;
if(!class_exists($class)) if (!class_exists($class)) {
throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree'); throw new Doctrine_Exception('The chosen class must extend Doctrine_Tree');
}
return new $class($table, $options); return new $class($table, $options);
} }
} // END class }
\ 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_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
{}
...@@ -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 {}
...@@ -49,8 +49,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -49,8 +49,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
*/ */
public function createRoot(Doctrine_Record $record = null) public function createRoot(Doctrine_Record $record = null)
{ {
if(!$record) if (!$record) {
{
$record = $this->table->create(); $record = $this->table->create();
} }
...@@ -73,8 +72,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -73,8 +72,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
->execute()->getFirst(); ->execute()->getFirst();
// if no record is returned, create record // if no record is returned, create record
if(!$root) if (!$root) {
{
$root = $this->table->create(); $root = $this->table->create();
} }
...@@ -83,6 +81,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -83,6 +81,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
return $root; return $root;
} }
/** /**
* 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
* *
...@@ -101,13 +100,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -101,13 +100,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
$root = $tree->getFirst(); $root = $tree->getFirst();
// if no record is returned, create record // if no record is returned, create record
if(!$root) if (!$root) {
{
$root = $this->table->create(); $root = $this->table->create();
} }
if($root->exists()) if ($root->exists()) {
{
// set level to prevent additional query // set level to prevent additional query
$root->getNode()->setLevel(0); $root->getNode()->setLevel(0);
...@@ -123,7 +120,10 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -123,7 +120,10 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
return $root->getNode()->traverse('Pre', $options); 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
* *
...@@ -134,10 +134,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int ...@@ -134,10 +134,11 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int
public function fetchBranch($pk, $options = array()) public function fetchBranch($pk, $options = array())
{ {
$record = $this->table->find($pk); $record = $this->table->find($pk);
if($record->exists()) if ($record->exists()) {
{
$options = array_merge(array('include_record'=>true), $options); $options = array_merge(array('include_record'=>true), $options);
return $record->getNode()->traverse('Pre', $options); return $record->getNode()->traverse('Pre', $options);
} }
// TODO: if record doesn't exist, throw exception or similar?
} }
} }
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