From 51d9050b671d4478468fc9d5715535f8f756eb0c Mon Sep 17 00:00:00 2001 From: Napas Date: Wed, 25 Jun 2014 13:38:56 +0100 Subject: [PATCH] Moved states array to the separate class and added Twig extension to display state name --- Form/UsStatesType.php | 55 +--------------------- Misc/UsStates.php | 88 +++++++++++++++++++++++++++++++++++ Resources/config/services.yml | 5 ++ Twig/StatesExtension.php | 48 +++++++++++++++++++ 4 files changed, 143 insertions(+), 53 deletions(-) create mode 100644 Misc/UsStates.php create mode 100644 Twig/StatesExtension.php diff --git a/Form/UsStatesType.php b/Form/UsStatesType.php index aa2a230..edbf0b7 100644 --- a/Form/UsStatesType.php +++ b/Form/UsStatesType.php @@ -2,6 +2,7 @@ namespace Napas\FormExtraBundle\Form; +use Napas\FormExtraBundle\Misc\UsStates; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolverInterface; @@ -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(), )); } } \ No newline at end of file diff --git a/Misc/UsStates.php b/Misc/UsStates.php new file mode 100644 index 0000000..e9fe9ec --- /dev/null +++ b/Misc/UsStates.php @@ -0,0 +1,88 @@ + '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; + } +} \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 51e983e..af48cac 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -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 } diff --git a/Twig/StatesExtension.php b/Twig/StatesExtension.php new file mode 100644 index 0000000..e6ed410 --- /dev/null +++ b/Twig/StatesExtension.php @@ -0,0 +1,48 @@ +