PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/View/Helper/TextHelper.php

https://gitlab.com/nghiep5890/bakery
PHP | 345 lines | 113 code | 23 blank | 209 comment | 6 complexity | 9a31bee9880e22cab803cd6a2baad810 MD5 | raw file
  1. <?php
  2. /**
  3. * Text Helper
  4. *
  5. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Helper
  17. * @since CakePHP(tm) v 0.10.0.1076
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('AppHelper', 'View/Helper');
  21. App::uses('Hash', 'Utility');
  22. /**
  23. * Text helper library.
  24. *
  25. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  26. *
  27. * @package Cake.View.Helper
  28. * @property HtmlHelper $Html
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
  30. * @see String
  31. */
  32. class TextHelper extends AppHelper {
  33. /**
  34. * helpers
  35. *
  36. * @var array
  37. */
  38. public $helpers = array('Html');
  39. /**
  40. * An array of md5sums and their contents.
  41. * Used when inserting links into text.
  42. *
  43. * @var array
  44. */
  45. protected $_placeholders = array();
  46. /**
  47. * CakeText utility instance
  48. *
  49. * @var stdClass
  50. */
  51. protected $_engine;
  52. /**
  53. * Constructor
  54. *
  55. * ### Settings:
  56. *
  57. * - `engine` Class name to use to replace CakeText functionality.
  58. * The class needs to be placed in the `Utility` directory.
  59. *
  60. * @param View $View the view object the helper is attached to.
  61. * @param array $settings Settings array Settings array
  62. * @throws CakeException when the engine class could not be found.
  63. */
  64. public function __construct(View $View, $settings = array()) {
  65. $settings = Hash::merge(array('engine' => 'CakeText'), $settings);
  66. parent::__construct($View, $settings);
  67. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  68. App::uses($engineClass, $plugin . 'Utility');
  69. if (class_exists($engineClass)) {
  70. $this->_engine = new $engineClass($settings);
  71. } else {
  72. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  73. }
  74. }
  75. /**
  76. * Call methods from CakeText utility class
  77. *
  78. * @param string $method Method to call.
  79. * @param array $params Parameters to pass to method.
  80. * @return mixed Whatever is returned by called method, or false on failure
  81. */
  82. public function __call($method, $params) {
  83. return call_user_func_array(array($this->_engine, $method), $params);
  84. }
  85. /**
  86. * Adds links (<a href=....) to a given text, by finding text that begins with
  87. * strings like http:// and ftp://.
  88. *
  89. * ### Options
  90. *
  91. * - `escape` Control HTML escaping of input. Defaults to true.
  92. *
  93. * @param string $text Text
  94. * @param array $options Array of HTML options, and options listed above.
  95. * @return string The text with links
  96. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
  97. */
  98. public function autoLinkUrls($text, $options = array()) {
  99. $this->_placeholders = array();
  100. $options += array('escape' => true);
  101. $pattern = '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[\p{L}0-9.\-_:]+(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#i';
  102. $text = preg_replace_callback(
  103. $pattern,
  104. array(&$this, '_insertPlaceHolder'),
  105. $text
  106. );
  107. $text = preg_replace_callback(
  108. '#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://|//)www\.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
  109. array(&$this, '_insertPlaceHolder'),
  110. $text
  111. );
  112. if ($options['escape']) {
  113. $text = h($text);
  114. }
  115. return $this->_linkUrls($text, $options);
  116. }
  117. /**
  118. * Saves the placeholder for a string, for later use. This gets around double
  119. * escaping content in URL's.
  120. *
  121. * @param array $matches An array of regexp matches.
  122. * @return string Replaced values.
  123. */
  124. protected function _insertPlaceHolder($matches) {
  125. $key = md5($matches[0]);
  126. $this->_placeholders[$key] = $matches[0];
  127. return $key;
  128. }
  129. /**
  130. * Replace placeholders with links.
  131. *
  132. * @param string $text The text to operate on.
  133. * @param array $htmlOptions The options for the generated links.
  134. * @return string The text with links inserted.
  135. */
  136. protected function _linkUrls($text, $htmlOptions) {
  137. $replace = array();
  138. foreach ($this->_placeholders as $hash => $url) {
  139. $link = $url;
  140. if (!preg_match('#^[a-z]+\://#', $url)) {
  141. $url = 'http://' . $url;
  142. }
  143. $replace[$hash] = $this->Html->link($link, $url, $htmlOptions);
  144. }
  145. return strtr($text, $replace);
  146. }
  147. /**
  148. * Links email addresses
  149. *
  150. * @param string $text The text to operate on
  151. * @param array $options An array of options to use for the HTML.
  152. * @return string
  153. * @see TextHelper::autoLinkEmails()
  154. */
  155. protected function _linkEmails($text, $options) {
  156. $replace = array();
  157. foreach ($this->_placeholders as $hash => $url) {
  158. $replace[$hash] = $this->Html->link($url, 'mailto:' . $url, $options);
  159. }
  160. return strtr($text, $replace);
  161. }
  162. /**
  163. * Adds email links (<a href="mailto:....) to a given text.
  164. *
  165. * ### Options
  166. *
  167. * - `escape` Control HTML escaping of input. Defaults to true.
  168. *
  169. * @param string $text Text
  170. * @param array $options Array of HTML options, and options listed above.
  171. * @return string The text with links
  172. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
  173. */
  174. public function autoLinkEmails($text, $options = array()) {
  175. $options += array('escape' => true);
  176. $this->_placeholders = array();
  177. $atom = '[\p{L}0-9!#$%&\'*+\/=?^_`{|}~-]';
  178. $text = preg_replace_callback(
  179. '/(?<=\s|^|\(|\>|\;)(' . $atom . '*(?:\.' . $atom . '+)*@[\p{L}0-9-]+(?:\.[\p{L}0-9-]+)+)/ui',
  180. array(&$this, '_insertPlaceholder'),
  181. $text
  182. );
  183. if ($options['escape']) {
  184. $text = h($text);
  185. }
  186. return $this->_linkEmails($text, $options);
  187. }
  188. /**
  189. * Convert all links and email addresses to HTML links.
  190. *
  191. * ### Options
  192. *
  193. * - `escape` Control HTML escaping of input. Defaults to true.
  194. *
  195. * @param string $text Text
  196. * @param array $options Array of HTML options, and options listed above.
  197. * @return string The text with links
  198. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
  199. */
  200. public function autoLink($text, $options = array()) {
  201. $text = $this->autoLinkUrls($text, $options);
  202. return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
  203. }
  204. /**
  205. * Highlights a given phrase in a text. You can specify any expression in highlighter that
  206. * may include the \1 expression to include the $phrase found.
  207. *
  208. * @param string $text Text to search the phrase in
  209. * @param string $phrase The phrase that will be searched
  210. * @param array $options An array of html attributes and options.
  211. * @return string The highlighted text
  212. * @see CakeText::highlight()
  213. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
  214. */
  215. public function highlight($text, $phrase, $options = array()) {
  216. return $this->_engine->highlight($text, $phrase, $options);
  217. }
  218. /**
  219. * Formats paragraphs around given text for all line breaks
  220. * <br /> added for single line return
  221. * <p> added for double line return
  222. *
  223. * @param string $text Text
  224. * @return string The text with proper <p> and <br /> tags
  225. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph
  226. */
  227. public function autoParagraph($text) {
  228. if (trim($text) !== '') {
  229. $text = preg_replace('|<br[^>]*>\s*<br[^>]*>|i', "\n\n", $text . "\n");
  230. $text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text));
  231. $texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
  232. $text = '';
  233. foreach ($texts as $txt) {
  234. $text .= '<p>' . nl2br(trim($txt, "\n")) . "</p>\n";
  235. }
  236. $text = preg_replace('|<p>\s*</p>|', '', $text);
  237. }
  238. return $text;
  239. }
  240. /**
  241. * Strips given text of all links (<a href=....)
  242. *
  243. * @param string $text Text
  244. * @return string The text without links
  245. * @see CakeText::stripLinks()
  246. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
  247. */
  248. public function stripLinks($text) {
  249. return $this->_engine->stripLinks($text);
  250. }
  251. /**
  252. * Truncates text.
  253. *
  254. * Cuts a string to the length of $length and replaces the last characters
  255. * with the ellipsis if the text is longer than length.
  256. *
  257. * ### Options:
  258. *
  259. * - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
  260. * - `exact` If false, $text will not be cut mid-word
  261. * - `html` If true, HTML tags would be handled correctly
  262. *
  263. * @param string $text String to truncate.
  264. * @param int $length Length of returned string, including ellipsis.
  265. * @param array $options An array of html attributes and options.
  266. * @return string Trimmed string.
  267. * @see CakeText::truncate()
  268. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
  269. */
  270. public function truncate($text, $length = 100, $options = array()) {
  271. return $this->_engine->truncate($text, $length, $options);
  272. }
  273. /**
  274. * Truncates text starting from the end.
  275. *
  276. * Cuts a string to the length of $length and replaces the first characters
  277. * with the ellipsis if the text is longer than length.
  278. *
  279. * ### Options:
  280. *
  281. * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
  282. * - `exact` If false, $text will not be cut mid-word
  283. *
  284. * @param string $text String to truncate.
  285. * @param int $length Length of returned string, including ellipsis.
  286. * @param array $options An array of html attributes and options.
  287. * @return string Trimmed string.
  288. * @see CakeText::tail()
  289. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::tail
  290. */
  291. public function tail($text, $length = 100, $options = array()) {
  292. return $this->_engine->tail($text, $length, $options);
  293. }
  294. /**
  295. * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
  296. * determined by radius.
  297. *
  298. * @param string $text String to search the phrase in
  299. * @param string $phrase Phrase that will be searched for
  300. * @param int $radius The amount of characters that will be returned on each side of the founded phrase
  301. * @param string $ending Ending that will be appended
  302. * @return string Modified string
  303. * @see CakeText::excerpt()
  304. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
  305. */
  306. public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  307. return $this->_engine->excerpt($text, $phrase, $radius, $ending);
  308. }
  309. /**
  310. * Creates a comma separated list where the last two items are joined with 'and', forming natural language.
  311. *
  312. * @param array $list The list to be joined.
  313. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'.
  314. * @param string $separator The separator used to join all the other items together. Defaults to ', '.
  315. * @return string The glued together string.
  316. * @see CakeText::toList()
  317. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
  318. */
  319. public function toList($list, $and = null, $separator = ', ') {
  320. return $this->_engine->toList($list, $and, $separator);
  321. }
  322. }