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 @@
*/
class Doctrine_Node implements IteratorAggregate
{
/**
* @param object $record reference to associated Doctrine_Record instance
*/
protected $record;
/**
* @param array $options
*/
protected $options;
/**
* @param string $iteratorType (Pre | Post | Level)
*/
protected $iteratorType;
/**
* @param array $iteratorOptions
*/
protected $iteratorOptions;
/**
* contructor, creates node with reference to record and any options
*
* @param object $record instance of Doctrine_Record
* @param array $options options
*/
public function __construct(&$record, $options)
{
$this->record = $record;
$this->options = $options;
}
/**
* factory method to return node instance based upon chosen implementation
*
* @param object $record instance of Doctrine_Record
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
* @param array $options options
* @return object $options instance of Doctrine_Node
*/
public static function factory(&$record, $implName, $options = array()) {
$class = 'Doctrine_Node_'.$implName;
if(!class_exists($class))
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
return new $class($record, $options);
}
/**
* setter for record attribute
*
* @param object $record instance of Doctrine_Record
*/
public function setRecord(&$record)
{
$this->record = $record;
}
/**
* getter for record attribute
*
* @return object instance of Doctrine_Record
*/
public function getRecord() {
return $this->record;
}
/**
* convenience function for getIterator
*
* @param string $type type of iterator (Pre | Post | Level)
* @param array $options 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
*/
public function getIterator($type = null, $options = null)
{
if ($type === null)
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
if ($options === null)
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
$iteratorClass = 'Doctrine_Node_'.$this->record->getTable()->getTreeImplName().'_'.ucfirst(strtolower($type)).'OrderIterator';
return new $iteratorClass($this->record, $options);
}
/**
* sets node's iterator type
*
* @param int
*/
public function setIteratorType($type) {
$this->iteratorType = $type;
}
/**
* sets node's iterator options
*
* @param int
*/
public function setIteratorOptions($options) {
$this->iteratorOptions = $options;
}
} // END class
\ No newline at end of file
/**
* @param object $record reference to associated Doctrine_Record instance
*/
protected $record;
/**
* @param array $options
*/
protected $options;
/**
* @param string $iteratorType (Pre | Post | Level)
*/
protected $iteratorType;
/**
* @param array $iteratorOptions
*/
protected $iteratorOptions;
/**
* contructor, creates node with reference to record and any options
*
* @param object $record instance of Doctrine_Record
* @param array $options options
*/
public function __construct(&$record, $options)
{
$this->record = $record;
$this->options = $options;
}
/**
* factory method to return node instance based upon chosen implementation
*
* @param object $record instance of Doctrine_Record
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
* @param array $options options
* @return object $options instance of Doctrine_Node
*/
public static function factory(&$record, $implName, $options = array())
{
$class = 'Doctrine_Node_' . $implName;
if (!class_exists($class)) {
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
}
return new $class($record, $options);
}
/**
* setter for record attribute
*
* @param object $record instance of Doctrine_Record
*/
public function setRecord(&$record)
{
$this->record = $record;
}
/**
* getter for record attribute
*
* @return object instance of Doctrine_Record
*/
public function getRecord()
{
return $this->record;
}
/**
* convenience function for getIterator
*
* @param string $type type of iterator (Pre | Post | Level)
* @param array $options 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
*/
public function getIterator($type = null, $options = null)
{
if ($type === null) {
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
}
if ($options === null) {
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
}
$implName = $this->record->getTable()->getTreeImplName();
$iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
return new $iteratorClass($this->record, $options);
}
/**
* sets node's iterator type
*
* @param int
*/
public function setIteratorType($type)
{
$this->iteratorType = $type;
}
/**
* sets node's iterator options
*
* @param int
*/
public function setIteratorOptions($options)
{
$this->iteratorOptions = $options;
}
}
......@@ -29,5 +29,6 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface { }
\ No newline at end of file
class Doctrine_Node_AdjacencyList extends Doctrine_Node implements Doctrine_Node_Interface
{}
......@@ -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
{}
......@@ -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
{}
......@@ -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
{}
......@@ -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
{}
This diff is collapsed.
......@@ -29,5 +29,6 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface { }
\ No newline at end of file
class Doctrine_Node_MaterializedPath extends Doctrine_Node implements Doctrine_Node_Interface
{}
......@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_LevelOrderIterator implements Iterator
{
private $topNode = null;
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');
}
}
}
......@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_PostOrderIterator implements Iterator
{
private $topNode = null;
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');
}
}
}
......@@ -32,30 +32,36 @@
class Doctrine_Node_MaterializedPath_PreOrderIterator implements Iterator
{
private $topNode = null;
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');
}
}
}
This diff is collapsed.
......@@ -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
{}
......@@ -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
{}
......@@ -30,9 +30,9 @@
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
{
{
/**
* @var Doctrine_Collection $collection
* @var Doctrine_Collection $collection
*/
protected $collection;
/**
......@@ -59,34 +59,32 @@ class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
* @var integer $count
*/
protected $count;
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');
}
$this->maxLevel = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
$this->options = $opts;
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
$this->keys = $this->collection->getKeys();
$this->count = $this->collection->count();
$this->index = -1;
$this->level = $record->getNode()->getLevel();
$this->prevLeft = $record->getNode()->getLeftValue();
echo $this->maxDepth;
// clear the table identity cache
$record->getTable()->clear();
public function __construct($record, $opts)
{
$componentName = $record->getTable()->getComponentName();
$q = $record->getTable()->createQuery();
$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;
$this->options = $opts;
$this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
$this->keys = $this->collection->getKeys();
$this->count = $this->collection->count();
$this->index = -1;
$this->level = $record->getNode()->getLevel();
$this->prevLeft = $record->getNode()->getLeftValue();
echo $this->maxDepth;
// clear the table identity cache
$record->getTable()->clear();
}
/**
......@@ -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,66 +103,73 @@ 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())
{
if($this->maxLevel && ($this->level > $this->maxLevel))
continue;
return $current;
}
return false;
public function next()
{
while ($current = $this->advanceIndex()) {
if ($this->maxLevel && ($this->level > $this->maxLevel)) {
continue;
}
return $current;
}
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);
}
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))
{
$left = $this->collection->get($this->key)->getNode()->getLeftValue();
$this->level += $this->prevLeft - $left + 2;
$this->prevLeft = $left;
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();
return $this->current();
}
return false;
}
return false;
}
}
......@@ -29,61 +29,63 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Tree
class Doctrine_Tree
{
/**
* @param object $table reference to associated Doctrine_Table instance
*/
protected $table;
/**
* @param array $options
*/
protected $options;
/**
* @param object $table reference to associated Doctrine_Table instance
*/
protected $table;
/**
* contructor, creates tree with reference to table and any options
*
* @param object $table instance of Doctrine_Table
* @param array $options options
*/
public function __construct($table, $options)
{
$this->table = $table;
$this->options = $options;
}
/**
* @param array $options
*/
protected $options;
/**
* Used to define table attributes required for the given implementation
*
*/
public function setTableDefinition()
{
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
}
/**
* constructor, creates tree with reference to table and any options
*
* @param object $table instance of Doctrine_Table
* @param array $options options
*/
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
*
*/
public function setUp()
{
}
/**
* Used to define table attributes required for the given implementation
*
* @throws Doctrine_Tree_Exception if table attributes have not been defined
*/
public function setTableDefinition()
{
throw new Doctrine_Tree_Exception('Table attributes have not been defined for this Tree implementation.');
}
/**
* factory method to return tree instance based upon chosen implementation
*
* @param object $table instance of Doctrine_Table
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
* @param array $options options
* @return object $options instance of Doctrine_Node
*/
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);
}
} // END class
\ No newline at end of file
/**
* this method is used for setting up relations and attributes and should be used by specific implementations
*
*/
public function setUp()
{
}
/**
* factory method to return tree instance based upon chosen implementation
*
* @param object $table instance of Doctrine_Table
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
* @param array $options options
* @return object $options instance of Doctrine_Node
* @throws Doctrine_Exception if class does not extend Doctrine_Tree
*/
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 @@
* @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
{}
......@@ -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
{}
......@@ -31,34 +31,34 @@
*/
interface Doctrine_Tree_Interface {
/**
* 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);
/**
* 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);
/**
* returns root node
*
* @return object $record instance of Doctrine_Record
*/
public function findRoot();
/**
* returns root node
*
* @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_<Implementation>_PreOrderIterator
*/
public function fetchTree($options = array());
/**
* optimised method to returns iterator for traversal of the entire tree from root
*
* @param array $options options
* @return object $iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/
public function fetchTree($options = array());
/**
* 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 array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/
public function fetchBranch($pk, $options = array());
}
\ No newline at end of file
/**
* 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 array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/
public function fetchBranch($pk, $options = array());
}
......@@ -29,4 +29,5 @@
* @version $Revision$
* @author Joe Simms <joe.simms@websites4.com>
*/
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface { }
\ No newline at end of file
class Doctrine_Tree_MaterializedPath extends Doctrine_Tree implements Doctrine_Tree_Interface
{}
......@@ -31,113 +31,114 @@
*/
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
{
/**
* used to define table attributes required for the NestetSet implementation
* adds lft and rgt columns for corresponding left and right values
*
*/
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)
/**
* used to define table attributes required for the NestetSet implementation
* adds lft and rgt columns for corresponding left and right values
*
*/
public function setTableDefinition()
{
$record = $this->table->create();
$this->table->setColumn("lft","integer",11);
$this->table->setColumn("rgt","integer",11);
}
$record->set('lft', '1');
$record->set('rgt', '2');
$record->save();
return $record;
}
/**
* 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)
/**
* 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)
{
$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);
return $root;
}
/**
* 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)
/**
* returns root node
*
* @return object $record instance of Doctrine_Record
*/
public function findRoot()
{
$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
$root->getNode()->setLevel(0);
// fetch tree
$q = $this->table->createQuery();
$tree = $q->where('lft >= ?', 1)
->orderBy('lft asc')
->execute();
// 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);
$root = $tree->getFirst();
// set collection for iterator
$options['collection'] = $tree;
// if no record is returned, create record
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
*
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
* @param array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/
public function fetchBranch($pk, $options = array())
{
$record = $this->table->find($pk);
if($record->exists())
/**
* 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 array $options options
* @return iterator instance of Doctrine_Node_<Implementation>_PreOrderIterator
*/
public function fetchBranch($pk, $options = array())
{
$options = array_merge(array('include_record'=>true), $options);
return $record->getNode()->traverse('Pre', $options);
$record = $this->table->find($pk);
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