Commit a6203423 by Alexander Makarov

Adjusted phpdoc, tests and changelog for StringHelper::truncate and StringHelper::truncateWords

parent 8d18d722
......@@ -25,7 +25,7 @@ Yii Framework 2 Change Log
- Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue)
- Enh #2837: Error page now shows arguments in stack trace method calls (samdark)
- Enh #2906: Added support for using conditional comments for js and css files registered through asset bundles and Html helper (exromany, qiangxue)
- Enh #2942: Added truncate and truncateWord methods (Alex-Code)
- Enh #2942: Added truncate and truncateWord methods (Alex-Code, samdark)
- Enh #3008: Added `Html::errorSummary()` (qiangxue)
- Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue)
- Enh #3103: debugger panel is now not displayed when printing a page (githubjeka)
......
......@@ -89,11 +89,12 @@ class BaseStringHelper
}
/**
* Truncates a string to the specified length.
* Truncates a string to the number of characters specified.
*
* @param string $string The string to truncate.
* @param integer $length The new length of the string.
* @param string $suffix A value to affix to the end.
* @param string $encoding The charset to use, defaults to application current.
* @param integer $length How many characters from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @param string $encoding The charset to use, defaults to charset currently used by application.
* @return string the truncated string.
*/
public static function truncate($string, $length, $suffix = '...', $encoding = null)
......@@ -106,10 +107,11 @@ class BaseStringHelper
}
/**
* Split a string into words preserving whitespace and return the specified amount of words.
* Truncates a string to the number of words specified.
*
* @param string $string The string to truncate.
* @param integer $count How many words to truncate to.
* @param string $suffix A value to affix to the end.
* @param integer $count How many words from original string to include into truncated string.
* @param string $suffix String to append to the end of truncated string.
* @return string the truncated string.
*/
public static function truncateWords($string, $count, $suffix = '...')
......
......@@ -10,6 +10,12 @@ use yiiunit\TestCase;
*/
class StringHelperTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testStrlen()
{
$this->assertEquals(4, StringHelper::byteLength('this'));
......@@ -68,16 +74,16 @@ class StringHelperTest extends TestCase
public function testTruncate()
{
$this->assertEquals('string test', StringHelper::truncate('string test', 20));
$this->assertEquals('string...', StringHelper::truncate('string test', 6));
$this->assertEquals('string!!!', StringHelper::truncate('string test', 6, '!!!'));
$this->assertEquals('привет, я multibyte...', StringHelper::truncate('привет, я multibyte строка!', 20));
$this->assertEquals('Не трогаем строку', StringHelper::truncate('Не трогаем строку', 20));
$this->assertEquals('исполь!!!', StringHelper::truncate('используем восклицательные знаки', 6, '!!!'));
}
public function testTruncateWords()
{
$this->assertEquals('this is a string test', StringHelper::truncateWords('this is a string test', 5));
$this->assertEquals('this is a string...', StringHelper::truncateWords('this is a string test', 4));
$this->assertEquals('this is a string!!!', StringHelper::truncateWords('this is a string test', 4, '!!!'));
$this->assertEquals('this is a big space...', StringHelper::truncateWords('this is a big space string', 5));
$this->assertEquals('это тестовая multibyte строка', StringHelper::truncateWords('это тестовая multibyte строка', 5));
$this->assertEquals('это тестовая multibyte...', StringHelper::truncateWords('это тестовая multibyte строка', 3));
$this->assertEquals('это тестовая multibyte!!!', StringHelper::truncateWords('это тестовая multibyte строка', 3, '!!!'));
$this->assertEquals('это строка с неожиданными...', StringHelper::truncateWords('это строка с неожиданными пробелами', 4));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment