fixes Text::toList to allow passing array( 1=>"abc", 2=>"abc" ) and the updated test case

Signed-off-by: Mark Story <mark@mark-story.com>
This commit is contained in:
dogmatic 2009-11-24 21:11:22 +02:00 committed by mark_story
parent d758dbc343
commit 51d0805ce0
2 changed files with 11 additions and 7 deletions

View file

@ -309,16 +309,17 @@ class TextHelper extends AppHelper {
* @access public * @access public
*/ */
function toList($list, $and = 'and') { function toList($list, $and = 'and') {
$r = ''; $return = '';
$c = count($list) - 1; $count = count($list) - 1;
$counter = 0;
foreach ($list as $i => $item) { foreach ($list as $i => $item) {
$r .= $item; $return .= $item;
if ($c > 0 && $i < $c) if ($count > 0 && $counter < $count) {
{ $return .= ($counter < $count - 1 ? ', ' : " {$and} ");
$r .= ($i < $c - 1 ? ', ' : " {$and} ");
} }
$counter++;
} }
return $r; return $return;
} }
/** /**
* Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax. * Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.

View file

@ -343,6 +343,9 @@ class TextHelperTest extends CakeTestCase {
$result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y'); $result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
$this->assertEqual($result, 'Dusty, Lucky y Ned'); $this->assertEqual($result, 'Dusty, Lucky y Ned');
$result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
$this->assertEqual($result, 'Dusty, Lucky y Ned');
} }
} }
?> ?>