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

View file

@ -70,10 +70,7 @@ class TextHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function testTruncate() { function testTruncate() {
if (!isset($this->method)) { $m = 'truncate';
$this->method = 'truncate';
}
$m = $this->method;
$text1 = 'The quick brown fox jumps over the lazy dog'; $text1 = 'The quick brown fox jumps over the lazy dog';
$text2 = 'Heiz&ouml;lr&uuml;cksto&szlig;abd&auml;mpfung'; $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'; $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, 255), $text7);
$this->assertIdentical($this->Text->{$m}($text7, 15), 'El moño está...'); $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...'); $this->assertIdentical($this->Text->{$m}($text8, 15), 'Vive la R'.chr(195).chr(169).'pu...');
if ($this->method == 'truncate') {
$this->method = 'trim';
$this->testTruncate();
}
} }
/** /**