Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a6203423
Commit
a6203423
authored
Apr 29, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adjusted phpdoc, tests and changelog for StringHelper::truncate and StringHelper::truncateWords
parent
8d18d722
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
15 deletions
+23
-15
CHANGELOG.md
framework/CHANGELOG.md
+1
-1
BaseStringHelper.php
framework/helpers/BaseStringHelper.php
+9
-7
StringHelperTest.php
tests/unit/framework/helpers/StringHelperTest.php
+13
-7
No files found.
framework/CHANGELOG.md
View file @
a6203423
...
...
@@ -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)
...
...
framework/helpers/BaseStringHelper.php
View file @
a6203423
...
...
@@ -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
=
'...'
)
...
...
tests/unit/framework/helpers/StringHelperTest.php
View file @
a6203423
...
...
@@ -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
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment