Source for file Container.php
Documentation is available at Container.php
* Base class for simple HTML_QuickForm2 containers
* Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
* Bertrand Mansion <golgote@mamasam.com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package HTML_QuickForm2
* @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.20 2007/10/22 10:11:47 mansion Exp $
* @link http://pear.php.net/package/HTML_QuickForm2
* Base class for all HTML_QuickForm2 elements
require_once 'HTML/QuickForm2/Node.php';
* Abstract base class for simple QuickForm2 containers
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
implements IteratorAggregate , Countable
* Array of elements contained in this container
* 'name' and 'id' attributes should be always present and their setting
* should go through setName() and setId().
"Required attribute 'name' can not be removed"
} elseif ('id' == $name) {
"Required attribute 'id' can not be removed"
return $this->attributes['name'];
$this->attributes['name'] = (string) $name;
return isset ($this->attributes['id'])? $this->attributes['id']: null;
public function setId($id = null )
$id = self ::generateId ($this->getName());
$this->attributes['id'] = (string) $id;
foreach ($this as $child) {
$child->toggleFrozen ($freeze);
if (null !== $persistent) {
foreach ($this as $child) {
$child->persistentFreeze ($persistent);
* Returns the element's value
* The default implementation for Containers is to return an array with
* contained elements' values. The array is indexed the same way $_GET and
* $_POST arrays would be for these elements.
foreach ($this as $child) {
$value = $child->getValue ();
$values = self ::arrayMerge ($values, $value);
$name = $child->getName ();
if (!isset ($valueAry[$token])) {
$valueAry[$token] = array ();
$valueAry = & $valueAry[$token];
} while (count($tokens) > 1 );
$valueAry[$tokens[0 ]] = $value;
return empty ($values)? null: $values;
* Merges two arrays like the PHP function array_merge_recursive does,
* the difference being that existing integer keys will not be renumbered.
* @return array resulting array
foreach ($b as $k => $v) {
$a[$k] = self ::arrayMerge (isset ($a[$k])? $a[$k]: array (), $v);
* Returns an array of this container's elements
* @return array Container elements
* Appends an element to the container
* If the element was previously added to the container or to another
* container, it is first removed there.
* @param HTML_QuickForm2_Node Element to add
* @return HTML_QuickForm2_Node Added element
* @throws HTML_QuickForm2_InvalidArgumentException
public function appendChild(HTML_QuickForm2_Node $element)
if ($this === $element->getContainer ()) {
$element->setContainer ($this);
* Appends an element to the container (possibly creating it first)
* If the first parameter is an instance of HTML_QuickForm2_Node then all
* other parameters are ignored and the method just calls {@link appendChild()}.
* In the other case the element is first created via
* {@link HTML_QuickForm2_Factory::createElement()} and then added via the
* same method. This is a convenience method to reduce typing and ease
* porting from HTML_QuickForm.
* @param string|HTML_QuickForm2_Node Either type name (treated
* case-insensitively) or an element instance
* @param mixed Element name
* @param mixed Element attributes
* @param array Element-specific data
* @return HTML_QuickForm2_Node Added element
* @throws HTML_QuickForm2_InvalidArgumentException
* @throws HTML_QuickForm2_NotFoundException
public function addElement($elementOrType, $name = null , $attributes = null ,
$elementOrType, $name, $attributes, $data
* Removes the element from this container
* If the reference object is not given, the element will be appended.
* @param HTML_QuickForm2_Node Element to remove
* @return HTML_QuickForm2_Node Removed object
public function removeChild(HTML_QuickForm2_Node $element)
if ($element->getContainer () !== $this) {
"Element with name '". $element->getName (). "' was not found"
foreach ($this as $key => $child){
if ($child === $element) {
$element->setContainer (null );
* Returns an element if its id is found
* @param string Element id to find
* @return HTML_QuickForm2_Node|null
if ($id == $element->getId ()) {
* Returns an array of elements which name corresponds to element
* @param string Elements name to find
if ($element->getName () == $name) {
* Inserts an element in the container
* If the reference object is not given, the element will be appended.
* @param HTML_QuickForm2_Node Element to insert
* @param HTML_QuickForm2_Node Reference to insert before
* @return HTML_QuickForm2_Node Inserted element
public function insertBefore(HTML_QuickForm2_Node $element, HTML_QuickForm2_Node $reference = null )
if (null === $reference) {
foreach ($this as $child) {
if ($child === $reference) {
if ($this === $element->getContainer ()) {
$element->setContainer ($this);
"Reference element with name '". $reference->getName (). "' was not found"
* Returns a recursive iterator for the container elements
* @return HTML_QuickForm2_ContainerIterator
* Returns a recursive iterator iterator for the container elements
* @return RecursiveIteratorIterator
return new RecursiveIteratorIterator (
RecursiveIteratorIterator ::SELF_FIRST
* Returns the number of elements in the container
* Called when the element needs to update its value from form's data sources
* The default behaviour is just to call the updateValue() methods of
* contained elements, since default Container doesn't have any value itself
foreach ($this as $child) {
* Performs the server-side validation
* This method also calls validate() on all contained elements.
* @return boolean Whether the container and all contained elements are valid
foreach ($this as $child) {
$valid = $child->validate () && $valid;
* Appends an element to the container, creating it first
* The element will be created via {@link HTML_QuickForm2_Factory::createElement()}
* and then added via the {@link appendChild()} method.
* The element type is deduced from the method name. Camelcases will be
* converted to underscores and lowercased.
* This is a convenience method to reduce typing.
* @param mixed Element name
* @param mixed Element attributes
* @param array Element-specific data
* @return HTML_QuickForm2_Node Added element
* @throws HTML_QuickForm2_InvalidArgumentException
* @throws HTML_QuickForm2_NotFoundException
public function __call($m, $a)
if (preg_match('/^(add)([a-zA-Z0-9_]+)$/', $m, $match)) {
if ($match[1 ] == 'add') {
$name = isset ($a[0 ]) ? $a[0 ] : null;
$attr = isset ($a[1 ]) ? $a[1 ] : null;
$data = isset ($a[2 ]) ? $a[2 ] : array ();
return $this->addElement($type, $name, $attr, $data);
* Implements a recursive iterator for the container elements
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: 0.2.0
public function __construct(HTML_QuickForm2_Container $container)
parent ::__construct ($container->getElements ());
Documentation generated on Mon, 22 Oct 2007 12:30:12 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|