From 4828811a48354b2bc913a57537ae34b972800b7a Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Sat, 27 Jul 2013 15:39:36 -0400 Subject: [PATCH] Added truncate() --- README.md | 18 ++++++++++++++-- src/Stringy/StaticStringy.php | 18 ++++++++++++++++ src/Stringy/Stringy.php | 24 +++++++++++++++++++++ tests/Stringy/CommonTest.php | 33 ++++++++++++++++++++++++++++- tests/Stringy/StaticStringyTest.php | 10 +++++++++ tests/Stringy/StringyTest.php | 8 +++---- 6 files changed, 104 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 224d60c..649d035 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele * [contains](#contains) * [surround](#surround) * [insert](#insert) + * [truncate](#truncate) * [safeTruncate](#safetruncate) * [reverse](#reverse) * [shuffle](#shuffle) @@ -467,6 +468,21 @@ S::create('fòô bà', 'UTF-8')->insert('ř', 6); S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř' ``` +##### truncate + +$stringy->truncate(int $length, [, string $substring = '' ]) + +S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]]) + +Truncates $str to a given length. If $substring is provided, and +truncating occurs, the string is further truncated so that the substring +may be appended without exceeding the desired length. + +```php +S::create('What are your plans today?')->safeTruncate(19, '...'); +S::safeTruncate('What are your plans today?', 19, '...'); // 'What are your pl...' +``` + ##### safeTruncate $stringy->safeTruncate(int $length, [, string $substring = '' ]) @@ -650,8 +666,6 @@ S::last('fòô bàř', 3, 'UTF-8'); // 'bàř' **excerpt** ($str, $substring, $radius) -**truncate** - **pluralize** ($count, $singular, $plural = null) **toBoolean** diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 7a8cc56..2d231ba 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -346,6 +346,24 @@ class StaticStringy return Stringy::create($str, $encoding)->insert($substring, $index)->str; } + /** + * Truncates $str to a given length. If $substring is provided, and + * truncating occurs, the string is further truncated so that the substring + * may be appended without exceeding the desired length. + * + * @param string $str String to truncate + * @param int $length Desired length of the truncated string + * @param string $substring The substring to append if it can fit + * @param string $encoding The character encoding + * @return string The resulting string after truncating + */ + public static function truncate($str, $length, $substring = '', + $encoding = null) + { + return Stringy::create($str, $encoding) + ->truncate($length, $substring)->str; + } + /** * Truncates the string to a given length, while ensuring that it does not * chop words. If $substring is provided, and truncating occurs, the string diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 2f61466..efc34f4 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -539,6 +539,30 @@ class Stringy return $this; } + /** + * Truncates $str to a given length. If $substring is provided, and + * truncating occurs, the string is further truncated so that the substring + * may be appended without exceeding the desired length. + * + * @param int $length Desired length of the truncated string + * @param string $substring The substring to append if it can fit + * @return Stringy Object with the resulting $str after truncating + */ + public function truncate($length, $substring = '') + { + if ($length >= $this->length()) + return $this; + + // Need to further trim the string so we can append the substring + $substringLength = mb_strlen($substring, $this->encoding); + $length = $length - $substringLength; + + $truncated = mb_substr($this->str, 0, $length, $this->encoding); + $this->str = $truncated . $substring; + + return $this; + } + /** * Truncates $str to a given length, while ensuring that it does not chop * words. If $substring is provided, and truncating occurs, the string diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index f088da7..d573e03 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -394,6 +394,36 @@ class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForTruncate() + { + $testData = array( + array('Test foo bar', 'Test foo bar', 12), + array('Test foo ba', 'Test foo bar', 11), + array('Test foo', 'Test foo bar', 8), + array('Test fo', 'Test foo bar', 7), + array('Test', 'Test foo bar', 4), + array('Test foo bar', 'Test foo bar', 12, '...'), + array('Test foo...', 'Test foo bar', 11, '...'), + array('Test ...', 'Test foo bar', 8, '...'), + array('Test...', 'Test foo bar', 7, '...'), + array('T...', 'Test foo bar', 4, '...'), + array('Test fo....', 'Test foo bar', 11, '....'), + array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'), + array('Test fòô bà', 'Test fòô bàř', 11, '', 'UTF-8'), + array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'), + array('Test fò', 'Test fòô bàř', 7, '', 'UTF-8'), + array('Test', 'Test fòô bàř', 4, '', 'UTF-8'), + array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'), + array('Test fòô ϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'), + array('Test fϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'), + array('Test ϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'), + array('Teϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'), + array('What are your pl...', 'What are your plans today?', 19, '...') + ); + + return $testData; + } + public function stringsForSafeTruncate() { $testData = array( @@ -417,7 +447,8 @@ class CommonTest extends PHPUnit_Framework_TestCase array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'), array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'), array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'), - array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8') + array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'), + array('What are your plans...', 'What are your plans today?', 22, '...') ); return $testData; diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 8e0d473..89e5277 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -231,6 +231,16 @@ class StaticStringyTestCase extends CommonTest $this->assertEquals($expected, $result); } + /** + * @dataProvider stringsForTruncate + */ + public function testTruncate($expected, $str, $length, $substring = '', + $encoding = null) + { + $result = S::truncate($str, $length, $substring, $encoding); + $this->assertEquals($expected, $result); + } + /** * @dataProvider stringsForSafeTruncate */ diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 28743bf..a233a56 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -232,12 +232,12 @@ class StringyTestCase extends CommonTest } /** - * @dataProvider stringsForSafeTruncate + * @dataProvider stringsForTruncate */ - public function testSafeTruncate($expected, $str, $length, $substring = '', - $encoding = null) + public function testTruncate($expected, $str, $length, $substring = '', + $encoding = null) { - $result = S::create($str, $encoding)->safeTruncate($length, $substring); + $result = S::create($str, $encoding)->truncate($length, $substring); $this->assertEquals($expected, $result); }