Skip to content

Commit

Permalink
Moved states array to the separate class and added Twig extension to …
Browse files Browse the repository at this point in the history
…display state name
  • Loading branch information
Napas committed Jun 25, 2014
1 parent 2473d05 commit 51d9050
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 53 deletions.
55 changes: 2 additions & 53 deletions Form/UsStatesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Napas\FormExtraBundle\Form;

use Napas\FormExtraBundle\Misc\UsStates;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Expand Down Expand Up @@ -35,59 +36,7 @@ public function getParent()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choices' => array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming',
),
'choices' => UsStates::getChoices(),
));
}
}
88 changes: 88 additions & 0 deletions Misc/UsStates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Napas\FormExtraBundle\Misc;

/**
* US States class
* @package Napas\FormExtraBundle\Misc
*/
class UsStates
{
/**
* @var array
*/
protected static $states = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming',
);

/**
* @return array
*/
public static function getChoices()
{
return self::$states;
}

/**
* @param $code
* @return null
*/
public static function getStateName($code)
{
if (isset(self::$states[$code])) {
return self::$states[$code];
}

return null;
}
}
5 changes: 5 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ services:
class: Napas\FormExtraBundle\Form\UsStatesType
tags:
- { name: form.type, alias: us_states }

napas_extra_form.twig.states_extension:
class: Napas\FormExtraBundle\Twig\StatesExtension
tags:
- { name: twig.extension }
48 changes: 48 additions & 0 deletions Twig/StatesExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Napas\FormExtraBundle\Twig;

use Napas\FormExtraBundle\Misc\UsStates;

/**
* Twig extension to display states
*
* @package Napas\FormExtraBundle\Twig
*/
class StatesExtension extends \Twig_Extension
{
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'states_extension';
}

/**
* @return array
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction(
'state_name',
array(
$this,
'getStateName'
)
),
);
}

/**
* @param $stateCode
* @return null
*/
public function getStateName($stateCode)
{
return UsStates::getStateName($stateCode);
}
}

0 comments on commit 51d9050

Please sign in to comment.