TextHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Constructor
  29. *
  30. * ### Settings:
  31. *
  32. * - `engine` Class name to use to replace Text functionality.
  33. * The class needs to be placed in the `Utility` directory.
  34. *
  35. * @param \Cake\View\View $View the view object the helper is attached to.
  36. * @param array $config Settings array Settings array
  37. * @throws \Cake\Core\Exception\Exception when the engine class could not be found.
  38. */
  39. public function __construct(View $View, array $config = []) {
  40. $config = Hash::merge(['engine' => 'Tools.Text'], $config);
  41. parent::__construct($View, $config);
  42. }
  43. /**
  44. * Minimizes the given URL to a maximum length
  45. *
  46. * @param string $url the url
  47. * @param int|null $max the maximum length
  48. * @param array $options
  49. * - placeholder
  50. * @return string the manipulated url (+ eventuell ...)
  51. */
  52. public function minimizeUrl($url, $max = null, array $options = []) {
  53. // check if there is nothing to do
  54. if (empty($url) || mb_strlen($url) <= (int)$max) {
  55. return $url;
  56. }
  57. // http:// etc has not to be displayed, so
  58. $url = Utility::stripProtocol($url);
  59. // cut the parameters
  60. if (mb_strpos($url, '/') !== false) {
  61. $url = strtok($url, '/');
  62. }
  63. // return if the url is short enough
  64. if (mb_strlen($url) <= (int)$max) {
  65. return $url;
  66. }
  67. // otherwise cut a part in the middle (but only if long enough!)
  68. // TODO: more dynamically
  69. $placeholder = CHAR_HELLIP;
  70. if (!empty($options['placeholder'])) {
  71. $placeholder = $options['placeholder'];
  72. }
  73. $end = mb_substr($url, -5, 5);
  74. $front = mb_substr($url, 0, (int)$max - 8);
  75. return $front . $placeholder . $end;
  76. }
  77. /**
  78. * Removes http:// or other protocols from the link.
  79. *
  80. * @param string $url
  81. * @param array $protocols Defaults to http and https. Pass empty array for all.
  82. * @return string strippedUrl
  83. */
  84. public function stripProtocol($url, $protocols = ['http', 'https']) {
  85. return Utility::stripProtocol($url, $protocols);
  86. }
  87. /**
  88. * Transforming int values into ordinal numbers (1st, 3rd, ...).
  89. * When using HTML, you can use <sup>, as well.
  90. *
  91. * @param int $num The number to be suffixed.
  92. * @param bool $sup Whether to wrap the suffix in a superscript (<sup>) tag on output.
  93. * @return string ordinal
  94. */
  95. public function ordinalNumber($num = 0, $sup = false) {
  96. $ordinal = Number::ordinal($num);
  97. return ($sup) ? $num . '<sup>' . $ordinal . '</sup>' : $num . $ordinal;
  98. }
  99. /**
  100. * Syntax highlighting using php internal highlighting
  101. *
  102. * @param string $file Filename
  103. * @return string
  104. */
  105. public function highlightFile($file) {
  106. return highlight_file($file, true);
  107. }
  108. /**
  109. * Syntax highlighting using php internal highlighting
  110. *
  111. * @param string $string Content
  112. * @return string
  113. */
  114. public function highlightString($string) {
  115. return highlight_string($string, true);
  116. }
  117. }