|
|
@@ -16,12 +16,9 @@ declare(strict_types=1);
|
|
|
*/
|
|
|
namespace Cake\View\Helper;
|
|
|
|
|
|
-use Cake\Core\App;
|
|
|
-use Cake\Core\Exception\CakeException;
|
|
|
use Cake\Utility\Security;
|
|
|
use Cake\Utility\Text;
|
|
|
use Cake\View\Helper;
|
|
|
-use Cake\View\View;
|
|
|
|
|
|
/**
|
|
|
* Text helper library.
|
|
|
@@ -29,6 +26,12 @@ use Cake\View\View;
|
|
|
* Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
|
|
|
*
|
|
|
* @property \Cake\View\Helper\HtmlHelper $Html
|
|
|
+ * @method string excerpt(string $text, string $phrase, int $radius = 100, string $ending = '...') See Text::excerpt()
|
|
|
+ * @method string highlight(string $text, array|string $phrase, array $options = []) See Text::highlight()
|
|
|
+ * @method string slug(string $string, array|string $options = []) See Text::slug()
|
|
|
+ * @method string tail(string $text, int $length = 100, array $options = []) See Text::tail()
|
|
|
+ * @method string toList(array $list, ?string $and = null, string $separator = ', ') See Text::toList()
|
|
|
+ * @method string truncate(string $text, int $length = 100, array $options = []) See Text::truncate()
|
|
|
* @link https://book.cakephp.org/4/en/views/helpers/text.html
|
|
|
* @see \Cake\Utility\Text
|
|
|
*/
|
|
|
@@ -42,15 +45,6 @@ class TextHelper extends Helper
|
|
|
protected array $helpers = ['Html'];
|
|
|
|
|
|
/**
|
|
|
- * Default config for this class
|
|
|
- *
|
|
|
- * @var array<string, mixed>
|
|
|
- */
|
|
|
- protected array $_defaultConfig = [
|
|
|
- 'engine' => Text::class,
|
|
|
- ];
|
|
|
-
|
|
|
- /**
|
|
|
* An array of hashes and their contents.
|
|
|
* Used when inserting links into text.
|
|
|
*
|
|
|
@@ -59,40 +53,6 @@ class TextHelper extends Helper
|
|
|
protected array $_placeholders = [];
|
|
|
|
|
|
/**
|
|
|
- * Cake Utility Text instance
|
|
|
- *
|
|
|
- * @var \Cake\Utility\Text
|
|
|
- */
|
|
|
- protected $_engine;
|
|
|
-
|
|
|
- /**
|
|
|
- * Constructor
|
|
|
- *
|
|
|
- * ### Settings:
|
|
|
- *
|
|
|
- * - `engine` Class name to use to replace String functionality.
|
|
|
- * The class needs to be placed in the `Utility` directory.
|
|
|
- *
|
|
|
- * @param \Cake\View\View $view the view object the helper is attached to.
|
|
|
- * @param array<string, mixed> $config Settings array Settings array
|
|
|
- * @throws \Cake\Core\Exception\CakeException when the engine class could not be found.
|
|
|
- */
|
|
|
- public function __construct(View $view, array $config = [])
|
|
|
- {
|
|
|
- parent::__construct($view, $config);
|
|
|
-
|
|
|
- $config = $this->_config;
|
|
|
-
|
|
|
- /** @psalm-var class-string<\Cake\Utility\Text>|null $engineClass */
|
|
|
- $engineClass = App::className($config['engine'], 'Utility');
|
|
|
- if ($engineClass === null) {
|
|
|
- throw new CakeException(sprintf('Class for %s could not be found', $config['engine']));
|
|
|
- }
|
|
|
-
|
|
|
- $this->_engine = new $engineClass($config);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Call methods from String utility class
|
|
|
*
|
|
|
* @param string $method Method to invoke
|
|
|
@@ -101,7 +61,7 @@ class TextHelper extends Helper
|
|
|
*/
|
|
|
public function __call(string $method, array $params): mixed
|
|
|
{
|
|
|
- return $this->_engine->{$method}(...$params);
|
|
|
+ return Text::{$method}(...$params);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -277,22 +237,6 @@ class TextHelper extends Helper
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Highlights a given phrase in a text. You can specify any expression in highlighter that
|
|
|
- * may include the \1 expression to include the $phrase found.
|
|
|
- *
|
|
|
- * @param string $text Text to search the phrase in
|
|
|
- * @param string $phrase The phrase that will be searched
|
|
|
- * @param array<string, mixed> $options An array of HTML attributes and options.
|
|
|
- * @return string The highlighted text
|
|
|
- * @see \Cake\Utility\Text::highlight()
|
|
|
- * @link https://book.cakephp.org/4/en/views/helpers/text.html#highlighting-substrings
|
|
|
- */
|
|
|
- public function highlight(string $text, string $phrase, array $options = []): string
|
|
|
- {
|
|
|
- return $this->_engine->highlight($text, $phrase, $options);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Formats paragraphs around given text for all line breaks
|
|
|
* <br /> added for single line return
|
|
|
* <p> added for double line return
|
|
|
@@ -319,111 +263,6 @@ class TextHelper extends Helper
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Truncates text.
|
|
|
- *
|
|
|
- * Cuts a string to the length of $length and replaces the last characters
|
|
|
- * with the ellipsis if the text is longer than length.
|
|
|
- *
|
|
|
- * ### Options:
|
|
|
- *
|
|
|
- * - `ellipsis` Will be used as Ending and appended to the trimmed string
|
|
|
- * - `exact` If false, $text will not be cut mid-word
|
|
|
- * - `html` If true, HTML tags would be handled correctly
|
|
|
- *
|
|
|
- * @param string $text String to truncate.
|
|
|
- * @param int $length Length of returned string, including ellipsis.
|
|
|
- * @param array<string, mixed> $options An array of HTML attributes and options.
|
|
|
- * @return string Trimmed string.
|
|
|
- * @see \Cake\Utility\Text::truncate()
|
|
|
- * @link https://book.cakephp.org/4/en/views/helpers/text.html#truncating-text
|
|
|
- */
|
|
|
- public function truncate(string $text, int $length = 100, array $options = []): string
|
|
|
- {
|
|
|
- return $this->_engine->truncate($text, $length, $options);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Truncates text starting from the end.
|
|
|
- *
|
|
|
- * Cuts a string to the length of $length and replaces the first characters
|
|
|
- * with the ellipsis if the text is longer than length.
|
|
|
- *
|
|
|
- * ### Options:
|
|
|
- *
|
|
|
- * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
|
|
|
- * - `exact` If false, $text will not be cut mid-word
|
|
|
- *
|
|
|
- * @param string $text String to truncate.
|
|
|
- * @param int $length Length of returned string, including ellipsis.
|
|
|
- * @param array<string, mixed> $options An array of HTML attributes and options.
|
|
|
- * @return string Trimmed string.
|
|
|
- * @see \Cake\Utility\Text::tail()
|
|
|
- * @link https://book.cakephp.org/4/en/views/helpers/text.html#truncating-the-tail-of-a-string
|
|
|
- */
|
|
|
- public function tail(string $text, int $length = 100, array $options = []): string
|
|
|
- {
|
|
|
- return $this->_engine->tail($text, $length, $options);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
|
|
|
- * determined by radius.
|
|
|
- *
|
|
|
- * @param string $text String to search the phrase in
|
|
|
- * @param string $phrase Phrase that will be searched for
|
|
|
- * @param int $radius The amount of characters that will be returned on each side of the founded phrase
|
|
|
- * @param string $ending Ending that will be appended
|
|
|
- * @return string Modified string
|
|
|
- * @see \Cake\Utility\Text::excerpt()
|
|
|
- * @link https://book.cakephp.org/4/en/views/helpers/text.html#extracting-an-excerpt
|
|
|
- */
|
|
|
- public function excerpt(string $text, string $phrase, int $radius = 100, string $ending = '...'): string
|
|
|
- {
|
|
|
- return $this->_engine->excerpt($text, $phrase, $radius, $ending);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Creates a comma separated list where the last two items are joined with 'and', forming natural language.
|
|
|
- *
|
|
|
- * @param array<string> $list The list to be joined.
|
|
|
- * @param string|null $and The word used to join the last and second last items together with. Defaults to 'and'.
|
|
|
- * @param string $separator The separator used to join all the other items together. Defaults to ', '.
|
|
|
- * @return string The glued together string.
|
|
|
- * @see \Cake\Utility\Text::toList()
|
|
|
- * @link https://book.cakephp.org/4/en/views/helpers/text.html#converting-an-array-to-sentence-form
|
|
|
- */
|
|
|
- public function toList(array $list, ?string $and = null, string $separator = ', '): string
|
|
|
- {
|
|
|
- return $this->_engine->toList($list, $and, $separator);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Returns a string with all spaces converted to dashes (by default),
|
|
|
- * characters transliterated to ASCII characters, and non word characters removed.
|
|
|
- *
|
|
|
- * ### Options:
|
|
|
- *
|
|
|
- * - `replacement`: Replacement string. Default '-'.
|
|
|
- * - `transliteratorId`: A valid transliterator id string.
|
|
|
- * If `null` (default) the transliterator (identifier) set via
|
|
|
- * `Text::setTransliteratorId()` or `Text::setTransliterator()` will be used.
|
|
|
- * If `false` no transliteration will be done, only non words will be removed.
|
|
|
- * - `preserve`: Specific non-word character to preserve. Default `null`.
|
|
|
- * For e.g. this option can be set to '.' to generate clean file names.
|
|
|
- *
|
|
|
- * @param string $string the string you want to slug
|
|
|
- * @param array|string $options If string it will be use as replacement character
|
|
|
- * or an array of options.
|
|
|
- * @return string
|
|
|
- * @see \Cake\Utility\Text::setTransliterator()
|
|
|
- * @see \Cake\Utility\Text::setTransliteratorId()
|
|
|
- */
|
|
|
- public function slug(string $string, array|string $options = []): string
|
|
|
- {
|
|
|
- return $this->_engine->slug($string, $options);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Event listeners.
|
|
|
*
|
|
|
* @return array<string, mixed>
|