diff --git a/web/modules/textwrap/src/TextWrap.php b/web/modules/textwrap/src/TextWrap.php index f4e9131..f1f66ed 100644 --- a/web/modules/textwrap/src/TextWrap.php +++ b/web/modules/textwrap/src/TextWrap.php @@ -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; } } diff --git a/web/modules/textwrap/tests/src/Unit/TextWrapTest.php b/web/modules/textwrap/tests/src/Unit/TextWrapTest.php index aedc1a0..e3bf67e 100644 --- a/web/modules/textwrap/tests/src/Unit/TextWrapTest.php +++ b/web/modules/textwrap/tests/src/Unit/TextWrapTest.php @@ -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); + } + + }