mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
@ -342,7 +342,7 @@ class BaseInflector
|
|||||||
{
|
{
|
||||||
$words = static::humanize(static::underscore($words), $ucAll);
|
$words = static::humanize(static::underscore($words), $ucAll);
|
||||||
|
|
||||||
return $ucAll ? mb_convert_case($words, MB_CASE_TITLE, self::encoding()) : self::mb_ucfirst($words, self::encoding());
|
return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -357,7 +357,7 @@ class BaseInflector
|
|||||||
*/
|
*/
|
||||||
public static function camelize($word)
|
public static function camelize($word)
|
||||||
{
|
{
|
||||||
return str_replace(' ', '', mb_convert_case(preg_replace('/[^\pL\pN]+/u', ' ', $word), MB_CASE_TITLE, self::encoding()));
|
return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -375,7 +375,7 @@ class BaseInflector
|
|||||||
'.',
|
'.',
|
||||||
], ' ', preg_replace('/(\p{Lu})/u', ' \0', $name))), self::encoding());
|
], ' ', preg_replace('/(\p{Lu})/u', ' \0', $name))), self::encoding());
|
||||||
|
|
||||||
return $ucwords ? mb_convert_case($label, MB_CASE_TITLE, self::encoding()) : $label;
|
return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -407,7 +407,7 @@ class BaseInflector
|
|||||||
*/
|
*/
|
||||||
public static function id2camel($id, $separator = '-')
|
public static function id2camel($id, $separator = '-')
|
||||||
{
|
{
|
||||||
return str_replace(' ', '', mb_convert_case(str_replace($separator, ' ', $id), MB_CASE_TITLE, self::encoding()));
|
return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -431,7 +431,7 @@ class BaseInflector
|
|||||||
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
|
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
|
||||||
$encoding = self::encoding();
|
$encoding = self::encoding();
|
||||||
|
|
||||||
return $ucAll ? mb_convert_case($word, MB_CASE_TITLE, $encoding) : self::mb_ucfirst($word, $encoding);
|
return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -609,18 +609,4 @@ class BaseInflector
|
|||||||
return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
|
return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The same as built-in `ucfirst`, but unicode-safe
|
|
||||||
*
|
|
||||||
* @param string $string
|
|
||||||
* @param string $encoding
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private static function mb_ucfirst($string, $encoding)
|
|
||||||
{
|
|
||||||
$firstChar = mb_substr($string, 0, 1, $encoding);
|
|
||||||
$rest = mb_substr($string, 1, null, $encoding);
|
|
||||||
|
|
||||||
return mb_strtoupper($firstChar, $encoding) . $rest;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -418,4 +418,40 @@ class BaseStringHelper
|
|||||||
|
|
||||||
return preg_match($pattern, $string) === 1;
|
return preg_match($pattern, $string) === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
|
||||||
|
*
|
||||||
|
* @param string $string the string to be proceeded
|
||||||
|
* @param string $encoding Optional, defaults to "UTF-8"
|
||||||
|
* @return string
|
||||||
|
* @see http://php.net/manual/en/function.ucfirst.php
|
||||||
|
* @since 2.0.16
|
||||||
|
*/
|
||||||
|
public static function mb_ucfirst($string, $encoding = 'UTF-8')
|
||||||
|
{
|
||||||
|
$firstChar = mb_substr($string, 0, 1, $encoding);
|
||||||
|
$rest = mb_substr($string, 1, null, $encoding);
|
||||||
|
|
||||||
|
return mb_strtoupper($firstChar, $encoding) . $rest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
|
||||||
|
*
|
||||||
|
* @param string $string the string to be proceeded
|
||||||
|
* @param string $encoding Optional, defaults to "UTF-8"
|
||||||
|
* @see http://php.net/manual/en/function.ucwords.php
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function mb_ucwords($string, $encoding = 'UTF-8')
|
||||||
|
{
|
||||||
|
$words = preg_split("/\s/u", $string, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
|
||||||
|
$titelized = array_map(function ($word) use ($encoding) {
|
||||||
|
return static::mb_ucfirst($word, $encoding);
|
||||||
|
}, $words);
|
||||||
|
|
||||||
|
return implode(' ', $titelized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -89,9 +89,9 @@ class InflectorTest extends TestCase
|
|||||||
|
|
||||||
public function testCamelize()
|
public function testCamelize()
|
||||||
{
|
{
|
||||||
$this->assertEquals('MeMySelfAndi', Inflector::camelize('me my_self-andI'));
|
$this->assertEquals('MeMySelfAndI', Inflector::camelize('me my_self-andI'));
|
||||||
$this->assertEquals('QweQweEwq', Inflector::camelize('qwe qwe^ewq'));
|
$this->assertEquals('QweQweEwq', Inflector::camelize('qwe qwe^ewq'));
|
||||||
$this->assertEquals('ВідомоЩоТестиЗберігатьНашіНерви', Inflector::camelize('Відомо, що тести зберігать наші НЕРВИ! 🙃'));
|
$this->assertEquals('ВідомоЩоТестиЗберігатьНашіНЕРВИ', Inflector::camelize('Відомо, що тести зберігать наші НЕРВИ! 🙃'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnderscore()
|
public function testUnderscore()
|
||||||
@ -138,6 +138,8 @@ class InflectorTest extends TestCase
|
|||||||
$this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_'));
|
$this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_'));
|
||||||
$this->assertEquals('НевжеІЦеПрацює', Inflector::id2camel('невже_і_це_працює', '_'));
|
$this->assertEquals('НевжеІЦеПрацює', Inflector::id2camel('невже_і_це_працює', '_'));
|
||||||
|
|
||||||
|
$this->assertEquals('ShouldNotBecomeLowercased', Inflector::id2camel('ShouldNotBecomeLowercased', '_'));
|
||||||
|
|
||||||
$this->assertEquals('FooYBar', Inflector::id2camel('foo-y-bar'));
|
$this->assertEquals('FooYBar', Inflector::id2camel('foo-y-bar'));
|
||||||
$this->assertEquals('FooYBar', Inflector::id2camel('foo_y_bar', '_'));
|
$this->assertEquals('FooYBar', Inflector::id2camel('foo_y_bar', '_'));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ use yiiunit\TestCase;
|
|||||||
*/
|
*/
|
||||||
class StringHelperTest extends TestCase
|
class StringHelperTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
@ -399,4 +400,48 @@ class StringHelperTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertSame($expectedResult, StringHelper::matchWildcard($pattern, $string, $options));
|
$this->assertSame($expectedResult, StringHelper::matchWildcard($pattern, $string, $options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dataProviderMb_ucfirst()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['foo', 'Foo'],
|
||||||
|
['foo bar', 'Foo bar'],
|
||||||
|
['👍🏻 foo bar', '👍🏻 foo bar'],
|
||||||
|
['', ''],
|
||||||
|
[null, ''],
|
||||||
|
['здесь我 multibyte我 строка', 'Здесь我 multibyte我 строка'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $string
|
||||||
|
* @param string $expectedResult
|
||||||
|
* @dataProvider dataProviderMb_ucfirst
|
||||||
|
*/
|
||||||
|
public function testMb_ucfirst($string, $expectedResult)
|
||||||
|
{
|
||||||
|
$this->assertSame($expectedResult, StringHelper::mb_ucfirst($string));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataProviderMb_ucwords()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['foo', 'Foo'],
|
||||||
|
['foo bar', 'Foo Bar'],
|
||||||
|
['👍🏻 foo bar', '👍🏻 Foo Bar'],
|
||||||
|
['', ''],
|
||||||
|
[null, ''],
|
||||||
|
['здесь我 multibyte我 строка', 'Здесь我 Multibyte我 Строка'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $string
|
||||||
|
* @param string $expectedResult
|
||||||
|
* @dataProvider dataProviderMb_ucwords
|
||||||
|
*/
|
||||||
|
public function testMb_ucwords($string, $expectedResult)
|
||||||
|
{
|
||||||
|
$this->assertSame($expectedResult, StringHelper::mb_ucwords($string));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user