Author: dr
Date: Fri Jul 20 10:25:50 2007
New Revision: 5723

Log:
- Update design and prototype with Thomas' comments.

Modified:
    experimental/Tree/design/design.txt
    experimental/Tree/src/backends/db.php
    experimental/Tree/src/backends/db_parent_child.php
    experimental/Tree/src/backends/xml.php
    experimental/Tree/src/interfaces/data_store.php
    experimental/Tree/src/stores/external_db.php
    experimental/Tree/src/stores/internal_xml.php
    experimental/Tree/src/tree.php
    experimental/Tree/src/tree_node.php
    experimental/Tree/src/tree_node_list_iterator.php

Modified: experimental/Tree/design/design.txt
==============================================================================
--- experimental/Tree/design/design.txt [iso-8859-1] (original)
+++ experimental/Tree/design/design.txt [iso-8859-1] Fri Jul 20 10:25:50 2007
@@ -1,8 +1,8 @@
 eZ Component: Tree, Design
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 :Author: Derick Rethans
-:Revision: $Revision:$
-:Date: $Date:$
+:Revision: $Revision$
+:Date: $Date$
 
 .. contents:: Contents
 
@@ -43,6 +43,9 @@
     $subTree = $node1->fetchSubtree();
     ?>
 
+It is possible to change the ezcTreeNode type to a class of your own, by
+setting the nodeClassName property of the Tree object. The user defined class
+should inherit the ezcTreeNode class though.
 
 
 ezcTreeNodeList
@@ -133,9 +136,9 @@
 ````````````````
 
 Interface that defines the methods that all data stores should implement, such
-as retrieving data for a node ID, or a list of node IDs, storing data by ID
-etc.  Stores are simple to implement, as the interfaces only consists of a few
-types. The component will standard have the following stores.
+as retrieving data for a node, or a list of nodes, storing data for a node etc.
+Stores are simple to implement, as the interfaces only consists of a few types.
+The component will standard have the following stores.
 
 ezcTreeDbDataStore
 ``````````````````

Modified: experimental/Tree/src/backends/db.php
==============================================================================
--- experimental/Tree/src/backends/db.php [iso-8859-1] (original)
+++ experimental/Tree/src/backends/db.php [iso-8859-1] Fri Jul 20 10:25:50 2007
@@ -28,22 +28,10 @@
      */
     public function __construct( ezcDbHandler $dbh, $indexTableName, 
ezcTreeDbDataStore $store )
     {
+        parent::__construct();
         $this->dbh = $dbh;
         $this->indexTableName = $indexTableName;
         $this->properties['store'] = $store;
-    }
-
-    /**
-     * Returns the node identified by the $id.
-     *
-     * @param string $id
-     * @return ezcTreeNode
-     */
-    public function fetchById( $id )
-    {
-        // Obtain data from the store
-        $data = $this->properties['store']->fetchDataById( $id );
-        return new ezcTreeNode( $this, $id, $data );
     }
 
     public function nodeExists( $id )

Modified: experimental/Tree/src/backends/db_parent_child.php
==============================================================================
--- experimental/Tree/src/backends/db_parent_child.php [iso-8859-1] (original)
+++ experimental/Tree/src/backends/db_parent_child.php [iso-8859-1] Fri Jul 20 
10:25:50 2007
@@ -69,17 +69,19 @@
 
     private function addChildNodes( $list, $nodeId )
     {
+        $className = $this->properties['nodeClassName'];
         foreach ( $this->fetchChildRecords( $nodeId ) as $record )
         {
-            $list->addNode( $record['id'], new ezcTreeNode( $this, 
$record['id'] ) );
+            $list->addNode( $record['id'], new $className( $this, 
$record['id'] ) );
             $this->addChildNodes( $list, $record['id'] );
         }
     }
 
     public function fetchSubtree( $nodeId )
     {
+        $className = $this->properties['nodeClassName'];
         $list = new ezcTreeNodeList;
-        $list->addNode( $nodeId, new ezcTreeNode( $this, $nodeId ) );
+        $list->addNode( $nodeId, new $className( $this, $nodeId ) );
         $this->addChildNodes( $list, $nodeId );
         return $list;
     }

Modified: experimental/Tree/src/backends/xml.php
==============================================================================
--- experimental/Tree/src/backends/xml.php [iso-8859-1] (original)
+++ experimental/Tree/src/backends/xml.php [iso-8859-1] Fri Jul 20 10:25:50 2007
@@ -54,6 +54,7 @@
      */
     public function __construct( $xmlFile, ezcTreeXmlDataStore $store )
     {
+        parent::__construct();
         $dom = new DomDocument();
         $dom->load( $xmlFile );
         $dom->relaxNGValidateSource( self::relaxNG );
@@ -82,19 +83,6 @@
     private function saveFile()
     {
         $this->dom->save( $this->xmlFile );
-    }
-
-    /**
-     * Returns the node identified by the $id.
-     *
-     * @param string $id
-     * @return ezcTreeNode
-     */
-    public function fetchById( $id )
-    {
-        // Obtain data from the store
-        $data = $this->properties['store']->fetchDataById( $id );
-        return new ezcTreeNode( $this, $id, $data );
     }
 
     public function nodeExists( $id )
@@ -162,7 +150,7 @@
         $root->setAttributeNode( new DOMAttr( 'id', "id{$node->id}" ) );
         $root->setIdAttribute( 'id', true );
         $document->appendChild( $root );
-        $this->store->storeData( $node->id, $node->data );
+        $this->store->storeDataForNode( $node, $node->data );
         $this->saveFile();
     }
 
@@ -178,7 +166,7 @@
 
         // Append to parent node
         $elem->appendChild( $child );
-        $this->store->storeData( $childNode->id, $childNode->data );
+        $this->store->storeDataForNode( $childNode, $childNode->data );
         $this->saveFile();
     }
 

Modified: experimental/Tree/src/interfaces/data_store.php
==============================================================================
--- experimental/Tree/src/interfaces/data_store.php [iso-8859-1] (original)
+++ experimental/Tree/src/interfaces/data_store.php [iso-8859-1] Fri Jul 20 
10:25:50 2007
@@ -17,7 +17,7 @@
  */
 interface ezcTreeDataStore
 {
-    public function fetchDataById( $id );
-    public function storeData( $id, $data );
+    public function fetchDataForNode( ezcTreeNode $node );
+    public function storeDataForNode( ezcTreeNode $node, $data );
 }
 ?>

Modified: experimental/Tree/src/stores/external_db.php
==============================================================================
--- experimental/Tree/src/stores/external_db.php [iso-8859-1] (original)
+++ experimental/Tree/src/stores/external_db.php [iso-8859-1] Fri Jul 20 
10:25:50 2007
@@ -40,8 +40,9 @@
         $this->dataField = $dataField;
     }
 
-    public function fetchDataById( $id )
+    public function fetchDataForNode( ezcTreeNode $node )
     {
+        $id = $node->id;
         if ( $this->table === null || $this->idField === null )
         {
             throw new Exception( "Not initialized" );
@@ -68,7 +69,7 @@
         return $data[$this->dataField];
     }
 
-    public function storeData( $id, $data )
+    public function storeDataForNode( ezcTreeNode $node, $data )
     {
     }
 }

Modified: experimental/Tree/src/stores/internal_xml.php
==============================================================================
--- experimental/Tree/src/stores/internal_xml.php [iso-8859-1] (original)
+++ experimental/Tree/src/stores/internal_xml.php [iso-8859-1] Fri Jul 20 
10:25:50 2007
@@ -24,14 +24,16 @@
         $this->dom = $dom;
     }
 
-    public function fetchDataById( $id )
+    public function fetchDataForNode( ezcTreeNode $node )
     {
+        $id = $node->id;
         $elem = $this->dom->getElementById( "id$id" );
         return $elem->getElementsByTagNameNS( 
'http://components.ez.no/Tree/data', 'data' )->item(0)->firstChild->data;
     }
 
-    public function storeData( $id, $data )
+    public function storeDataForNode( ezcTreeNode $node, $data )
     {
+        $id = $node->id;
         $elem = $this->dom->getElementById( "id$id" );
         $dataNode = $elem->ownerDocument->createElement( 'etd:data', $data );
         $elem->appendChild( $dataNode );

Modified: experimental/Tree/src/tree.php
==============================================================================
--- experimental/Tree/src/tree.php [iso-8859-1] (original)
+++ experimental/Tree/src/tree.php [iso-8859-1] Fri Jul 20 10:25:50 2007
@@ -10,6 +10,11 @@
      */
     protected $properties = array();
 
+    public function __construct()
+    {
+        $this->properties['nodeClassName'] = 'ezcTreeNode';
+    }
+
     /**
      * Returns the value of the property $name.
      *
@@ -21,6 +26,7 @@
         switch ( $name )
         {
             case 'store':
+            case 'nodeClassName':
                 return $this->properties[$name];
 
         }
@@ -41,6 +47,24 @@
             case 'store':
                 throw new ezcBasePropertyPermissionException( $name, 
ezcBasePropertyPermissionException::READ );
 
+            case 'nodeClassName':
+                if ( !is_string( $propertyValue ) )
+                {
+                    throw new ezcBaseValueException( $propertyName, 
$propertyValue, 'string that contains a class name' );
+                }
+
+                // Check if the passed classname actually implements the
+                // correct parent class. We have to do that with reflection
+                // here unfortunately
+                $parentClass = new ReflectionClass( 'ezcTreeNode' );
+                $handlerClass = new ReflectionClass( $propertyValue );
+                if ( 'ezcMail' !== $propertyValue && 
!$handlerClass->isSubclassOf( $parentClass ) )
+                {
+                    throw new ezcBaseInvalidParentClassException( 
'ezcTreeNode', $propertyValue );
+                }
+                $this->properties[$propertyName] = $propertyValue;
+                break;
+
             default:
                 throw new ezcBasePropertyNotFoundException( $name );
         }
@@ -48,7 +72,23 @@
 
     public function createNode( $id, $data )
     {
-        return new ezcTreeNode( $this, $id, $data );
+        $className = $this->properties['nodeClassName'];
+        return new $className( $this, $id, $data );
+    }
+
+    /**
+     * Returns the node identified by the $id.
+     *
+     * @param string $id
+     * @return ezcTreeNode
+     */
+    public function fetchById( $id )
+    {
+        $className = $this->properties['nodeClassName'];
+        // Obtain data from the store
+        $node = new $className( $this, $id );
+        $node->data = $this->properties['store']->fetchDataForNode( $node );
+        return $node;
     }
 }
 ?>

Modified: experimental/Tree/src/tree_node.php
==============================================================================
--- experimental/Tree/src/tree_node.php [iso-8859-1] (original)
+++ experimental/Tree/src/tree_node.php [iso-8859-1] Fri Jul 20 10:25:50 2007
@@ -99,6 +99,7 @@
 
     public function getChildCount()
     {
+        return $this->tree->getChildCount( $this->id );
     }
 
     public function getPathLength()

Modified: experimental/Tree/src/tree_node_list_iterator.php
==============================================================================
--- experimental/Tree/src/tree_node_list_iterator.php [iso-8859-1] (original)
+++ experimental/Tree/src/tree_node_list_iterator.php [iso-8859-1] Fri Jul 20 
10:25:50 2007
@@ -49,13 +49,12 @@
 
     public function current()
     {
-        $key = key( $this->nodeList );
         $node = current( $this->nodeList );
 
         if ( $node->data === null )
         {
             // fetch the data on the fly
-            $node->data = $this->tree->store->fetchDataById( $key );
+            $node->data = $this->tree->store->fetchDataForNode( $node );
         }
         return $node->data;
     }


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to