sfCultureInfo::simplify() implementation may be a little modernized using foreach() instead of for() loops. Resulting code is more concise and a little faster, especially since it may handle relatively big arrays (eg. the 195-item languages list).
Here is the patch:
Index: lib/i18n/sfCultureInfo.class.php
===================================================================
--- lib/i18n/sfCultureInfo.class.php (révision 4161)
+++ lib/i18n/sfCultureInfo.class.php (copie de travail)
@@ -623,15 +623,13 @@
*/
protected function simplify($array)
{
- for ($i = 0, $max = count($array); $i < $max; $i++)
- {
- $key = key($array);
- if (is_array($array[$key]) && count($array[$key]) == 1)
- {
- $array[$key] = $array[$key][0];
- }
- next($array);
- }
+ foreach ($array as &$item)
+ {
+ if (is_array($item) && count($item) == 1)
+ {
+ $item = $item[0];
+ }
+ }
return $array;
}
I also wrote some basic unit tests:
<?php
include(dirname(__FILE__).'/../bootstrap/unit.php');
require_once($sf_symfony_lib_dir.'/i18n/sfCultureInfo.class.php');
$array1 = array(0 => 'hello', 1 => 'world');
$array2 = array(0 => array('hello'), 1 => 'world');
$array3 = array(0 => array('hello', 'hi'), 1 => 'world');
$ci = new sfCultureInfo;
$t = new lime_test(4, new lime_output_color());
$t->diag('sfCultureInfo::simplify()');
$t->isa_ok($ci->simplify($array1), 'array', 'returns an array');
$t->is_deeply($ci->simplify($array1), $array1, 'leaves 1D-arrays unchanged');
$t->is_deeply($ci->simplify($array2), $array1, 'simplifies arrays');
$t->is_deeply($ci->simplify($array3), $array3, 'leaves not-simplifiable arrays unchanged');
Please note that those tests only work if the method is switched from protected to public, which is not necessary in "real life". By the way it could be made static as well but a quick profiling has not revealed significant performance gain.
The change seems to pass the following tests as well:
http://trac.symfony-project.com/trac/browser/trunk/test/unit/i18n/sfCultureInfoTest.php?rev=2763