Tree: ezcTree
[ ]
[ ]
[ ]
[ ]
[ ]
Class: ezcTree
|
ezcTree is an abstract class from which all the tree implementations inherit. [
source]
Implemented Interfaces
Example:
1. <?php
2. // Instantiating an existing tree, and creating a new tree is done through
3. // the inherited classes
4.
5. // Creating a new in-memory tree
6. $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
7.
8. // Opening an existing tree in an XML file
9. $tree = new ezcTreeXml( 'test.xml', new ezcTreeXmlInternalDataStore() );
10.
11. // Opening an existing tree from a database, using a nested set backend
12. // - This retrieves data from the ezcTreeDbExternalTableDataStore store
13. // using $this->dbh as database handle, $dataTable as table where to fetch
14. // data from using 'id' as ID field.
15. $store = new ezcTreeDbExternalTableDataStore( $this->dbh, $dataTable, 'id' );
16. // - It uses the 'nested_set' table for keeping the tree structure
17. $tree = new ezcTreeDbNestedSet( $this->dbh, 'nested_set', $store );
18.
19. // Fetching nodes and subtrees
20. $node = $tree->fetchNodeById( 'He' );
21. $nodeList = $tree->fetchSubtree( 'Pantherinae' );
22.
23. // Checking for relations between nodes
24. $tree->isDescendantOf( 'Tiger', 'Panthera' );
25. $tree->isSiblingOf( 'Lion', 'Leopard' );
26. ?>
Descendents
| Child Class |
Description |
| ezcTreeXml |
ezcTreeXml is an implementation of a tree backend that operates on an XML file.
|
| ezcTreeMemory |
ezcTreeMemory is an implementation of a tree backend that operates on an in-memory tree structure. Meta-information is kept in objects of the ezcTreeMemoryNode class.
|
| ezcTreeDb |
ezcTreeDb contains common methods for the different database tree backends.
|
Properties
|
boolean |
read/write
|
$autoId
When set to true, you can add nodes to the database without setting the ID. This only works with numeric keys however. |
|
string |
read/write
|
$nodeClassName
Which class is used as tree node - this class *must* inherit the ezcTreeNode class. |
|
ezcTreeXmlDataStore |
read
|
$store
The data store that is used for retrieving/storing data. |
Member Variables
|
protected bool |
$inTransaction
= false
Contains whether a transaction has been started. |
|
protected bool |
$inTransactionCommit
= false
Contains whether we are in a transaction commit stage. |
|
protected array(string=>mixed) |
$properties
= array( 'nodeClassName' => 'ezcTreeNode' )
Holds the properties of this class. |
Method Summary
|
public void |
accept(
$visitor )
Implements the accept method for visiting. |
|
public abstract void |
addChild(
$parentId, $childNode )
Adds the node $childNode as child of the node with ID $parentId. |
|
protected void |
addTransactionItem(
$item )
Adds a new transaction item to the list. |
|
public void |
beginTransaction(
)
Starts an transaction in which all tree modifications are queued until the transaction is committed with the commit() method. |
|
protected void |
checkNodeId(
$nodeId )
This method checks whether a node ID is valid to be used in a backend. |
|
public void |
commit(
)
Commits the transaction by running the stored instructions to modify the tree structure. |
|
public static void |
copy(
$from, $to )
Copies the tree in $from to the empty tree in $to. |
|
public ezcTreeNode |
createNode(
$nodeId, $data )
Creates a new tree node with node ID $nodeId and $data. |
|
public abstract void |
delete(
$nodeId )
Deletes the node with ID $nodeId from the tree, including all its children. |
|
public abstract ezcTreeNodeList |
fetchChildren(
$nodeId )
Returns all the children of the node with ID $nodeId. |
|
public ezcTreeNode |
fetchNodeById(
$nodeId )
Returns the node identified by the ID $nodeId. |
|
public abstract ezcTreeNode |
fetchParent(
$nodeId )
Returns the parent node of the node with ID $nodeId. |
|
public abstract ezcTreeNodeList |
fetchPath(
$nodeId )
Returns all the nodes in the path from the root node to the node with ID $nodeId, including those two nodes. |
|
public abstract ezcTreeNodeList |
fetchSubtree(
$nodeId )
Alias for fetchSubtreeDepthFirst(). |
|
public abstract ezcTreeNodeList |
fetchSubtreeBreadthFirst(
$nodeId )
Returns the node with ID $nodeId and all its children, sorted according to the Breadth-first sorting algorithm. |
|
public abstract ezcTreeNodeList |
fetchSubtreeDepthFirst(
$nodeId )
Returns the node with ID $nodeId and all its children, sorted according to the Depth-first sorting algorithm. |
|
protected abstract integer |
generateNodeID(
)
This method generates the next node ID. |
|
public abstract int |
getChildCount(
$nodeId )
Returns the number of direct children of the node with ID $nodeId. |
|
public abstract int |
getChildCountRecursive(
$nodeId )
Returns the number of children of the node with ID $nodeId, recursively. |
|
public abstract int |
getPathLength(
$nodeId )
Returns the distance from the root node to the node with ID $nodeId. |
|
public abstract ezcTreeNode |
getRootNode(
)
Returns the root node. |
|
public abstract bool |
hasChildNodes(
$nodeId )
Returns whether the node with ID $nodeId has children. |
|
public bool |
inTransaction(
)
Returns whether we are currently in a transaction or not |
|
public bool |
inTransactionCommit(
)
Returns whether we are currently in a transaction commit state or not |
|
public abstract bool |
isChildOf(
$childId, $parentId )
Returns whether the node with ID $childId is a direct child of the node with ID $parentId. |
|
public abstract bool |
isDescendantOf(
$childId, $parentId )
Returns whether the node with ID $childId is a direct or indirect child of the node with ID $parentId. |
|
public abstract bool |
isSiblingOf(
$child1Id, $child2Id )
Returns whether the nodes with IDs $child1Id and $child2Id are siblings (ie, they share the same parent). |
|
public abstract void |
move(
$nodeId, $targetParentId )
Moves the node with ID $nodeId as child to the node with ID $targetParentId. |
|
public abstract bool |
nodeExists(
$nodeId )
Returns whether the node with ID $nodeId exists. |
|
public void |
rollback(
)
Rolls back the transaction by clearing all stored instructions. |
|
public abstract void |
setRootNode(
$node )
Sets a new node as root node, this also wipes out the whole tree. |
Methods
accept
Implements the accept method for visiting.
Parameters
addChild
void addChild(
string
$parentId,
ezcTreeNode
$childNode )
Adds the node $childNode as child of the node with ID $parentId.
Parameters
| Name |
Type |
Description |
$parentId |
string |
|
$childNode |
ezcTreeNode |
|
Redefined in descendants as
addTransactionItem
void addTransactionItem(
$item )
Adds a new transaction item to the list.
Parameters
| Name |
Type |
Description |
$item |
ezcTreeTransactionItem |
|
beginTransaction
void beginTransaction(
)
Starts an transaction in which all tree modifications are queued until the transaction is committed with the commit() method.
checkNodeId
void checkNodeId(
string
$nodeId )
This method checks whether a node ID is valid to be used in a backend.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Throws
| Class | Description |
ezcTreeInvalidNodeIDException |
if the node is not valid. |
Redefined in descendants as
commit
void commit(
)
Commits the transaction by running the stored instructions to modify the tree structure.
copy
Copies the tree in $from to the empty tree in $to.
This method copies all the nodes, including associated data from the used data store, from the tree $from to the tree $to. Because this function uses internally setRootNode() the target tree will be cleared out automatically. The method will not check whether the $from and $to trees share the same database table or data store, so make sure they are different to prevent unexpected behavior.
Parameters
createNode
Creates a new tree node with node ID $nodeId and $data.
This method returns by default an object of the ezcTreeNode class, however if a replacement is configured through the nodeClassName property an object of that class is returned instead. This object is guaranteed to inherit from ezcTreeNode.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
$data |
mixed |
|
delete
void delete(
string
$nodeId )
Deletes the node with ID $nodeId from the tree, including all its children.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchChildren
Returns all the children of the node with ID $nodeId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchNodeById
Returns the node identified by the ID $nodeId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Throws
| Class | Description |
ezcTreeUnknownIdException |
if there is no node with ID $nodeId |
Redefined in descendants as
fetchParent
Returns the parent node of the node with ID $nodeId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchPath
Returns all the nodes in the path from the root node to the node with ID $nodeId, including those two nodes.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchSubtree
Alias for fetchSubtreeDepthFirst().
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchSubtreeBreadthFirst
Returns the node with ID $nodeId and all its children, sorted according to the
Breadth-first sorting algorithm.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
fetchSubtreeDepthFirst
Returns the node with ID $nodeId and all its children, sorted according to the
Depth-first sorting algorithm.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
generateNodeID
integer generateNodeID(
)
This method generates the next node ID.
Redefined in descendants as
getChildCount
int getChildCount(
string
$nodeId )
Returns the number of direct children of the node with ID $nodeId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
getChildCountRecursive
int getChildCountRecursive(
string
$nodeId )
Returns the number of children of the node with ID $nodeId, recursively.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
getPathLength
int getPathLength(
string
$nodeId )
Returns the distance from the root node to the node with ID $nodeId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
getRootNode
Returns the root node.
Redefined in descendants as
hasChildNodes
bool hasChildNodes(
string
$nodeId )
Returns whether the node with ID $nodeId has children.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
inTransaction
bool inTransaction(
)
Returns whether we are currently in a transaction or not
inTransactionCommit
bool inTransactionCommit(
)
Returns whether we are currently in a transaction commit state or not
isChildOf
bool isChildOf(
string
$childId, string
$parentId )
Returns whether the node with ID $childId is a direct child of the node with ID $parentId.
Parameters
| Name |
Type |
Description |
$childId |
string |
|
$parentId |
string |
|
Redefined in descendants as
isDescendantOf
bool isDescendantOf(
string
$childId, string
$parentId )
Returns whether the node with ID $childId is a direct or indirect child of the node with ID $parentId.
Parameters
| Name |
Type |
Description |
$childId |
string |
|
$parentId |
string |
|
Redefined in descendants as
isSiblingOf
bool isSiblingOf(
string
$child1Id, string
$child2Id )
Returns whether the nodes with IDs $child1Id and $child2Id are siblings (ie, they share the same parent).
Parameters
| Name |
Type |
Description |
$child1Id |
string |
|
$child2Id |
string |
|
Redefined in descendants as
move
void move(
string
$nodeId, string
$targetParentId )
Moves the node with ID $nodeId as child to the node with ID $targetParentId.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
$targetParentId |
string |
|
Redefined in descendants as
nodeExists
bool nodeExists(
string
$nodeId )
Returns whether the node with ID $nodeId exists.
Parameters
| Name |
Type |
Description |
$nodeId |
string |
|
Redefined in descendants as
rollback
void rollback(
)
Rolls back the transaction by clearing all stored instructions.
setRootNode
Sets a new node as root node, this also wipes out the whole tree.
Parameters
Redefined in descendants as
Last updated: Mon, 09 Feb 2009