This commit is contained in:
cuiliang
2016-06-11 19:49:57 +08:00
2065 changed files with 88639 additions and 74804 deletions

View File

@ -61,7 +61,7 @@ class BaseStringHelper
*/
public static function basename($path, $suffix = '')
{
if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
$path = mb_substr($path, 0, -$len);
}
$path = rtrim(str_replace('\\', '/', $path), '/\\');
@ -105,7 +105,7 @@ class BaseStringHelper
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
{
if ($asHtml) {
return self::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
}
if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
@ -128,7 +128,7 @@ class BaseStringHelper
public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
{
if ($asHtml) {
return self::truncateHtml($string, $count, $suffix);
return static::truncateHtml($string, $count, $suffix);
}
$words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
@ -141,7 +141,7 @@ class BaseStringHelper
/**
* Truncate a string while preserving the HTML.
*
*
* @param string $string The string to truncate
* @param integer $count
* @param string $suffix String to append to the end of the truncated string.
@ -152,6 +152,7 @@ class BaseStringHelper
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{
$config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, null);
$openTokens = 0;
@ -161,10 +162,10 @@ class BaseStringHelper
if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
$openTokens++;
$truncated[] = $token;
} else if ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
if (false === $encoding) {
$token->data = self::truncateWords($token->data, $count - $totalCount, '');
$currentCount = str_word_count($token->data);
$currentCount = self::countWords($token->data);
} else {
$token->data = self::truncate($token->data, $count - $totalCount, '', $encoding) . ' ';
$currentCount = mb_strlen($token->data, $encoding);
@ -174,10 +175,10 @@ class BaseStringHelper
$token->data = ' ' . $token->data;
}
$truncated[] = $token;
} else if ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
} elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
$openTokens--;
$truncated[] = $token;
} else if ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
} elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
$truncated[] = $token;
}
if (0 === $openTokens && $totalCount >= $count) {
@ -186,7 +187,7 @@ class BaseStringHelper
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
}
/**
@ -247,17 +248,22 @@ class BaseStringHelper
* @param boolean $skipEmpty Whether to skip empty strings between delimiters. Default is false.
* @return array
<<<<<<< HEAD
<<<<<<< HEAD
=======
* @since 2.0.4
>>>>>>> yiichina/master
=======
* @since 2.0.4
>>>>>>> master
*/
public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) {
public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
{
$result = explode($delimiter, $string);
if ($trim) {
if ($trim === true) {
$trim = 'trim';
} elseif (!is_callable($trim)) {
$trim = function($v) use ($trim) {
$trim = function ($v) use ($trim) {
return trim($v, $trim);
};
}
@ -265,8 +271,22 @@ class BaseStringHelper
}
if ($skipEmpty) {
// Wrapped with array_values to make array keys sequential after empty values removing
$result = array_values(array_filter($result));
$result = array_values(array_filter($result, function ($value) {
return $value !== '';
}));
}
return $result;
}
/**
* Counts words in a string
* @since 2.0.8
*
* @param string $string
* @return integer
*/
public static function countWords($string)
{
return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
}
}