mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #13254: Made yii\helpers\StringHelper
and yii\validators\StringValidator
independent of Yii::$app
instance
This commit is contained in:

committed by
Alexander Makarov

parent
61996979db
commit
36b535c26b
@ -4,6 +4,7 @@ Yii Framework 2 Change Log
|
||||
2.0.13 under development
|
||||
------------------------
|
||||
|
||||
- Enh #13254: Made `yii\helpers\StringHelper` and `yii\validators\StringValidator` independent of `Yii::$app` instance (cebe)
|
||||
- Enh #14273: `yii\log\Target::$enabled` now supports callable value (dmirogin)
|
||||
- Bug #14773: Fixed `yii\widgets\ActiveField::$options` does not support 'class' option in array format (klimov-paul)
|
||||
- Bug #14723: Fixed serialization of `yii\db\Connection` instance closes database connection (klimov-paul)
|
||||
|
@ -104,12 +104,15 @@ class BaseStringHelper
|
||||
*/
|
||||
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
|
||||
{
|
||||
if ($encoding === null) {
|
||||
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
|
||||
}
|
||||
if ($asHtml) {
|
||||
return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
|
||||
return static::truncateHtml($string, $length, $suffix, $encoding);
|
||||
}
|
||||
|
||||
if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
|
||||
return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix;
|
||||
if (mb_strlen($string, $encoding) > $length) {
|
||||
return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix;
|
||||
}
|
||||
|
||||
return $string;
|
||||
@ -152,7 +155,9 @@ class BaseStringHelper
|
||||
protected static function truncateHtml($string, $count, $suffix, $encoding = false)
|
||||
{
|
||||
$config = \HTMLPurifier_Config::create(null);
|
||||
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
|
||||
if (Yii::$app !== null) {
|
||||
$config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
|
||||
}
|
||||
$lexer = \HTMLPurifier_Lexer::create($config);
|
||||
$tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
|
||||
$openTokens = [];
|
||||
@ -215,9 +220,10 @@ class BaseStringHelper
|
||||
}
|
||||
if ($caseSensitive) {
|
||||
return strncmp($string, $with, $bytes) === 0;
|
||||
}
|
||||
|
||||
return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
|
||||
}
|
||||
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
|
||||
return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), $encoding) === mb_strtolower($with, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -243,7 +249,8 @@ class BaseStringHelper
|
||||
return substr_compare($string, $with, -$bytes, $bytes) === 0;
|
||||
}
|
||||
|
||||
return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
|
||||
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
|
||||
return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), $encoding) === mb_strtolower($with, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +82,7 @@ class StringValidator extends Validator
|
||||
$this->length = null;
|
||||
}
|
||||
if ($this->encoding === null) {
|
||||
$this->encoding = Yii::$app->charset;
|
||||
$this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
|
||||
}
|
||||
if ($this->message === null) {
|
||||
$this->message = Yii::t('yii', '{attribute} must be a string.');
|
||||
|
@ -19,7 +19,9 @@ class StringHelperTest extends TestCase
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->mockApplication();
|
||||
|
||||
// destroy application, Helper must work without Yii::$app
|
||||
$this->destroyApplication();
|
||||
}
|
||||
|
||||
public function testStrlen()
|
||||
|
@ -19,7 +19,9 @@ class StringValidatorTest extends TestCase
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->mockApplication();
|
||||
|
||||
// destroy application, Validator must work without Yii::$app
|
||||
$this->destroyApplication();
|
||||
}
|
||||
|
||||
public function testValidateValue()
|
||||
|
Reference in New Issue
Block a user