1<?php
2/**
3 * Produce a HTML Select dropdown of US States
4 *
5 * PHP Version 4
6 *
7 * @category  HTML
8 * @package   HTML_Select_Common
9 * @author    Richard Heyes <richard@php.net>
10 * @copyright 2002 Richard Heyes <richard@php.net>
11 * @license   BSD (see http://www.opensource.org/licenses/bsd-license.php)
12 * @version   CVS: $Id: USState.php 303988 2010-10-04 12:27:17Z clockwerx $
13 * @link      http://pear.php.net/package/HTML_Select_Common
14 */
15
16/**
17 * Class to produce a HTML Select dropdown of US States
18 *
19 * @category HTML
20 * @package  HTML_Select
21 * @author   Richard Heyes <richard@php.net>
22 * @access   public
23 * @license  BSD (see http://www.opensource.org/licenses/bsd-license.php);
24 * @link     http://pear.php.net/package/HTML_Select_Common
25 */
26
27class HTML_Select_Common_USState
28{
29    /**
30    * Constructor
31    *
32    * @access public
33    */
34    function HTML_Select_Common_USState()
35    {
36        $this->_states['al'] = 'Alabama';
37        $this->_states['ak'] = 'Alaska';
38        $this->_states['az'] = 'Arizona';
39        $this->_states['ar'] = 'Arkansas';
40        $this->_states['ca'] = 'California';
41        $this->_states['co'] = 'Colorado';
42        $this->_states['ct'] = 'Connecticut';
43        $this->_states['de'] = 'Delaware';
44        $this->_states['dc'] = 'District of Columbia';
45        $this->_states['fl'] = 'Florida';
46        $this->_states['ga'] = 'Georgia';
47        $this->_states['hi'] = 'Hawaii';
48        $this->_states['id'] = 'Idaho';
49        $this->_states['il'] = 'Illinois';
50        $this->_states['in'] = 'Indiana';
51        $this->_states['ia'] = 'Iowa';
52        $this->_states['ks'] = 'Kansas';
53        $this->_states['ky'] = 'Kentucky';
54        $this->_states['la'] = 'Louisiana';
55        $this->_states['me'] = 'Maine';
56        $this->_states['md'] = 'Maryland';
57        $this->_states['ma'] = 'Massachusetts';
58        $this->_states['mi'] = 'Michigan';
59        $this->_states['mn'] = 'Minnesota';
60        $this->_states['ms'] = 'Mississippi';
61        $this->_states['mo'] = 'Missouri';
62        $this->_states['mt'] = 'Montana';
63        $this->_states['ne'] = 'Nebraska';
64        $this->_states['nv'] = 'Nevada';
65        $this->_states['nh'] = 'New Hampshire';
66        $this->_states['nj'] = 'New Jersey';
67        $this->_states['nm'] = 'New Mexico';
68        $this->_states['ny'] = 'New York';
69        $this->_states['nc'] = 'North Carolina';
70        $this->_states['nd'] = 'North Dakota';
71        $this->_states['oh'] = 'Ohio';
72        $this->_states['ok'] = 'Oklahoma';
73        $this->_states['or'] = 'Oregon';
74        $this->_states['pa'] = 'Pennsylvania';
75        $this->_states['ri'] = 'Rhode Island';
76        $this->_states['sc'] = 'South Carolina';
77        $this->_states['sd'] = 'South Dakota';
78        $this->_states['tn'] = 'Tennessee';
79        $this->_states['tx'] = 'Texas';
80        $this->_states['ut'] = 'Utah';
81        $this->_states['vt'] = 'Vermont';
82        $this->_states['va'] = 'Virginia';
83        $this->_states['wa'] = 'Washington';
84        $this->_states['wv'] = 'West Virginia';
85        $this->_states['wi'] = 'Wisconsin';
86        $this->_states['wy'] = 'Wyoming';
87    }
88
89    /**
90    * Produces the HTML for the dropdown
91    *
92    * @param string $name            The name="" attribute
93    * @param string $selectedOption  The option to be selected by default.
94    *                                Must match the state name exactly,
95    *                                (though it can be a different case).
96    * @param string $promoText       The text to appear as the first option
97    * @param string $extraAttributes Any extra attributes for the <select> tag
98    *
99    * @return string                  The HTML for the <select>
100    * @access public
101    */
102    function toHTML(
103        $name,
104        $selectedOption = null,
105        $promoText = 'Select a state...',
106        $extraAttributes = ''
107    ) {
108        $options[]      = sprintf('<option value="">%s</option>', $promoText);
109        $selectedOption = strtolower($selectedOption);
110
111        foreach ($this->_states as $state) {
112            $state_lc  = strtolower($state);
113            $selected  = $selectedOption == $state_lc ? ' selected="selected"' : '';
114            $options[] = '<option value="' . $state_lc . '"' . $selected . '>' .
115                         ucfirst($state) . '</option>';
116        }
117
118        return sprintf(
119            '<select name="%s" %s>%s</select>',
120            $name,
121            $extraAttributes,
122            implode("\r\n", $options)
123        );
124    }
125
126    /**
127    * Returns an array with all states, indexed by shortcut
128    *
129    * @return array                   The array containing all state data
130    * @access public
131    */
132    function getList()
133    {
134        return $this->_states;
135    }
136}
137
138?>
139