PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/View/Helper/FluentList.php

https://github.com/shopaholiccompany/shopaholic
PHP | 77 lines | 44 code | 8 blank | 25 comment | 17 complexity | cdc499ebb81e754f515b05e8c1c7c2a9 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_View
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: FluentList.php 7244 2010-09-01 01:49:53Z john $
  10. * @todo documentation
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_View
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Engine_View_Helper_FluentList extends Zend_View_Helper_Abstract
  19. {
  20. /**
  21. * Generates a fluent list of item. Example:
  22. * You
  23. * You and Me
  24. * You, Me, and Jenny
  25. *
  26. * @param array|Traversable $items
  27. * @return string
  28. */
  29. public function fluentList($items, $translate = false)
  30. {
  31. if( 0 === ($num = count($items)) )
  32. {
  33. return '';
  34. }
  35. $comma = $this->view->translate(',');
  36. $and = $this->view->translate('and');
  37. $index = 0;
  38. $content = '';
  39. foreach( $items as $item )
  40. {
  41. if( $num > 2 && $index > 0 ) $content .= $comma . ' '; else $content .= ' ';
  42. if( $num > 1 && $index == $num - 1 ) $content .= $and . ' ';
  43. $href = null;
  44. $title = null;
  45. if( is_object($item) ) {
  46. if( method_exists($item, 'getTitle') && method_exists($item, 'getHref') ) {
  47. $href = $item->getHref();
  48. $title = $item->getTitle();
  49. } else if( method_exists($item, '__toString') ) {
  50. $title = $item->__toString();
  51. } else {
  52. $title = (string) $item;
  53. }
  54. } else {
  55. $title = (string) $item;
  56. }
  57. if( $translate ) {
  58. $title = $this->view->translate($title);
  59. }
  60. if( null === $href ) {
  61. $content .= $title;
  62. } else {
  63. $content .= $this->view->htmlLink($href, $title);
  64. }
  65. $index++;
  66. }
  67. return $content;
  68. }
  69. }