Development

Changeset 11842

You must first sign up to be able to contribute.

Changeset 11842

Show
Ignore:
Timestamp:
09/29/08 12:53:28 (6 years ago)
Author:
fabien
Message:

[1.2] added an optional argument to some sfCulture::* methods

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r11841 r11842  
    14001400configuration classes. 
    14011401 
     1402The `sfCulture::getCountries()`, `sfCulture::getCurrencies()`, and 
     1403`sfCulture::getLanguages()` methods now take an optional argument which allows 
     1404to restrict the return value: 
     1405 
     1406    [php] 
     1407    // will only return the FR and ES countries in english 
     1408    $countries = sfCultureInfo::getInstance('en')->getCountries(array('FR', 'ES')); 
     1409 
    14021410Exception Templates 
    14031411------------------- 
  • branches/1.2/lib/i18n/sfCultureInfo.class.php

    r11700 r11842  
    660660   * Gets a list of countries in the language of the localized version. 
    661661   * 
     662   * @param  array An array of countries used to restrict the returned array (null by default, which means all countries) 
     663   * 
    662664   * @return array a list of localized country names.  
    663665   */ 
    664   public function getCountries() 
    665   { 
    666     return $this->simplify($this->findInfo('Countries', true)); 
     666  public function getCountries($countries = null) 
     667  { 
     668    $allCountries = $this->simplify($this->findInfo('Countries', true)); 
     669 
     670    // restrict countries to a sub-set 
     671    if (!is_null($countries)) 
     672    { 
     673      if ($problems = array_diff($countries, array_keys($allCountries))) 
     674      { 
     675        throw new InvalidArgumentException(sprintf('The following countries do not exist: %s.', implode(', ', $problems))); 
     676      } 
     677 
     678      $allCountries = array_intersect_key($allCountries, array_flip($countries)); 
     679    } 
     680 
     681    asort($allCountries); 
     682 
     683    return $allCountries; 
    667684  } 
    668685 
     
    670687   * Gets a list of currencies in the language of the localized version. 
    671688   * 
     689   * @param  array An array of currencies used to restrict the returned array (null by default, which means all currencies) 
     690   * 
    672691   * @return array a list of localized currencies. 
    673692   */ 
    674   public function getCurrencies() 
    675   { 
    676     return $this->findInfo('Currencies', true); 
     693  public function getCurrencies($currencies = null) 
     694  { 
     695    $allCurrencies = $this->findInfo('Currencies', true); 
     696 
     697    // restrict countries to a sub-set 
     698    if (!is_null($currencies)) 
     699    { 
     700      if ($problems = array_diff($currencies, array_keys($allCurrencies))) 
     701      { 
     702        throw new InvalidArgumentException(sprintf('The following currencies do not exist: %s.', implode(', ', $problems))); 
     703      } 
     704 
     705      $allCurrencies = array_intersect_key($allCurrencies, array_flip($currencies)); 
     706    } 
     707 
     708    asort($allCurrencies); 
     709 
     710    return $allCurrencies; 
    677711  } 
    678712 
     
    680714   * Gets a list of languages in the language of the localized version. 
    681715   * 
     716   * @param  array An array of languages used to restrict the returned array (null by default, which means all languages) 
     717   * 
    682718   * @return array list of localized language names. 
    683719   */ 
    684   public function getLanguages() 
    685   { 
    686     return $this->simplify($this->findInfo('Languages', true)); 
     720  public function getLanguages($languages = null) 
     721  { 
     722    $allLanguages = $this->simplify($this->findInfo('Languages', true)); 
     723 
     724    // restrict languages to a sub-set 
     725    if (!is_null($languages)) 
     726    { 
     727      if ($problems = array_diff($languages, array_keys($allLanguages))) 
     728      { 
     729        throw new InvalidArgumentException(sprintf('The following languages do not exist: %s.', implode(', ', $problems))); 
     730      } 
     731 
     732      $allLanguages = array_intersect_key($allLanguages, array_flip($languages)); 
     733    } 
     734 
     735    asort($allLanguages); 
     736 
     737    return $allLanguages; 
    687738  } 
    688739 
  • branches/1.2/lib/widget/i18n/sfWidgetFormI18nSelectCountry.class.php

    r11700 r11842  
    2424   * Available options: 
    2525   * 
    26    *  * culture:    The culture to use for internationalized strings (required) 
    27    *  * countries: An array of country codes to use (ISO 3166) 
    28    *  * add_empty: Whether to add a first empty value or not (false by default) 
    29    *                If the option is not a Boolean, the value will be used as the text value 
     26   *  * culture:   The culture to use for internationalized strings (required) 
     27   *  * countries: An array of country codes to use (ISO 3166) 
     28   *  * add_empty: Whether to add a first empty value or not (false by default) 
     29   *               If the option is not a Boolean, the value will be used as the text value 
    3030   * 
    3131   * @param array $options     An array of options 
     
    4545    $culture = isset($options['culture']) ? $options['culture'] : 'en'; 
    4646 
    47     $countries = sfCultureInfo::getInstance($culture)->getCountries(); 
     47    $countries = sfCultureInfo::getInstance($culture)->getCountries(isset($options['countries']) ? $options['countries'] : null); 
    4848 
    49     // restrict countries to a sub-set 
    50     if (isset($options['countries'])) 
    51     { 
    52       if ($problems = array_diff($options['countries'], array_keys($countries))) 
    53       { 
    54         throw new InvalidArgumentException(sprintf('The following countries do not exist: %s.', implode(', ', $problems))); 
    55       } 
    56  
    57       $countries = array_intersect_key($countries, array_flip($options['countries'])); 
    58     } 
    59  
    60     asort($countries); 
    6149    $addEmpty = isset($options['add_empty']) ? $options['add_empty'] : false; 
    6250    if (false !== $addEmpty) 
  • branches/1.2/lib/widget/i18n/sfWidgetFormI18nSelectLanguage.class.php

    r11700 r11842  
    2626   *  * culture:   The culture to use for internationalized strings (required) 
    2727   *  * languages: An array of language codes to use (ISO 639-1) 
    28    *  * add_empty: Whether to add a first empty value or not (false by default) 
    29    *                If the option is not a Boolean, the value will be used as the text value 
     28   *  * add_empty: Whether to add a first empty value or not (false by default) 
     29   *               If the option is not a Boolean, the value will be used as the text value 
    3030   * 
    3131   * @param array $options     An array of options 
     
    4545    $culture = isset($options['culture']) ? $options['culture'] : 'en'; 
    4646 
    47     $languages = sfCultureInfo::getInstance($culture)->getLanguages(); 
     47    $languages = sfCultureInfo::getInstance($culture)->getLanguages(isset($options['languages']) ? $options['languages'] : null); 
    4848 
    49     // restrict languages to a sub-set 
    50     if (isset($options['languages'])) 
    51     { 
    52       if ($problems = array_diff($options['languages'], array_keys($languages))) 
    53       { 
    54         throw new InvalidArgumentException(sprintf('The following languages do not exist: %s.', implode(', ', $problems))); 
    55       } 
    56  
    57       $languages = array_intersect_key($languages, array_flip($options['languages'])); 
    58     } 
    59  
    60     asort($languages); 
    6149    $addEmpty = isset($options['add_empty']) ? $options['add_empty'] : false; 
    6250    if (false !== $addEmpty) 
  • branches/1.2/test/unit/i18n/sfCultureInfoTest.php

    r11700 r11842  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(56, new lime_output_color()); 
     13$t = new lime_test(62, new lime_output_color()); 
    1414 
    1515// ->getInstance() 
     
    4848$t->is($languages_en, $c_en->Languages, '->getLanguages() is equivalent to ->Languages'); 
    4949 
     50$languages = $c_en->getLanguages(array('fr', 'es')); 
     51$t->is(array_keys($languages), array('fr', 'es'), '->getLanguages() takes an array of languages as its first argument'); 
     52 
     53try 
     54{ 
     55  $c_en->getLanguages(array('fr', 'gb')); 
     56  $t->fail('->getLanguages() throws an Exception if the list of given languages contains some invalid ones.'); 
     57} 
     58catch (Exception $e) 
     59{ 
     60  $t->pass('->getLanguages() throws an Exception if the list of given languages contains some invalid ones.'); 
     61} 
     62 
    5063// ->getCurrencies() 
    5164$t->diag('->getCurrencies()'); 
     
    5669$t->is($currencies_en, $c_en->Currencies, '->getCurrencies() is equivalent to ->Currencies'); 
    5770 
     71$currencies = $c_en->getCurrencies(array('USD', 'EUR')); 
     72$t->is(array_keys($currencies), array('USD', 'EUR'), '->getCurrencies() takes an array of currencies as its first argument'); 
     73 
     74try 
     75{ 
     76  $c_en->getCurrencies(array('USD', 'FRANCS')); 
     77  $t->fail('->getCurrencies() throws an Exception if the list of given currencies contains some invalid ones.'); 
     78} 
     79catch (Exception $e) 
     80{ 
     81  $t->pass('->getCurrencies() throws an Exception if the list of given currencies contains some invalid ones.'); 
     82} 
     83 
    5884// ->getCountries() 
    5985$t->diag('->getCountries()'); 
     
    6490$t->is($countries_en, $c_en->Countries, '->getCountries() is equivalent to ->Countries'); 
    6591 
     92$countries = $c_en->getCountries(array('FR', 'ES')); 
     93$t->is(array_keys($countries), array('FR', 'ES'), '->getCountries() takes an array of countries as its first argument'); 
     94 
     95try 
     96{ 
     97  $c_en->getCountries(array('FR', 'EN')); 
     98  $t->fail('->getCountries() throws an Exception if the list of given countries contains some invalid ones.'); 
     99} 
     100catch (Exception $e) 
     101{ 
     102  $t->pass('->getCountries() throws an Exception if the list of given countries contains some invalid ones.'); 
     103} 
     104 
    66105// ->getScripts() 
    67106$t->diag('->getScripts()');