Development

Changeset 3520

You must first sign up to be able to contribute.

Changeset 3520

Show
Ignore:
Timestamp:
02/19/07 18:14:05 (2 years ago)
Author:
tristan
Message:

sfPropelActAsNestedSetBehaviorPlugin :

  • Implemented more methods (+ unit tests) :
    • insertAsParentOf
    • retrieveSiblings
    • isEqualTo
    • isChildOf
  • Enhanced internal API
  • Releasing 0.8.0
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelActAsNestedSetBehaviorPlugin/README

    r3519 r3520  
    168168 * `void insertAsNextSiblingOf(BaseObject $dest_node)` : Inserts node as next sibling of given node. 
    169169 * `void insertAsPrevSiblingOf(BaseObject $dest_node)` : Inserts node as previous sibling of given node. 
    170  * `void insertAsParentOf(BaseObject $dest_node)` : '''NOT IMPLEMENTED YET''' 
     170 * `void insertAsParentOf(BaseObject $dest_node)` : Inserts node as parent of given node 
    171171 
    172172=== Informational methods === 
     
    178178 * `bool hasPrevSibling()` : Returns true if given node has a previous sibling. 
    179179 * `bool isLeaf()` : Returns true if given node does not have children. 
    180  * `bool isChildOf()` : '''NOT IMPLEMENTED YET''' 
    181  * `bool isChildOfOrSiblingTo()` : '''NOT IMPLEMENTED YET''' 
     180 * `bool isChildOf()` : Returns true if given node is parent of node. 
    182181 * `integer getNumberOfChildren()` : Returns given node number of direct children. 
    183182 * `integer getNumberOfDescendants()` : Returns given node number of descendants (n level). 
     
    193192 * `BaseObject retrieveLastChild()` : Returns given node last child. 
    194193 * `BaseObject retrieveParent()` : Returns given node parent. 
    195  * `array retrieveSiblings()` : '''NOT IMPLEMENTED YET''' 
     194 * `array retrieveSiblings()` : Returns node siblings. 
    196195 * `array getPath()` : Returns path to a specific node as an array, useful to create breadcrumbs. 
    197196 
     
    209208 * `void makeRoot()` : Sets node properties to make it a root node. 
    210209 * `BaseObject reload()` : Returns an up to date version of node 
     210 * `bool isEqualTo(BaseObject $node)` : Returns true if given node is equivalent to node. 
    211211 
    212212== Changelog == 
     213 
     214=== 2007-02-19 | 0.8.0 === 
     215 
     216Implemented more methods (+ unit tests) : 
     217 
     218 * `insertAsParentOf` 
     219 * `retrieveSiblings` 
     220 * `isEqualTo` 
     221 * `isChildOf` 
    213222 
    214223=== 2007-02-19 | 0.7.0 === 
  • plugins/sfPropelActAsNestedSetBehaviorPlugin/config/config.php

    r3519 r3520  
    6969  array ( 
    7070    'sfPropelActAsNestedSetBehavior', 
     71    'insertAsParentOf' 
     72  ), 
     73  array ( 
     74    'sfPropelActAsNestedSetBehavior', 
    7175    'hasChildren' 
    7276  ), 
     
    106110    'sfPropelActAsNestedSetBehavior', 
    107111    'isLeaf' 
     112  ), 
     113  array ( 
     114    'sfPropelActAsNestedSetBehavior', 
     115    'isEqualTo' 
     116  ), 
     117  array ( 
     118    'sfPropelActAsNestedSetBehavior', 
     119    'isChildOf' 
    108120  ), 
    109121  array ( 
     
    153165  array ( 
    154166    'sfPropelActAsNestedSetBehavior', 
     167    'retrieveSiblings' 
     168  ), 
     169  array ( 
     170    'sfPropelActAsNestedSetBehavior', 
    155171    'getLevel' 
    156172  ), 
  • plugins/sfPropelActAsNestedSetBehaviorPlugin/lib/sfPropelActAsNestedSetBehavior.class.php

    r3519 r3520  
    332332  } 
    333333 
     334  /** 
     335   * Inserts node as parent of given node. 
     336   *  
     337   * @param      BaseObject     $node 
     338   * @param      BaseObject     $dest_node 
     339   * @throws     Exception      When trying to insert node as parent of a root node 
     340   */ 
     341  public function insertAsParentOf(BaseObject $node, BaseObject $dest_node) 
     342  { 
     343    if ($dest_node->isRoot()) 
     344    { 
     345      $msg = 'Impossible to insert a node as parent of a root node'; 
     346      throw new Exception($msg); 
     347    } 
     348 
     349    $peer_name = get_class($node->getPeer()); 
     350 
     351    $this->addPreSaveStackEntries(self::shiftRLValues($peer_name, $dest_node->getLeftValue(), 1, $dest_node->getScopeIdValue())); 
     352    $this->addPreSaveStackEntries(self::shiftRLValues($peer_name, $dest_node->getRightValue() + 2, -1, $dest_node->getScopeIdValue())); 
     353     
     354    $node->setLeftValue($dest_node->getLeftValue()); 
     355    $node->setRightValue($dest_node->getRightValue() + 2); 
     356 
     357    $previous_parent = $dest_node->getParentIdValue(); 
     358    $node->setParentIdValue($previous_parent); 
     359    $dest_node->setParentIdValue($node->getPrimaryKey()); 
     360 
     361    $this->addPreSaveStackEntries(array($dest_node)); 
     362     
     363  } 
     364 
    334365# ---- INFORMATIONAL METHODS 
    335366 
     
    398429  { 
    399430    return $node->getRightValue() - $node->getLeftValue() == 1; 
     431  } 
     432 
     433  /** 
     434   * Returns true if given node is identical to node. 
     435   *  
     436   * @param      BaseObject      $node 
     437   * @param      BaseObject      $compared_node 
     438   * @return     bool 
     439   */ 
     440  public function isEqualTo(BaseObject $node, BaseObject $compared_node) 
     441  { 
     442    return ($node->getLeftValue()        === $compared_node->getLeftValue() 
     443            && $node->getRightValue()    === $compared_node->getRightValue() 
     444            && $node->getScopeIdValue()  === $compared_node->getScopeIdValue() 
     445            && $node->getParentIdValue() === $compared_node->getParentIdValue()); 
     446  } 
     447 
     448  /** 
     449   * Returns true if given node is parent of node. 
     450   *  
     451   * @param      BaseObject      $node 
     452   * @param      BaseObject      $parent_node 
     453   */ 
     454  public function isChildOf(BaseObject $node, BaseObject $parent_node) 
     455  { 
     456    return ($node->getParentIdValue() === $parent_node->getPrimaryKey() 
     457            && $node->getScopeIdValue() === $parent_node->getScopeIdValue()); 
    400458  } 
    401459 
     
    598656  } 
    599657 
     658  /** 
     659   * Returns node siblings. 
     660   *  
     661   * @param      BaseObject     $node 
     662   * @param      string         $peer_method    (optional) defaults to "doSelect" 
     663   * @return     array 
     664   */ 
     665  public function retrieveSiblings(BaseObject $node, $peer_method = 'doSelect') 
     666  { 
     667    $c = new Criteria(); 
     668    $c->add(self::getColumnConstant(get_class($node), 'parent'), $node->getParentIdValue()); 
     669    $c->add(self::getColumnConstant(get_class($node), 'scope'), $node->getScopeIdValue()); 
     670     
     671    $results = call_user_func(array($node->getPeer(), $peer_method), $c); 
     672    $final_results = array(); 
     673    if (is_array($results)) 
     674    { 
     675      foreach ($results as $r) 
     676      { 
     677        if ($node->isEqualTo($r)) 
     678        { 
     679          continue; 
     680        } 
     681        $final_results[] = $r; 
     682      } 
     683    } 
     684     
     685    return $final_results; 
     686  } 
    600687 
    601688  /** 
     
    631718    $node->setParentIdValue($dest_node->getPrimaryKey()); 
    632719    $node->setScopeIdValue($dest_node->getScopeIdValue()); 
    633     $this->addPreSaveStackEntries($this->getUpdateTreeQueries($node, $dest_node->getLeftValue() + 1)); 
     720    $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getLeftValue() + 1)); 
    634721  } 
    635722 
     
    644731    $node->setParentIdValue($dest_node->getPrimaryKey()); 
    645732    $node->setScopeIdValue($dest_node->getScopeIdValue()); 
    646     $this->addPreSaveStackEntries($this->getUpdateTreeQueries($node, $dest_node->getRightValue())); 
     733    $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getRightValue())); 
    647734  } 
    648735 
     
    657744    $node->setParentIdValue($dest_node->getParentIdValue()); 
    658745    $node->setScopeIdValue($dest_node->getScopeIdValue()); 
    659     $this->addPreSaveStackEntries($this->getUpdateTreeQueries($node, $dest_node->getRightValue() + 1)); 
     746    $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getRightValue() + 1)); 
    660747  } 
    661748 
     
    670757    $node->setParentIdValue($dest_node->getParentIdValue()); 
    671758    $node->setScopeIdValue($dest_node->getScopeIdValue()); 
    672     $this->addPreSaveStackEntries($this->getUpdateTreeQueries($node, $dest_node->getLeftValue())); 
     759    $this->addPreSaveStackEntries(self::getUpdateTreeQueries($node, $dest_node->getLeftValue())); 
    673760  } 
    674761 
     
    9141001   * @return array 
    9151002   */ 
    916   private function getUpdateTreeQueries(BaseObject $node, $dest_left) 
     1003  private static function getUpdateTreeQueries(BaseObject $node, $dest_left) 
    9171004  { 
    9181005    $statements = array(); 
  • plugins/sfPropelActAsNestedSetBehaviorPlugin/package.xml

    r3519 r3520  
    1313 <date>2007-02-19</date> 
    1414 <version> 
    15    <release>0.7.0</release> 
    16    <api>0.7.0</api> 
     15   <release>0.8.0</release> 
     16   <api>0.8.0</api> 
    1717 </version> 
    1818 <stability> 
     
    5353 
    5454 <changelog> 
     55 
     56  <release> 
     57   <version> 
     58    <release>0.8.0</release> 
     59    <api>0.8.0</api> 
     60   </version> 
     61   <stability> 
     62    <release>beta</release> 
     63    <api>beta</api> 
     64   </stability> 
     65   <date>2007-02-19</date> 
     66   <license uri="http://www.symfony-project.com/license">MIT license</license> 
     67   <notes> 
     68Implemented more methods (+ unit tests) : 
     69  
     70 * `insertAsParentOf` 
     71 * `retrieveSiblings` 
     72 * `isEqualTo` 
     73 * `isChildOf` 
     74   </notes> 
     75  </release> 
    5576 
    5677  <release> 
  • plugins/sfPropelActAsNestedSetBehaviorPlugin/test/unit/PropelActAsNestedSetBehaviorTest.php

    r3519 r3520  
    8282 
    8383// insertAsFirstChildOf 
    84 $t = new lime_test(24, new lime_output_color()); 
     84$t = new lime_test(6, new lime_output_color()); 
    8585$t->diag('insertAsFirstChildOf'); 
    8686$t->diag('insert N1 as first child of ROOT'); 
     
    9898$t->is($n1->getParentIdValue(), $root->getId(), 'N1 parent id equals ROOT id'); 
    9999$t->is($n1->getScopeIdValue(), $root->getScopeIdValue(), 'N1 scope id equals ROOT id'); 
     100 
     101// isEqualTo 
     102$t = new lime_test(2, new lime_output_color()); 
     103$t->is($n1->isEqualTo($n1), true, 'isEqualTo() returns true when nodes are equivalent'); 
     104$t->is($root->isEqualTo($n1), false, 'isEqualTo() returns false when nodes are different'); 
    100105 
    101106$t->diag('insert N2 as first child of ROOT'); 
     
    611616$t->is($n6->getParentIdValue(), $n8->getPrimaryKey(), 'N6 parent id equals N8 id'); 
    612617$t->is($n6->getScopeIdValue(), $n8->getScopeIdValue(), 'N6 scope id equals N8 scope id'); 
     618 
     619// retrieveSiblings 
     620$t = new lime_test(15, new lime_output_color()); 
     621$t->diag('retrieveSiblings'); 
     622$t->diag('Retrieve siblings of N7'); 
     623$expected = array($n3->getId(), $n4->getId()); 
     624$got = array(); 
     625foreach ($n7->retrieveSiblings() as $node) 
     626{ 
     627  $got[] = $node->getId(); 
     628} 
     629 
     630$t->is($got, $expected, 'retrieveSiblings() returns the right siblings'); 
    613631 
    614632// delete 
     
    837855 
    838856// deleteDescendants 
    839 $t = new lime_test(22, new lime_output_color()); 
     857$t = new lime_test(14, new lime_output_color()); 
    840858$t->diag('deleteDescendants'); 
    841859$t->diag('Delete N8 descendants'); 
     
    879897$t->is($n8->getScopeIdValue(), $root->getScopeIdValue(), 'N8 scope id equals ROOT scope id'); 
    880898 
     899// insertAsParentOf 
     900$t = new lime_test(15, new lime_output_color()); 
     901$t->diag('insertAsParentOf'); 
     902$t->diag('Insert N8 as parent of N4'); 
     903$t->diag('root'); 
     904$t->diag(' |-N8'); 
     905$t->diag('   |-N4'); 
     906$t->diag(' |-N7'); 
     907 
     908$n8->insertAsParentOf($n4); 
     909$n8->save(); 
     910 
     911// -- reload nodes 
     912$n4 = $n4->reload(); 
     913$n7 = $n7->reload(); 
     914$n8 = $n8->reload(); 
     915$root = $root->reload(); 
     916 
     917$t->is($root->getLeftValue(), 1, 'root "left" is "1"'); 
     918$t->is($root->getRightValue(), 8, 'root "right" is "8"'); 
     919$t->is($n4->getLeftValue(), 3, 'N4 "left" is "3"'); 
     920$t->is($n4->getRightValue(), 4, 'N4 "right" is "4"'); 
     921$t->is($n4->getParentIdValue(), $n8->getPrimaryKey(), 'N4 parent is N8'); 
     922$t->is($n4->getScopeIdValue(), $n8->getScopeIdValue(), 'N4 scope id equals N8 scope id'); 
     923$t->is($n7->getLeftValue(), 6, 'N7 "left" is "6"'); 
     924$t->is($n7->getRightValue(), 7, 'N7 "right" is "7"'); 
     925$t->is($n7->getParentIdValue(), $root->getPrimaryKey(), 'N7 parent is ROOT'); 
     926$t->is($n7->getScopeIdValue(), $root->getScopeIdValue(), 'N7 scope id equals ROOT scope id'); 
     927$t->is($n8->getLeftValue(), 2, 'N8 "left" is "2"'); 
     928$t->is($n8->getRightValue(), 5, 'N8 "right" is "5"'); 
     929$t->is($n8->getParentIdValue(), $root->getPrimaryKey(), 'N8 parent is ROOT'); 
     930$t->is($n8->getScopeIdValue(), $root->getScopeIdValue(), 'N8 scope id equals ROOT scope id'); 
     931 
     932try 
     933{ 
     934  $n8->insertAsParentOf($root); 
     935  $t->fail('insertAsParentOf() throws an exception when asked to insert a node as parent of a root node'); 
     936} 
     937catch (Exception $e) 
     938{ 
     939  $t->pass('insertAsParentOf() throws an exception when asked to insert a node as parent of a root node'); 
     940} 
     941 
     942// isChildOf 
     943$t = new lime_test(2, new lime_output_color()); 
     944$t->diag('isChildOf'); 
     945$t->is($n4->isChildOf($n8), true, 'isChildOf() returns true when given node is node parent'); 
     946$t->is($n4->isChildOf($root), false, 'isChildOf() returns false when given node is not node parent'); 
     947 
    881948// Helper functions 
    882949