Skip to content

Commit

Permalink
[Form] Implemented CountryField
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Dec 16, 2010
1 parent 78b6987 commit 9db7db4
Show file tree
Hide file tree
Showing 236 changed files with 132 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Symfony/Component/Form/CountryField.php
@@ -0,0 +1,62 @@
<?php

namespace Symfony\Component\Form;

/**
* A field for selecting from a list of countries
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class CountryField extends ChoiceField
{
/**
* Caches the country choices in different locales
* @var array
*/
protected static $countries;

/**
* @inheritDoc
*/
protected function configure()
{
$this->addOption('choices', self::getCountryChoices($this->locale));

parent::configure();
}

/**
* Returns the list of countries for a locale
*
* @param string $locale The locale to use for the country names
* @throws RuntimeException When the resource bundles cannot be loaded
*/
protected static function getCountryChoices($locale)
{
if (!isset(self::$countries[$locale])) {
$bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/region');

if ($bundle === null) {
throw new RuntimeException('The region resource bundle could not be loaded');
}

$collator = new \Collator($locale);
$countries = array();

foreach ($bundle->get('Countries') as $code => $name) {
// Global regions (f.i. "America") have numeric codes
// Countries have alphabetic codes
// "ZZ" is the code for unknown region
if (ctype_alpha($code) && $code !== 'ZZ') {
$countries[$code] = $name;
}
}

$collator->asort($countries);

self::$countries[$locale] = $countries;
}

return self::$countries[$locale];
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Form/Resources/data/UPDATE.txt
@@ -0,0 +1,34 @@
How to update the ICU data
==========================

1. Checkout the current version of the ICU data files

$ svn co http://source.icu-project.org/repos/icu/icu/trunk/source/data icu-data

2. Build the region resource bundles

$ cd icu-data/region
$ mkdir build
$ genrb -d build *.txt

3a. Replace the *.res files bundled with Symfony2 with the new ones.

.dat-package
------------

The individual *.res files can be combined into a single .dat-file.
Unfortunately, PHP's `ResourceBundle` class is currently not able to handle
.dat-files.

Once it is, the following steps have to be followed to build the .dat-file:

3b. Package the resource bundles into a single file

$ find . -name *.res | sed -e "s/\.\///g" > packagelist.txt
$ pkgdata -p region -T build -d . packagelist.txt

4. Clean up

$ rm -rf build packagelist.txt

5. You can now move region.dat to replace the version bundled with Symfony2.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions tests/Symfony/Tests/Component/Form/CountryFieldTest.php
@@ -0,0 +1,36 @@
<?php

namespace Symfony\Tests\Component\Form;

use Symfony\Component\Form\CountryField;
use Symfony\Component\Form\FormConfiguration;

class CountryFieldTest extends \PHPUnit_Framework_TestCase
{
public function testCountriesAreSelectable()
{
FormConfiguration::setDefaultLocale('de_AT');

$field = new CountryField('country');
$choices = $field->getOtherChoices();

$this->assertArrayHasKey('DE', $choices);
$this->assertEquals('Deutschland', $choices['DE']);
$this->assertArrayHasKey('GB', $choices);
$this->assertEquals('Vereinigtes Königreich', $choices['GB']);
$this->assertArrayHasKey('US', $choices);
$this->assertEquals('Vereinigte Staaten', $choices['US']);
$this->assertArrayHasKey('FR', $choices);
$this->assertEquals('Frankreich', $choices['FR']);
$this->assertArrayHasKey('MY', $choices);
$this->assertEquals('Malaysia', $choices['MY']);
}

public function testUnknownCountryIsNotIncluded()
{
$field = new CountryField('country');
$choices = $field->getOtherChoices();

$this->assertArrayNotHasKey('ZZ', $choices);
}
}

0 comments on commit 9db7db4

Please sign in to comment.