cvs: pear /HTML_QuickForm2/QuickForm2/Element Select.php /HTML_QuickForm2/tests/QuickForm2/Element SelectTest.php

From: Date: Thu, 19 Oct 2006 21:40:29 +0000
Subject: cvs: pear /HTML_QuickForm2/QuickForm2/Element Select.php /HTML_QuickForm2/tests/QuickForm2/Element SelectTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-42658@lists.php.net to get a copy of this message
avb Thu Oct 19 21:40:29 2006 UTC Modified files: /pear/HTML_QuickForm2/QuickForm2/Element Select.php /pear/HTML_QuickForm2/tests/QuickForm2/Element SelectTest.php Log: Reimplemented Select::getValue() taking this into account: http://quickform.mamasam.com/wiki/ideas/values Added unit tests for "intrinsic validation" Added loadOptions() method (without unit tests currently)

http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Element/Select.php?r1=1.1&r2=1.2&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Element/Select.php diff -u pear/HTML_QuickForm2/QuickForm2/Element/Select.php:1.1 pear/HTML_QuickForm2/QuickForm2/Element/Select.php:1.2 --- pear/HTML_QuickForm2/QuickForm2/Element/Select.php:1.1 Wed Oct 18 21:04:04 2006 +++ pear/HTML_QuickForm2/QuickForm2/Element/Select.php Thu Oct 19 21:40: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: Select.php,v 1.1 2006/10/18 21:04:04 avb Exp $ + * @version CVS: $Id: Select.php,v 1.2 2006/10/19 21:40:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -80,15 +80,23 @@ */ protected $values; + /** + * Reference to parent <select>'s possible values + * @var array + */ + protected $possibleValues; + /** * Class constructor * * @param array Reference to values of parent <select> element + * @param array Reference to possible values of parent <select> element */ - public function __construct(&$values) + public function __construct(&$values, &$possibleValues) { - $this->values =& $values; + $this->values =& $values; + $this->possibleValues =& $possibleValues; } /** @@ -117,6 +125,9 @@ } $attributes['value'] = (string)$value; } + if (!isset($attributes['disabled'])) { + $this->possibleValues[(string)$value] = true; + } $this->options[] = array('text' => $text, 'attr' => $attributes); } @@ -131,7 +142,8 @@ public function addOptgroup($label, $attributes = null) { $optgroup = new HTML_QuickForm2_Element_Select_Optgroup( - $this->values, $label, $attributes + $this->values, $this->possibleValues, + $label, $attributes ); $this->options[] = $optgroup; return $optgroup; @@ -223,13 +235,14 @@ * Class constructor * * @param array Reference to values of parent <select> element + * @param array Reference to possible values of parent <select> element * @param string 'label' attribute for optgroup tag * @param mixed Additional attributes for <optgroup> tag (either as a * string or as an associative array) */ - public function __construct(&$values, $label, $attributes = null) + public function __construct(&$values, &$possibleValues, $label, $attributes = null) { - parent::__construct($values); + parent::__construct($values, $possibleValues); $this->setAttributes($attributes); $this->attributes['label'] = (string)$label; } @@ -280,6 +293,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @version Release: @package_version@ + * @todo Unit tests and better PHPDoc for loadOptions() */ class HTML_QuickForm2_Element_Select extends HTML_QuickForm2_Element implements IteratorAggregate, Countable @@ -293,6 +307,16 @@ protected $values = array(); /** + * Possible values for select elements + * + * A value is considered possible if it is present as a value attribute of + * some option and that option is not disabled. + * @var array + */ + protected $possibleValues = array(); + + + /** * Object containing options for the <select> element * @var HTML_QuickForm2_Element_Select_OptionContainer */ @@ -303,17 +327,15 @@ * * @param string Element name * @param mixed Data used to populate the element's options, passed to - * {@link load()} method + * {@link loadOptions()} method * @param mixed Label for the element (may be an array of labels) * @param mixed Attributes (either a string or an array) + * @throws HTML_QuickForm2_InvalidArgumentException if junk is given in $options */ public function __construct($name = null, $options = null, $label = null, $attributes = null) { parent::__construct($name, $options, $label, $attributes); - if (null !== $options) { - $this->load($options); - } - $this->optionContainer = new HTML_QuickForm2_Element_Select_OptionContainer($this->values); + $this->loadOptions($options); } public function getType() @@ -343,34 +365,82 @@ protected function getFrozenHtml() { - $value = array(); - foreach ($this->values as $key => $val) { - foreach ($this->getRecursiveIterator() as $child) { - if (is_array($child) && (string)$val == $child['attr']['value']) { - $value[$key] = $child['text']; - } + if (null === ($value = $this->getValue())) { + return '&nbsp;'; + } + $valueHash = is_array($value)? array_flip($value): array($value => true); + $options = array(); + foreach ($this->getRecursiveIterator() as $child) { + if (is_array($child) && isset($valueHash[$child['attr']['value']]) && + empty($child['attr']['disabled'])) + { + $options[] = $child['text']; } } - $html = empty($value)? '&nbsp;': implode('<br />', $value); + $html = implode('<br />', $options); if ($this->persistent) { $name = $this->attributes['name'] . (empty($this->attributes['multiple'])? '': '[]'); // Only use id attribute if doing single hidden input - $idAttr = (1 == count($value))? array('id' => $this->getId()): array(); - foreach ($value as $key => $item) { + $idAttr = (1 == count($valueHash))? array('id' => $this->getId()): array(); + foreach ($valueHash as $key => $item) { $html .= '<input type="hidden"' . self::getAttributesString(array( 'name' => $name, - 'value' => $this->values[$key] + 'value' => $key ) + $idAttr) . ' />'; } } return $html; } + /** + * Returns the value of the <select> element + * + * Please note that the returned value may not necessarily be equal to that + * passed to {@link setValue()}. It passes "intrinsic validation" confirming + * that such value could possibly be submitted by this <select> element. + * Specifically, this method will return null if the elements "disabled" + * attribute is set, it will not return values if there are no options having + * such a "value" attribute or if such options' "disabled" attribute is set. + * It will also only return a scalar value for single selects, mimicking + * the common browsers' behaviour. + * + * @return mixed "value" attribute of selected option in case of single + * select, array of selected options' "value" attributes in + * case of multiple selects, null if no options selected + */ public function getValue() { - return $this->values; + if (0 == count($this) || !empty($this->attributes['disabled']) || + 0 == count($this->values) || 0 == count($this->possibleValues)) + { + return null; + } + + $values = array(); + foreach ($this->values as $value) { + if (!empty($this->possibleValues[$value])) { + $values[] = $value; + } + } + if (0 == count($values)) { + return null; + } elseif (!empty($this->attributes['multiple'])) { + return $values; + } elseif (1 == count($values)) { + return $values[0]; + } else { + // The <select> is not multiple, but several options are to be + // selected. At least IE and Mozilla select the last selected + // option in this case, we should do the same + foreach ($this->getRecursiveIterator() as $child) { + if (is_array($child) && in_array($child['attr']['value'], $values)) { + $lastValue = $child['attr']['value']; + } + } + return $lastValue; + } } public function setValue($value) @@ -383,13 +453,52 @@ } /** - * To be written + * Loads <option>s (and <optgroup>s) for select element + * + * @param array + * @throws HTML_QuickForm2_InvalidArgumentException if junk is given in $options */ - public function load($options) + public function loadOptions($options) { - throw new HTML_QuickForm2_Exception('Not implemented yet'); + $this->values = array(); + $this->possibleValues = array(); + $this->optionContainer = new HTML_QuickForm2_Element_Select_OptionContainer( + $this->values, $this->possibleValues + ); + if (null === $options) { + return; + } elseif (is_array($options)) { + $this->loadOptionsFromArray($this->optionContainer, $options); + } else { + throw new HTML_QuickForm2_InvalidArgumentException( + 'loadOptions() expects an array' + ); + } } + + /** + * Adds options from given array into given container + * + * @param HTML_QuickForm2_Element_Select_OptionContainer options will be + * added to this container + * @param array options array + */ + protected function loadOptionsFromArray( + HTML_QuickForm2_Element_Select_OptionContainer $container, $options + ) + { + foreach ($options as $key => $value) { + if (is_array($value)) { + $optgroup = $container->addOptgroup($key); + $this->loadOptionsFromArray($optgroup, $value); + } else { + $container->addOption($value, $key); + } + } + } + + // // The following methods decorate those of OptionContainer // http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php?r1=1.1&r2=1.2&diff_format=u Index: pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php diff -u pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php:1.1 pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php:1.2 --- pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php:1.1 Wed Oct 18 21:04:03 2006 +++ pear/HTML_QuickForm2/tests/QuickForm2/Element/SelectTest.php Thu Oct 19 21:40: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: SelectTest.php,v 1.1 2006/10/18 21:04:03 avb Exp $ + * @version CVS: $Id: SelectTest.php,v 1.2 2006/10/19 21:40:29 avb Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -72,13 +72,68 @@ { $sel = new HTML_QuickForm2_Element_Select(); $this->assertEquals(0, count($sel)); - $this->assertEquals(0, count($sel->getValue())); + $this->assertNull($sel->getValue()); $this->assertRegExp( '!^<select[^>]*>\\s*</select>$!', $sel->__toString() ); } + public function testSelectSingleValueIsScalar() + { + $sel = new HTML_QuickForm2_Element_Select(); + $sel->addOption('Text', 'Value'); + $sel->setValue('Value'); + $this->assertEquals('Value', $sel->getValue()); + + $sel->setValue('Nonextistent'); + $this->assertNull($sel->getValue()); + + $sel2 = new HTML_QuickForm2_Element_Select(); + $sel2->addOption('Text', 'Value'); + $sel2->addOption('Other Text', 'Other Value'); + $sel2->addOption('Different Text', 'Different Value'); + + $sel2->setValue(array('Different value', 'Value')); + $this->assertEquals('Value', $sel2->getValue()); + } + + public function testSelectMultipleValueIsArray() + { + $sel = new HTML_QuickForm2_Element_Select('mult', null, null, array('multiple')); + $sel->addOption('Text', 'Value'); + $sel->addOption('Other Text', 'Other Value'); + $sel->addOption('Different Text', 'Different Value'); + + $sel->setValue('Other Value'); + $this->assertEquals(array('Other Value'), $sel->getValue()); + + $sel->setValue('Nonexistent'); + $this->assertNull($sel->getValue()); + + $sel->setValue(array('Value', 'Different Value', 'Nonexistent')); + $this->assertEquals(array('Value', 'Different Value'), $sel->getValue()); + } + + public function testDisabledSelectHasNoValue() + { + $sel = new HTML_QuickForm2_Element_Select('disableMe', null, null, array('disabled')); + $sel->addOption('Text', 'Value'); + $sel->setValue('Value'); + + $this->assertNull($sel->getValue()); + } + + public function testDisabledOptionsDoNotProduceValues() + { + $sel = new HTML_QuickForm2_Element_Select(); + $sel->addOption('Disabled Text', 'Disabled Value', array('disabled')); + $sel->setValue('Disabled Value'); + + $this->assertNull($sel->getValue()); + } + + public function testAddOption() { $sel = new HTML_QuickForm2_Element_Select(); @@ -97,7 +152,7 @@ $sel3 = new HTML_QuickForm2_Element_Select(); $sel3->addOption('Text', 'Value', array('selected')); - $this->assertContains('Value', $sel3->getValue()); + $this->assertEquals('Value', $sel3->getValue()); $this->assertRegExp( '!<option[^>]+selected="selected"[^>]*>Text</option>!', $sel3->__toString() @@ -144,7 +199,7 @@ $sel3 = new HTML_QuickForm2_Element_Select(); $optgroup3 = $sel3->addOptgroup('Label'); $optgroup3->addOption('Text', 'Value', array('selected')); - $this->assertContains('Value', $sel3->getValue()); + $this->assertEquals('Value', $sel3->getValue()); $this->assertRegExp( '!<optgroup[^>]+label="Label"[^>]*>\\s*<option[^>]+selected="selected"[^>]*>Text</option>\\s*</optgroup>!', $sel3->__toString()
« previous php.pear.cvs (#42658) next »