cvs: pear /HTML_QuickForm2/QuickForm2 Container.php /HTML_QuickForm2/tests/QuickForm2 ContainerTest.php
| From: | Alexey Borzov | Date: | Thu, 12 Oct 2006 21:24:29 +0000 |
| Subject: | cvs: pear /HTML_QuickForm2/QuickForm2 Container.php /HTML_QuickForm2/tests/QuickForm2 ContainerTest.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-42510@lists.php.net to get a copy of this message | ||
avb Thu Oct 12 21:24:29 2006 UTC
Modified files:
/pear/HTML_QuickForm2/QuickForm2 Container.php
/pear/HTML_QuickForm2/tests/QuickForm2 ContainerTest.php
Log:
* removed id cache from Container, getElementById() now iterates over elements
* frozen status and persistent freeze behaviour now propagate to contained elements
http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Container.php?r1=1.8&r2=1.9&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Container.php diff -u pear/HTML_QuickForm2/QuickForm2/Container.php:1.8 pear/HTML_QuickForm2/QuickForm2/Container.php:1.9 --- pear/HTML_QuickForm2/QuickForm2/Container.php:1.8 Sat Oct 7 09:10:55 2006 +++ pear/HTML_QuickForm2/QuickForm2/Container.php Thu Oct 12 21:24:29 2006 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Container.php,v 1.8 2006/10/07 09:10:55 mansion Exp $ + * @version CVS: $Id: Container.php,v 1.9 2006/10/12 21:24:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -68,14 +68,6 @@ protected $elements = array(); /** - * Array of contained ids - * Format: element_id => key of child container - * @var array - */ - protected $idIndex = array(); - - - /** * 'name' and 'id' attributes should be always present and their setting * should go through setName() and setId(). * @var array @@ -128,6 +120,25 @@ $this->attributes['id'] = (string)$id; } + public function toggleFrozen($freeze = null) + { + if (null !== $freeze) { + foreach ($this as $child) { + $child->toggleFrozen($freeze); + } + } + return parent::toggleFrozen($freeze); + } + + public function persistentFreeze($persistent = null) + { + if (null !== $persistent) { + foreach ($this as $child) { + $child->persistentFreeze($persistent); + } + } + return parent::persistentFreeze($persistent); + } @@ -150,26 +161,15 @@ * * @param HTML_QuickForm2_AbstractElement Element to add * @return HTML_QuickForm2_AbstractElement Added element + * @throws HTML_QuickForm2_InvalidArgumentException */ public function addElement(HTML_QuickForm2_AbstractElement $element) { - if (null !== ($container = $element->getContainer())) { - $container->removeChild($element); + if ($this === $element->getContainer()) { + $this->removeChild($element); } $element->setContainer($this); - $this->elements[] = $element; - end($this->elements); - $current = key($this->elements); - $this->setChildIndex($element->getId(), $current); - - if ($element instanceof HTML_QuickForm2_Container && - count($element) > 0) { - while (list($k) = each($element->idIndex)) { - $this->setChildIndex($k, $current); - } - } - return $element; } @@ -183,45 +183,22 @@ */ public function removeChild(HTML_QuickForm2_AbstractElement $element) { - if ($element->getContainer() === $this) { - $id = $element->getId(); - $index = $this->getChildIndex($id); - $this->removeChildIndex($id); - unset($this->elements[$index]); - $element->setContainer(null); - return $element; - } - throw new HTML_QuickForm2_NotFoundException( - "Element with name '".$element->getName()."' was not found" - ); - } - - protected function setChildIndex($id, $index) - { - $this->idIndex[$id] = $index; - if (null != ($parent = $this->getContainer())) { - $indexInParent = $parent->idIndex[$this->getId()]; - $parent->setChildIndex($id, $indexInParent); + if ($element->getContainer() !== $this) { + throw new HTML_QuickForm2_NotFoundException( + "Element with name '".$element->getName()."' was not found" + ); } - - } - - protected function getChildIndex($id) - { - if (isset($this->idIndex[$id])) { - return $this->idIndex[$id]; + foreach ($this as $key => $child){ + if ($child === $element) { + unset($this->elements[$key]); + $element->setContainer(null); + break; + } } - return null; + return $element; } - protected function removeChildIndex($id) - { - unset($this->idIndex[$id]); - if (null != ($parent = $this->getContainer())) { - $parent->removeChildIndex($id); - } - } /** * Returns an element if its id is found @@ -231,12 +208,10 @@ */ public function getElementById($id) { - if (isset($this->idIndex[$id])) { - $element = $this->elements[$this->idIndex[$id]]; - if ($element->getId() != $id) { - return $element->getElementById($id); + foreach ($this->getRecursiveIterator() as $element) { + if ($id == $element->getId()) { + return $element; } - return $element; } return null; } @@ -269,35 +244,24 @@ */ public function insertBefore(HTML_QuickForm2_AbstractElement $element, HTML_QuickForm2_AbstractElement $reference = null) { - if (isset($reference)) { - $index = $this->getChildIndex($reference->getId()); - if (null !== $index) { - if (isset($this->elements[$index]) && - $this->elements[$index] === $reference) { - - $newIdx = array(); - foreach ($this->idIndex as $id => $idx) { - if ($idx >= $index) { - $new[$id] = $idx+1; - } - } - $this->idIndex = array_merge($this->idIndex, $new); - - $head = array_slice($this->elements, 0, $index, true); - $tail = array_slice($this->elements, $index, -1, true); - $this->elements = $head; - $this->addElement($element); - foreach ($tail as $k => $v) { - $this->elements[$k+1] = $v; - } - return $element; + if (null === $reference) { + return $this->addElement($element); + } + $offset = 0; + foreach ($this as $child) { + if ($child === $reference) { + if ($this === $element->getContainer()) { + $this->removeChild($element); } + $element->setContainer($this); + array_splice($this->elements, $offset, 0, array($element)); + return $element; } - throw new HTML_QuickForm2_NotFoundException( - "Reference element with name '".$reference->getName()."' was not found" - ); + $offset++; } - return $this->addElement($element); + throw new HTML_QuickForm2_NotFoundException( + "Reference element with name '".$reference->getName()."' was not found" + ); } /** http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php?r1=1.5&r2=1.6&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.5 pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.6 --- pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.5 Fri Sep 8 16:58:16 2006 +++ pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php Thu Oct 12 21:24:29 2006 @@ -38,7 +38,7 @@ * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: ContainerTest.php,v 1.5 2006/09/08 16:58:16 avb Exp $ + * @version CVS: $Id: ContainerTest.php,v 1.6 2006/10/12 21:24:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -367,5 +367,33 @@ $this->assertEquals(1, count($c2), 'Element count is incorrect'); $this->assertSame($e1, $c2->getElementById('dup')); } + + public function testFrozenStatusPropagates() + { + $cFreeze = new HTML_QuickForm2_ContainerImpl('cFreeze'); + $elFreeze = $cFreeze->addElement(new HTML_QuickForm2_ElementImpl2('elFreeze')); + + $cFreeze->toggleFrozen(true); + $this->assertTrue($cFreeze->toggleFrozen(), 'Container should be frozen'); + $this->assertTrue($elFreeze->toggleFrozen(), 'Contained element should be frozen'); + + $cFreeze->toggleFrozen(false); + $this->assertFalse($cFreeze->toggleFrozen(), 'Container should not be frozen'); + $this->assertFalse($elFreeze->toggleFrozen(), 'Contained element should not be frozen'); + } + + public function testPersistentFreezePropagates() + { + $cPers = new HTML_QuickForm2_ContainerImpl('cPersistent'); + $elPers = $cPers->addElement(new HTML_QuickForm2_ElementImpl2('elPersistent')); + + $cPers->persistentFreeze(true); + $this->assertTrue($cPers->persistentFreeze(), 'Container should have persistent freeze behaviour'); + $this->assertTrue($elPers->persistentFreeze(), 'Contained element should have persistent freeze behaviour'); + + $cPers->persistentFreeze(false); + $this->assertFalse($cPers->persistentFreeze(), 'Container should not have persistent freeze behaviour'); + $this->assertFalse($elPers->persistentFreeze(), 'Contained element should not have persistent freeze behaviour'); + } } -?> \ No newline at end of file +?>
http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Container.php?r1=1.8&r2=1.9&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Container.php diff -u pear/HTML_QuickForm2/QuickForm2/Container.php:1.8 pear/HTML_QuickForm2/QuickForm2/Container.php:1.9 --- pear/HTML_QuickForm2/QuickForm2/Container.php:1.8 Sat Oct 7 09:10:55 2006 +++ pear/HTML_QuickForm2/QuickForm2/Container.php Thu Oct 12 21:24:29 2006 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Container.php,v 1.8 2006/10/07 09:10:55 mansion Exp $ + * @version CVS: $Id: Container.php,v 1.9 2006/10/12 21:24:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -68,14 +68,6 @@ protected $elements = array(); /** - * Array of contained ids - * Format: element_id => key of child container - * @var array - */ - protected $idIndex = array(); - - - /** * 'name' and 'id' attributes should be always present and their setting * should go through setName() and setId(). * @var array @@ -128,6 +120,25 @@ $this->attributes['id'] = (string)$id; } + public function toggleFrozen($freeze = null) + { + if (null !== $freeze) { + foreach ($this as $child) { + $child->toggleFrozen($freeze); + } + } + return parent::toggleFrozen($freeze); + } + + public function persistentFreeze($persistent = null) + { + if (null !== $persistent) { + foreach ($this as $child) { + $child->persistentFreeze($persistent); + } + } + return parent::persistentFreeze($persistent); + } @@ -150,26 +161,15 @@ * * @param HTML_QuickForm2_AbstractElement Element to add * @return HTML_QuickForm2_AbstractElement Added element + * @throws HTML_QuickForm2_InvalidArgumentException */ public function addElement(HTML_QuickForm2_AbstractElement $element) { - if (null !== ($container = $element->getContainer())) { - $container->removeChild($element); + if ($this === $element->getContainer()) { + $this->removeChild($element); } $element->setContainer($this); - $this->elements[] = $element; - end($this->elements); - $current = key($this->elements); - $this->setChildIndex($element->getId(), $current); - - if ($element instanceof HTML_QuickForm2_Container && - count($element) > 0) { - while (list($k) = each($element->idIndex)) { - $this->setChildIndex($k, $current); - } - } - return $element; } @@ -183,45 +183,22 @@ */ public function removeChild(HTML_QuickForm2_AbstractElement $element) { - if ($element->getContainer() === $this) { - $id = $element->getId(); - $index = $this->getChildIndex($id); - $this->removeChildIndex($id); - unset($this->elements[$index]); - $element->setContainer(null); - return $element; - } - throw new HTML_QuickForm2_NotFoundException( - "Element with name '".$element->getName()."' was not found" - ); - } - - protected function setChildIndex($id, $index) - { - $this->idIndex[$id] = $index; - if (null != ($parent = $this->getContainer())) { - $indexInParent = $parent->idIndex[$this->getId()]; - $parent->setChildIndex($id, $indexInParent); + if ($element->getContainer() !== $this) { + throw new HTML_QuickForm2_NotFoundException( + "Element with name '".$element->getName()."' was not found" + ); } - - } - - protected function getChildIndex($id) - { - if (isset($this->idIndex[$id])) { - return $this->idIndex[$id]; + foreach ($this as $key => $child){ + if ($child === $element) { + unset($this->elements[$key]); + $element->setContainer(null); + break; + } } - return null; + return $element; } - protected function removeChildIndex($id) - { - unset($this->idIndex[$id]); - if (null != ($parent = $this->getContainer())) { - $parent->removeChildIndex($id); - } - } /** * Returns an element if its id is found @@ -231,12 +208,10 @@ */ public function getElementById($id) { - if (isset($this->idIndex[$id])) { - $element = $this->elements[$this->idIndex[$id]]; - if ($element->getId() != $id) { - return $element->getElementById($id); + foreach ($this->getRecursiveIterator() as $element) { + if ($id == $element->getId()) { + return $element; } - return $element; } return null; } @@ -269,35 +244,24 @@ */ public function insertBefore(HTML_QuickForm2_AbstractElement $element, HTML_QuickForm2_AbstractElement $reference = null) { - if (isset($reference)) { - $index = $this->getChildIndex($reference->getId()); - if (null !== $index) { - if (isset($this->elements[$index]) && - $this->elements[$index] === $reference) { - - $newIdx = array(); - foreach ($this->idIndex as $id => $idx) { - if ($idx >= $index) { - $new[$id] = $idx+1; - } - } - $this->idIndex = array_merge($this->idIndex, $new); - - $head = array_slice($this->elements, 0, $index, true); - $tail = array_slice($this->elements, $index, -1, true); - $this->elements = $head; - $this->addElement($element); - foreach ($tail as $k => $v) { - $this->elements[$k+1] = $v; - } - return $element; + if (null === $reference) { + return $this->addElement($element); + } + $offset = 0; + foreach ($this as $child) { + if ($child === $reference) { + if ($this === $element->getContainer()) { + $this->removeChild($element); } + $element->setContainer($this); + array_splice($this->elements, $offset, 0, array($element)); + return $element; } - throw new HTML_QuickForm2_NotFoundException( - "Reference element with name '".$reference->getName()."' was not found" - ); + $offset++; } - return $this->addElement($element); + throw new HTML_QuickForm2_NotFoundException( + "Reference element with name '".$reference->getName()."' was not found" + ); } /** http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php?r1=1.5&r2=1.6&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.5 pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.6 --- pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php:1.5 Fri Sep 8 16:58:16 2006 +++ pear/HTML_QuickForm2/tests/QuickForm2/ContainerTest.php Thu Oct 12 21:24:29 2006 @@ -38,7 +38,7 @@ * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: ContainerTest.php,v 1.5 2006/09/08 16:58:16 avb Exp $ + * @version CVS: $Id: ContainerTest.php,v 1.6 2006/10/12 21:24:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -367,5 +367,33 @@ $this->assertEquals(1, count($c2), 'Element count is incorrect'); $this->assertSame($e1, $c2->getElementById('dup')); } + + public function testFrozenStatusPropagates() + { + $cFreeze = new HTML_QuickForm2_ContainerImpl('cFreeze'); + $elFreeze = $cFreeze->addElement(new HTML_QuickForm2_ElementImpl2('elFreeze')); + + $cFreeze->toggleFrozen(true); + $this->assertTrue($cFreeze->toggleFrozen(), 'Container should be frozen'); + $this->assertTrue($elFreeze->toggleFrozen(), 'Contained element should be frozen'); + + $cFreeze->toggleFrozen(false); + $this->assertFalse($cFreeze->toggleFrozen(), 'Container should not be frozen'); + $this->assertFalse($elFreeze->toggleFrozen(), 'Contained element should not be frozen'); + } + + public function testPersistentFreezePropagates() + { + $cPers = new HTML_QuickForm2_ContainerImpl('cPersistent'); + $elPers = $cPers->addElement(new HTML_QuickForm2_ElementImpl2('elPersistent')); + + $cPers->persistentFreeze(true); + $this->assertTrue($cPers->persistentFreeze(), 'Container should have persistent freeze behaviour'); + $this->assertTrue($elPers->persistentFreeze(), 'Contained element should have persistent freeze behaviour'); + + $cPers->persistentFreeze(false); + $this->assertFalse($cPers->persistentFreeze(), 'Container should not have persistent freeze behaviour'); + $this->assertFalse($elPers->persistentFreeze(), 'Contained element should not have persistent freeze behaviour'); + } } -?> \ No newline at end of file +?>