USState.php

From: Date: Sun, 06 Apr 2003 23:12:22 +0000
Subject: USState.php
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-14958@lists.php.net to get a copy of this message
Hi Dev, Thank you for the HTML_SELECT_COMMON_USState and Country selectors. I'm using them daily. Here is a patch for USState.php, based on cvs1.2 to make it more closely parallel Country.php in two ways they are not currently: 1. Provide getName() and isValidCode() methods. 2. In 1.2 toHtml(), both the option value and text are the long name. In Country, the value is the shortcut code and the text is the long name. My patch makes the values be the codes for States. The good news is that USState is simpler, faster, the user's code is simpler, and the two are now parallel. The bad news is that if someone was dependent on the value="" contents being the long name, this is not backward compatible. But since they were forced to convert back and forth several times, if they wanted to store the codes, then it would be worth it. Patch against 1.2 attached. Regards, -- Mitchell Perilstein mitch at enetis dot net www.enetis.net/~mitch

--- /tmp/USState.php 2003-04-06 17:01:37.000000000 -0600 +++ pear/HTML/Select/Common/USState.php 2003-04-06 16:57:03.000000000 -0600 @@ -110,7 +110,7 @@ * * @param string $name The name="" attribute * @param string $selectedOption The option to be selected by default. - * Must match the state name exactly, + * Must match the shortcut exactly, * (though it can be a different case). * @param string $promoText The text to appear as the first option * @param string $extraAttributes Any extra attributes for the <select> tag @@ -122,10 +122,9 @@ $options[] = sprintf('<option value="">%s</option>', $promoText); $selectedOption = strtolower($selectedOption); - foreach ($this->_states as $state) { - $state_lc = strtolower($state); - $selected = $selectedOption == $state_lc ? ' selected="selected"' : ''; - $options[] = '<option value="' . $state_lc . '"' . $selected . '>' . ucfirst($state) . '</option>'; + foreach ($this->_states as $shortcut=>$state) { + $selected = $selectedOption == $shortcut ? ' selected="selected"' : ''; + $options[] = '<option value="' . $shortcut . '"' . $selected . '>' . ucfirst($state) . '</option>'; } return sprintf('<select name="%s" %s>%s</select>', @@ -144,6 +143,31 @@ { return $this->_states; } + + + /** + * @param string $code State to find + * @return boolean True if valid + * @access public + */ + function isValidCode($code) + { + return isset($this->_states[strtolower($code)]); + } + + /** + * Returns state name given its code + * + * @param string $code State to find + * @return string Long state name, or null for not found. + * @access public + */ + function getName($code = '') + { + return $this->isValidCode($code = strtolower($code)) + ? $this->_states[$code] + : false; + } } ?>
« previous php.pear.dev (#14958) next »