TextHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Tools\View\Helper;
  3. use Cake\Utility\Hash;
  4. use Cake\View\Helper\TextHelper as CakeTextHelper;
  5. use Cake\View\View;
  6. use Tools\Utility\Number;
  7. use Tools\Utility\Utility;
  8. if (!defined('CHAR_HELLIP')) {
  9. define('CHAR_HELLIP', '&#8230;'); # � (horizontal ellipsis = three dot leader)
  10. }
  11. /**
  12. * This helper extends the core Text helper and adds some improvements.
  13. *
  14. * autoLinkEmails
  15. * - obfuscate (defaults to FALSE right now)
  16. * (- maxLength?)
  17. * - escape (defaults to TRUE for security reasons regarding plain text)
  18. *
  19. * autoLinkUrls
  20. * - stripProtocol (defaults To FALSE right now)
  21. * - maxLength (to shorten links in order to not mess up the layout in some cases - appends ...)
  22. * - escape (defaults to TRUE for security reasons regarding plain text)
  23. *
  24. * @property \Cake\View\Helper\HtmlHelper $Html
  25. */
  26. class TextHelper extends CakeTextHelper {
  27. /**
  28. * ### Settings:
  29. *
  30. * - `engine` Class name to use to replace Text functionality.
  31. * The class needs to be placed in the `Utility` directory.
  32. *
  33. * @param \Cake\View\View $View the view object the helper is attached to.
  34. * @param array $config Settings array Settings array
  35. * @throws \Cake\Core\Exception\Exception when the engine class could not be found.
  36. */
  37. public function __construct(View $View, array $config = []) {
  38. $config = Hash::merge(['engine' => 'Tools.Text'], $config);
  39. parent::__construct($View, $config);
  40. }
  41. /**
  42. * Minimizes the given URL to a maximum length
  43. *
  44. * @param string $url the url
  45. * @param int|null $max the maximum length
  46. * @param array $options
  47. * - placeholder
  48. * @return string the manipulated url (+ eventuell ...)
  49. */
  50. public function minimizeUrl($url, $max = null, array $options = []) {
  51. // check if there is nothing to do
  52. if (empty($url) || mb_strlen($url) <= (int)$max) {
  53. return $url;
  54. }
  55. // http:// etc has not to be displayed, so
  56. $url = Utility::stripProtocol($url);
  57. // cut the parameters
  58. if (mb_strpos($url, '/') !== false) {
  59. $url = strtok($url, '/');
  60. }
  61. // return if the url is short enough
  62. if (mb_strlen($url) <= (int)$max) {
  63. return $url;
  64. }
  65. // otherwise cut a part in the middle (but only if long enough!)
  66. // TODO: more dynamically
  67. $placeholder = CHAR_HELLIP;
  68. if (!empty($options['placeholder'])) {
  69. $placeholder = $options['placeholder'];
  70. }
  71. $end = mb_substr($url, -5, 5);
  72. $front = mb_substr($url, 0, (int)$max - 8);
  73. return $front . $placeholder . $end;
  74. }
  75. /**
  76. * Removes http:// or other protocols from the link.
  77. *
  78. * @param string $url
  79. * @param array $protocols Defaults to http and https. Pass empty array for all.
  80. * @return string strippedUrl
  81. */
  82. public function stripProtocol($url, $protocols = ['http', 'https']) {
  83. return Utility::stripProtocol($url, $protocols);
  84. }
  85. /**
  86. * Transforming int values into ordinal numbers (1st, 3rd, ...).
  87. * When using HTML, you can use <sup>, as well.
  88. *
  89. * @param int $num The number to be suffixed.
  90. * @param bool $sup Whether to wrap the suffix in a superscript (<sup>) tag on output.
  91. * @return string ordinal
  92. */
  93. public function ordinalNumber($num = 0, $sup = false) {
  94. $ordinal = Number::ordinal($num);
  95. return ($sup) ? $num . '<sup>' . $ordinal . '</sup>' : $num . $ordinal;
  96. }
  97. /**
  98. * Syntax highlighting using php internal highlighting
  99. *
  100. * @param string $file Filename
  101. * @return string
  102. */
  103. public function highlightFile($file) {
  104. return highlight_file($file, true);
  105. }
  106. /**
  107. * Syntax highlighting using php internal highlighting
  108. *
  109. * @param string $string Content
  110. * @return string
  111. */
  112. public function highlightString($string) {
  113. return highlight_string($string, true);
  114. }
  115. }