mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX: returns duplicated number of the same sentences (from r78773, r78778)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@81472 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
f515ed7898
commit
a738dc2027
@ -86,30 +86,25 @@ class Text extends DBField {
|
||||
* @param int $sentCount The amount of sentences you want.
|
||||
*/
|
||||
function LimitSentences($sentCount = 2) {
|
||||
$output = '';
|
||||
$data = Convert::xml2raw($this->value);
|
||||
if(!is_numeric($sentCount)) user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
|
||||
|
||||
$output = array();
|
||||
$data = trim(Convert::xml2raw($this->value));
|
||||
$sentences = explode('.', $data);
|
||||
if(count($sentences) == 1) {
|
||||
return $sentences[0] . '.';
|
||||
} elseif(count($sentences) > 1) {
|
||||
if(is_numeric($sentCount) && $sentCount != 0) {
|
||||
if($sentCount == 1) {
|
||||
$output = $sentences[0] . '. ';
|
||||
} else {
|
||||
for($i = 1; $i <= $sentCount-1; $i++) {
|
||||
if($sentences[0]) {
|
||||
$output .= $sentences[0] . '. ';
|
||||
}
|
||||
if($sentences[$i]) {
|
||||
$output .= $sentences[$i] . '. ';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
|
||||
if ($sentCount == 0) return '';
|
||||
|
||||
for($i = 0; $i < $sentCount; $i++) {
|
||||
if(isset($sentences[$i])) {
|
||||
$sentence = trim($sentences[$i]);
|
||||
if(!empty($sentence)) $output[] .= $sentence;
|
||||
}
|
||||
}
|
||||
|
||||
return count($output)==0 ? '' : implode($output, '. ') . '.';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
||||
*/
|
||||
|
@ -66,5 +66,26 @@ class TextTest extends SapphireTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link Text->LimitSentences()}
|
||||
*/
|
||||
function testLimitSentences() {
|
||||
$cases = array(
|
||||
'' => '',
|
||||
'First sentence.' => 'First sentence.',
|
||||
'First sentence. Second sentence' => 'First sentence. Second sentence.',
|
||||
'<p>First sentence.</p>' => 'First sentence.',
|
||||
'<p>First sentence. Second sentence. Third sentence</p>' => 'First sentence. Second sentence.',
|
||||
'<p>First sentence. <em>Second sentence</em>. Third sentence</p>' => 'First sentence. Second sentence.',
|
||||
'<p>First sentence. <em class="dummyClass">Second sentence</em>. Third sentence</p>' => 'First sentence. Second sentence.'
|
||||
);
|
||||
|
||||
foreach($cases as $originalValue => $expectedValue) {
|
||||
$textObj = new Text('Test');
|
||||
$textObj->setValue($originalValue);
|
||||
$this->assertEquals($expectedValue, $textObj->LimitSentences(2));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user