Fixing issue where value of '$and' was appended to output string even when list array contained only one element. Fixed #285

This commit is contained in:
ADmad 2010-01-31 23:37:54 +05:30
parent 3f9813a9ec
commit 59a4732eb8
2 changed files with 11 additions and 1 deletions

View file

@ -336,7 +336,11 @@ class TextHelper extends AppHelper {
* @access public
*/
function toList($list, $and = 'and', $separator = ', ') {
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
if (count($list) > 1) {
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
} else {
return array_pop($list);
}
}
}
?>

View file

@ -355,6 +355,12 @@ class TextHelperTest extends CakeTestCase {
* @return void
*/
function testListGeneration() {
$result = $this->Text->toList(array());
$this->assertEqual($result, '');
$result = $this->Text->toList(array('One'));
$this->assertEqual($result, 'One');
$result = $this->Text->toList(array('Larry', 'Curly', 'Moe'));
$this->assertEqual($result, 'Larry, Curly and Moe');