|
|
@@ -60,27 +60,25 @@ class Text
|
|
|
* @return string RFC 4122 UUID
|
|
|
* @copyright Matt Farina MIT License https://github.com/lootils/uuid/blob/master/LICENSE
|
|
|
*/
|
|
|
- public static function uuid()
|
|
|
+ public static function uuid(): string
|
|
|
{
|
|
|
- $random = function_exists('random_int') ? 'random_int' : 'mt_rand';
|
|
|
-
|
|
|
return sprintf(
|
|
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
|
|
// 32 bits for "time_low"
|
|
|
- $random(0, 65535),
|
|
|
- $random(0, 65535),
|
|
|
+ random_int(0, 65535),
|
|
|
+ random_int(0, 65535),
|
|
|
// 16 bits for "time_mid"
|
|
|
- $random(0, 65535),
|
|
|
+ random_int(0, 65535),
|
|
|
// 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
|
|
|
- $random(0, 4095) | 0x4000,
|
|
|
+ random_int(0, 4095) | 0x4000,
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res",
|
|
|
// 8 bits for "clk_seq_low",
|
|
|
// two most significant bits holds zero and one for variant DCE1.1
|
|
|
- $random(0, 0x3fff) | 0x8000,
|
|
|
+ random_int(0, 0x3fff) | 0x8000,
|
|
|
// 48 bits for "node"
|
|
|
- $random(0, 65535),
|
|
|
- $random(0, 65535),
|
|
|
- $random(0, 65535)
|
|
|
+ random_int(0, 65535),
|
|
|
+ random_int(0, 65535),
|
|
|
+ random_int(0, 65535)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -94,7 +92,7 @@ class Text
|
|
|
* @param string $rightBound The right boundary to ignore separators in.
|
|
|
* @return array|string Array of tokens in $data or original input if empty.
|
|
|
*/
|
|
|
- public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')')
|
|
|
+ public static function tokenize(string $data, string $separator = ',', string $leftBound = '(', string $rightBound = ')')
|
|
|
{
|
|
|
if (empty($data)) {
|
|
|
return [];
|
|
|
@@ -188,14 +186,14 @@ class Text
|
|
|
* @param array $options An array of options, see description above
|
|
|
* @return string
|
|
|
*/
|
|
|
- public static function insert($str, $data, array $options = [])
|
|
|
+ public static function insert(string $str, array $data, array $options = []): string
|
|
|
{
|
|
|
$defaults = [
|
|
|
- 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
|
|
|
+ 'before' => ':', 'after' => '', 'escape' => '\\', 'format' => null, 'clean' => false
|
|
|
];
|
|
|
$options += $defaults;
|
|
|
$format = $options['format'];
|
|
|
- $data = (array)$data;
|
|
|
+ $data = $data;
|
|
|
if (empty($data)) {
|
|
|
return $options['clean'] ? static::cleanInsert($str, $options) : $str;
|
|
|
}
|
|
|
@@ -253,7 +251,7 @@ class Text
|
|
|
* @return string
|
|
|
* @see \Cake\Utility\Text::insert()
|
|
|
*/
|
|
|
- public static function cleanInsert($str, array $options)
|
|
|
+ public static function cleanInsert(string $str, array $options): string
|
|
|
{
|
|
|
$clean = $options['clean'];
|
|
|
if (!$clean) {
|
|
|
@@ -323,7 +321,7 @@ class Text
|
|
|
* @param array|int $options Array of options to use, or an integer to wrap the text to.
|
|
|
* @return string Formatted text.
|
|
|
*/
|
|
|
- public static function wrap($text, $options = [])
|
|
|
+ public static function wrap(string $text, $options = []): string
|
|
|
{
|
|
|
if (is_numeric($options)) {
|
|
|
$options = ['width' => $options];
|
|
|
@@ -360,7 +358,7 @@ class Text
|
|
|
* @param array|int $options Array of options to use, or an integer to wrap the text to.
|
|
|
* @return string Formatted text.
|
|
|
*/
|
|
|
- public static function wrapBlock($text, $options = [])
|
|
|
+ public static function wrapBlock(string $text, $options = []): string
|
|
|
{
|
|
|
if (is_numeric($options)) {
|
|
|
$options = ['width' => $options];
|
|
|
@@ -409,7 +407,7 @@ class Text
|
|
|
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
|
|
|
* @return string Formatted text.
|
|
|
*/
|
|
|
- public static function wordWrap($text, $width = 72, $break = "\n", $cut = false)
|
|
|
+ public static function wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string
|
|
|
{
|
|
|
$paragraphs = explode($break, $text);
|
|
|
foreach ($paragraphs as &$paragraph) {
|
|
|
@@ -428,7 +426,7 @@ class Text
|
|
|
* @param bool $cut If the cut is set to true, the string is always wrapped at the specified width.
|
|
|
* @return string Formatted text.
|
|
|
*/
|
|
|
- protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = false)
|
|
|
+ protected static function _wordWrap(string $text, int $width = 72, string $break = "\n", bool $cut = false): string
|
|
|
{
|
|
|
if ($cut) {
|
|
|
$parts = [];
|
|
|
@@ -487,7 +485,7 @@ class Text
|
|
|
* @return string The highlighted text
|
|
|
* @link https://book.cakephp.org/3.0/en/core-libraries/text.html#highlighting-substrings
|
|
|
*/
|
|
|
- public static function highlight($text, $phrase, array $options = [])
|
|
|
+ public static function highlight(string $text, $phrase, array $options = []): string
|
|
|
{
|
|
|
if (empty($phrase)) {
|
|
|
return $text;
|
|
|
@@ -544,7 +542,7 @@ class Text
|
|
|
* @param array $options An array of options.
|
|
|
* @return string Trimmed string.
|
|
|
*/
|
|
|
- public static function tail($text, $length = 100, array $options = [])
|
|
|
+ public static function tail(string $text, int $length = 100, array $options = []): string
|
|
|
{
|
|
|
$default = [
|
|
|
'ellipsis' => '...', 'exact' => true
|
|
|
@@ -585,7 +583,7 @@ class Text
|
|
|
* @return string Trimmed string.
|
|
|
* @link https://book.cakephp.org/3.0/en/core-libraries/text.html#truncating-text
|
|
|
*/
|
|
|
- public static function truncate($text, $length = 100, array $options = [])
|
|
|
+ public static function truncate(string $text, int $length = 100, array $options = []): string
|
|
|
{
|
|
|
$default = [
|
|
|
'ellipsis' => '...', 'exact' => true, 'html' => false, 'trimWidth' => false,
|
|
|
@@ -683,7 +681,7 @@ class Text
|
|
|
* @return string Trimmed string.
|
|
|
* @see \Cake\Utility\Text::truncate()
|
|
|
*/
|
|
|
- public static function truncateByWidth($text, $length = 100, array $options = [])
|
|
|
+ public static function truncateByWidth(string $text, int $length = 100, array $options = []): string
|
|
|
{
|
|
|
return static::truncate($text, $length, ['trimWidth' => true] + $options);
|
|
|
}
|
|
|
@@ -700,7 +698,7 @@ class Text
|
|
|
* @param array $options An array of options.
|
|
|
* @return int
|
|
|
*/
|
|
|
- protected static function _strlen($text, array $options)
|
|
|
+ protected static function _strlen(string $text, array $options): int
|
|
|
{
|
|
|
if (empty($options['trimWidth'])) {
|
|
|
$strlen = 'mb_strlen';
|
|
|
@@ -736,11 +734,11 @@ class Text
|
|
|
*
|
|
|
* @param string $text The input string.
|
|
|
* @param int $start The position to begin extracting.
|
|
|
- * @param int $length The desired length.
|
|
|
+ * @param int|null $length The desired length.
|
|
|
* @param array $options An array of options.
|
|
|
* @return string
|
|
|
*/
|
|
|
- protected static function _substr($text, $start, $length, array $options)
|
|
|
+ protected static function _substr(string $text, int $start, ?int $length, array $options): string
|
|
|
{
|
|
|
if (empty($options['trimWidth'])) {
|
|
|
$substr = 'mb_substr';
|
|
|
@@ -826,12 +824,12 @@ class Text
|
|
|
* @param string $text The input text
|
|
|
* @return string
|
|
|
*/
|
|
|
- protected static function _removeLastWord($text)
|
|
|
+ protected static function _removeLastWord(string $text): string
|
|
|
{
|
|
|
$spacepos = mb_strrpos($text, ' ');
|
|
|
|
|
|
if ($spacepos !== false) {
|
|
|
- $lastWord = mb_strrpos($text, $spacepos);
|
|
|
+ $lastWord = mb_substr($text, $spacepos);
|
|
|
|
|
|
// Some languages are written without word separation.
|
|
|
// We recognize a string as a word if it doesn't contain any full-width characters.
|
|
|
@@ -856,7 +854,7 @@ class Text
|
|
|
* @return string Modified string
|
|
|
* @link https://book.cakephp.org/3.0/en/core-libraries/text.html#extracting-an-excerpt
|
|
|
*/
|
|
|
- public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...')
|
|
|
+ public static function excerpt(string $text, string $phrase, int $radius = 100, string $ellipsis = '...'): string
|
|
|
{
|
|
|
if (empty($text) || empty($phrase)) {
|
|
|
return static::truncate($text, $radius * 2, ['ellipsis' => $ellipsis]);
|
|
|
@@ -899,16 +897,16 @@ class Text
|
|
|
* @return string The glued together string.
|
|
|
* @link https://book.cakephp.org/3.0/en/core-libraries/text.html#converting-an-array-to-sentence-form
|
|
|
*/
|
|
|
- public static function toList(array $list, $and = null, $separator = ', ')
|
|
|
+ public static function toList(array $list, ?string $and = null, string $separator = ', '): string
|
|
|
{
|
|
|
if ($and === null) {
|
|
|
$and = __d('cake', 'and');
|
|
|
}
|
|
|
if (count($list) > 1) {
|
|
|
- return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
|
|
|
+ return implode($separator, array_slice($list, 0, -1)) . ' ' . $and . ' ' . array_pop($list);
|
|
|
}
|
|
|
|
|
|
- return array_pop($list);
|
|
|
+ return (string)array_pop($list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -917,7 +915,7 @@ class Text
|
|
|
* @param string $string value to test
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public static function isMultibyte($string)
|
|
|
+ public static function isMultibyte(string $string): bool
|
|
|
{
|
|
|
$length = strlen($string);
|
|
|
|
|
|
@@ -938,7 +936,7 @@ class Text
|
|
|
* @param string $string String to convert.
|
|
|
* @return array
|
|
|
*/
|
|
|
- public static function utf8($string)
|
|
|
+ public static function utf8(string $string): array
|
|
|
{
|
|
|
$map = [];
|
|
|
|
|
|
@@ -979,7 +977,7 @@ class Text
|
|
|
* @param array $array Array
|
|
|
* @return string
|
|
|
*/
|
|
|
- public static function ascii(array $array)
|
|
|
+ public static function ascii(array $array): string
|
|
|
{
|
|
|
$ascii = '';
|
|
|
|
|
|
@@ -1008,7 +1006,7 @@ class Text
|
|
|
* @throws \InvalidArgumentException On invalid Unit type.
|
|
|
* @link https://book.cakephp.org/3.0/en/core-libraries/text.html#Cake\Utility\Text::parseFileSize
|
|
|
*/
|
|
|
- public static function parseFileSize($size, $default = false)
|
|
|
+ public static function parseFileSize(string $size, $default = false)
|
|
|
{
|
|
|
if (ctype_digit($size)) {
|
|
|
return (int)$size;
|
|
|
@@ -1044,7 +1042,6 @@ class Text
|
|
|
*
|
|
|
* @return \Transliterator|null Either a Transliterator instance, or `null`
|
|
|
* in case no transliterator has been set yet.
|
|
|
- * @since 3.7.0
|
|
|
*/
|
|
|
public static function getTransliterator()
|
|
|
{
|
|
|
@@ -1056,7 +1053,6 @@ class Text
|
|
|
*
|
|
|
* @param \Transliterator $transliterator A `Transliterator` instance.
|
|
|
* @return void
|
|
|
- * @since 3.7.0
|
|
|
*/
|
|
|
public static function setTransliterator(\Transliterator $transliterator)
|
|
|
{
|
|
|
@@ -1096,7 +1092,7 @@ class Text
|
|
|
* @return string
|
|
|
* @see https://secure.php.net/manual/en/transliterator.transliterate.php
|
|
|
*/
|
|
|
- public static function transliterate($string, $transliterator = null)
|
|
|
+ public static function transliterate(string $string, $transliterator = null): string
|
|
|
{
|
|
|
if (!$transliterator) {
|
|
|
$transliterator = static::$_defaultTransliterator ?: static::$_defaultTransliteratorId;
|
|
|
@@ -1126,7 +1122,7 @@ class Text
|
|
|
* @see setTransliterator()
|
|
|
* @see setTransliteratorId()
|
|
|
*/
|
|
|
- public static function slug($string, $options = [])
|
|
|
+ public static function slug(string $string, $options = []): string
|
|
|
{
|
|
|
if (is_string($options)) {
|
|
|
$options = ['replacement' => $options];
|