TextHelper.php 3.6 KB

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