Permalink
Browse files

a new take on toList and more tests for passing a different separator

  • Loading branch information...
1 parent 398113f commit 53bfc8cca2263467a2dbbc628e85d51aafb2b767 @dogmatic69 dogmatic69 committed with markstory Nov 24, 2009
Showing with 10 additions and 14 deletions.
  1. +2 −12 cake/libs/view/helpers/text.php
  2. +8 −2 cake/tests/cases/libs/view/helpers/text.test.php
@@ -331,18 +331,8 @@ function excerpt($text, $phrase, $radius = 100, $ending = '...') {
* @return string
* @access public
*/
- function toList($list, $and = 'and') {
- $return = '';
- $count = count($list) - 1;
- $counter = 0;
- foreach ($list as $i => $item) {
- $return .= $item;
- if ($count > 0 && $counter < $count) {
- $return .= ($counter < $count - 1 ? ', ' : " {$and} ");
- }
- $counter++;
- }
- return $return;
+ function toList($list, $and = 'and', $separator = ', ') {
+ return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop(array_slice($list, -1));
}
}
?>
@@ -363,8 +363,14 @@ function testListGeneration() {
$result = $this->Text->toList(array('Dusty', 'Lucky', 'Ned'), 'y');
$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');
+ $result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'y');
+ $this->assertEqual($result, 'Dusty, Lucky y Ned');
+
+ $result = $this->Text->toList(array( 1 => 'Dusty', 2 => 'Lucky', 3 => 'Ned'), 'and', ' + ');
+ $this->assertEqual($result, 'Dusty + Lucky and Ned');
+
+ $result = $this->Text->toList(array( 'name1' => 'Dusty', 'name2' => 'Lucky'));
+ $this->assertEqual($result, 'Dusty and Lucky');
}
}
?>

0 comments on commit 53bfc8c

Please sign in to comment.