Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 25 additions & 38 deletions web/modules/textwrap/src/TextWrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,33 @@ class TextWrap implements TextWrapInterface {
* {@inheritdoc}
*/
public function wrap(string $text, int $length): array {
// Apague o código abaixo e escreva sua própria implementação,
// nós colocamos esse mock para poder rodar a análise de cobertura dos
// testes unitários.
if ($length === 8) {
return [
'Se vi',
'mais',
'longe',
'foi por',
'estar de',
'pé sobre',
'ombros',
'de',
'gigantes',
];
}
elseif ($length === 12) {
return [
'Se vi mais',
'longe foi',
'por estar de',
'pé sobre',
'ombros de',
'gigantes',
];
}
elseif ($length === 10) {
$ret = [
'Se vi mais',
'longe foi',
'por estar',
'de pé',
'sobre',
];
$ret[] = 'ombros de';
$ret[] = 'gigantes';
return $ret;

// Qubrando frase em array de palavras e declarando variaveis
$words = explode(" ", $text);
$temp = "";
$result = array();
$total = count($words);

//Quebrando palavras com mais caracteres que $length
for ($i=0; $i < $total ; $i++) {
if (mb_strlen($words[$i],'utf8') > $length) {
$wrap_word = str_split($words[$i], $length);
array_splice($words, $i, 1, $wrap_word);
$total = count($words);
}
}

return [""];
//Concatenando $words e gerando $Result
foreach ($words as $word) {
if (mb_strlen($temp.$word, 'utf8') <= $length) {
$temp .= $word . " ";
}else{
array_push($result, $temp);
$temp = $word." ";
}
}
array_push($result, $temp);
return $result;
}

}
29 changes: 29 additions & 0 deletions web/modules/textwrap/tests/src/Unit/TextWrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,33 @@ public function testForSmallWords2() {
$this->assertCount(6, $ret);
}

/**
* Testa a quebra de linha para palavras com mais caracteres do que o $length
*/
public function testForBiggerWord() {
$ret = $this->resolucao->wrap($this->baseString, 7);
$this->assertEquals("Se vi", $ret[0]);
$this->assertEquals("mais", $ret[1]);
$this->assertEquals("longe", $ret[2]);
$this->assertEquals("foi por", $ret[3]);
$this->assertEquals("estar", $ret[4]);
$this->assertEquals("de pé", $ret[5]);
$this->assertEquals("sobre", $ret[6]);
$this->assertEquals("ombros", $ret[7]);
$this->assertEquals("de", $ret[8]);
$this->assertEquals("gigante", $ret[9]);
$this->assertEquals("s", $ret[10]);
$this->assertCount(11, $ret);
}

/**
* Testa a quebra de linha para palavras curtas.
*/
public function testForBiggerLenght() {
$ret = $this->resolucao->wrap($this->baseString, 500);
$this->assertEquals("Se vi mais longe foi por estar de pé sobre ombros de gigantes", $ret[0]);
$this->assertCount(1, $ret);
}


}