Deprecating TextHelper::trim() as it is an alias for truncate().

Expanding use of mb_ methods.
Tests updated.
This commit is contained in:
mark_story 2009-09-28 23:12:44 -04:00
parent 190a8b8307
commit 06162d5022
2 changed files with 11 additions and 17 deletions

View file

@ -138,7 +138,8 @@ class TextHelper extends AppHelper {
$options = 'array(';
foreach ($htmlOptions as $option => $value) {
$options .= "'$option' => '$value', ";
$value = var_export($value, true);
$options .= "'$option' => $value, ";
}
$options .= ')';
@ -226,7 +227,7 @@ class TextHelper extends AppHelper {
if (mb_strlen($text) <= $length) {
return $text;
} else {
$truncate = mb_substr($text, 0, $length - strlen($ending));
$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
}
}
if (!$exact) {
@ -265,6 +266,7 @@ class TextHelper extends AppHelper {
* @access public
*/
function trim() {
trigger_error('TextHelper::trim() is deprecated. Use TextHelper::truncate() instead', E_USER_WARNING);
$args = func_get_args();
return call_user_func_array(array(&$this, 'truncate'), $args);
}
@ -284,26 +286,26 @@ class TextHelper extends AppHelper {
return $this->truncate($text, $radius * 2, $ending);
}
$phraseLen = strlen($phrase);
$phraseLen = mb_strlen($phrase);
if ($radius < $phraseLen) {
$radius = $phraseLen;
}
$pos = strpos(strtolower($text), strtolower($phrase));
$pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
$startPos = 0;
if ($pos > $radius) {
$startPos = $pos - $radius;
}
$textLen = strlen($text);
$textLen = mb_strlen($text);
$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$endPos = $textLen;
}
$excerpt = substr($text, $startPos, $endPos - $startPos);
$excerpt = mb_substr($text, $startPos, $endPos - $startPos);
if ($startPos != 0) {
$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
}

View file

@ -70,10 +70,7 @@ class TextHelperTest extends CakeTestCase {
* @return void
*/
function testTruncate() {
if (!isset($this->method)) {
$this->method = 'truncate';
}
$m = $this->method;
$m = 'truncate';
$text1 = 'The quick brown fox jumps over the lazy dog';
$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung';
$text3 = '<b>&copy; 2005-2007, Cake Software Foundation, Inc.</b><br />written by Alexander Wegener';
@ -106,11 +103,6 @@ class TextHelperTest extends CakeTestCase {
$this->assertIdentical($this->Text->{$m}($text7, 255), $text7);
$this->assertIdentical($this->Text->{$m}($text7, 15), 'El moño está...');
$this->assertIdentical($this->Text->{$m}($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
if ($this->method == 'truncate') {
$this->method = 'trim';
$this->testTruncate();
}
}
/**