FieldDisplay.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ mil+JUpF8/G9zBd15XnBTHX77sftjfhQHORFSiXPaA8=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\View\Helper;
  13. /**
  14. * displays a field's value if the field exists or false otherwise
  15. * also display array and serialized fields, and glue them by a selected implode separator
  16. *
  17. * Class FieldDisplay
  18. *
  19. * @package Cube\View\Helper
  20. */
  21. class FieldDisplay extends AbstractHelper
  22. {
  23. const DISPLAY_FALSE = 'n/a';
  24. const IMPLODE_GLUE = ', ';
  25. /**
  26. *
  27. * returns the formatted field for display purposes
  28. *
  29. * @param mixed $field
  30. * @param string $true
  31. * @param string $false
  32. * @param string $glue
  33. *
  34. * @return string
  35. */
  36. public function fieldDisplay($field, $true = null, $false = null, $glue = null)
  37. {
  38. $glue = ($glue !== null) ? (string)$glue : self::IMPLODE_GLUE;
  39. if ($field) {
  40. if (is_array($field)) {
  41. return (implode($glue, $field));
  42. }
  43. if (($array = @unserialize(html_entity_decode($field))) !== false) {
  44. return implode($glue, $array);
  45. }
  46. return ($true === null) ? $field : $true;
  47. }
  48. else {
  49. return ($false === null) ? self::DISPLAY_FALSE : $false;
  50. }
  51. }
  52. }