Skip to content

Commit

Permalink
[NFE-57] Ignorar Caracteres Especiais
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoapaes committed Jan 25, 2021
1 parent fb00626 commit 3842b53
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files.encoding": "utf8"
}
16 changes: 16 additions & 0 deletions src/HtmlString.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class HtmlString {
*/
private $string;

/**
* @var bool
*/
private $ignoreSpecialCharacters = true;

/**
* @param string $string
*/
Expand All @@ -34,6 +39,11 @@ public function strlen() {
return strlen($this->clear());
}

public function notIgnoreSpecialCharacters() {
$this->ignoreSpecialCharacters = false;
return $this;
}

/**
* @param int $width
* @param string $break
Expand Down Expand Up @@ -158,6 +168,12 @@ public function substr($start, $length, $exact=true, &$diffLength=null, array &$
// the number of characters which are left
$leftStart = $total_length > $start ? 0 : $start-$total_length;
$left = ($length-$start) - $start_length;

if($this->ignoreSpecialCharacters) {
$truncate .= substr($line_matchings[2], $leftStart,$left);
break;
}

$entities_length = 0;
// search for html entities
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
Expand Down
36 changes: 33 additions & 3 deletions tests/WordWrapNoExactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @author Ricardo Paes
* @since 16/01/2019 às 15:56
* @since 16/01/2019 �s 15:56
*/

namespace Like\Util\HtmlString\Tests;
Expand Down Expand Up @@ -54,13 +54,43 @@ public function testWordwrap3() {
}

public function testWordwrap4() {
$string = " <b><d>Sistema Service</d></b> Rua Mato Grosso, <b>3037</b> Campo Mourão\PR";
$string = " <b><d>Sistema Service</d></b> Rua Mato Grosso, <b>3037</b> Campo Mourão\PR";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,false);
$this->assertEquals(3, count($wordWrapArray));
$this->assertEquals([
' <b><d>Sistema Service</d></b> ',
'Rua Mato Grosso, <b>3037</b> ',
'Campo Mourão\PR'
'Campo Mourão\PR'
], $wordWrapArray);
}

public function testWordwrapSemPontoVirgula() {
$string = "AV VINTE E NOVE DE ABRIL, 965 - QDR 0009MLOTE 00001 PLANTA 01; - CENTRO";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,false);
$this->assertEquals(2, count($wordWrapArray));
$this->assertEquals([
'AV VINTE E NOVE DE ABRIL, 965 - QDR 0009MLOTE',
'00001 PLANTA 01; - CENTRO'
], $wordWrapArray);
}

public function testWordwrapPontoVirgula() {
$string = "AV VINTE E NOVE DE ABRIL, 965 - QDR 0009;LOTE 00001 PLANTA 01; - CENTRO";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,false);
$this->assertEquals(2, count($wordWrapArray));
$this->assertEquals([
'AV VINTE E NOVE DE ABRIL, 965 - QDR 0009;LOTE',
'00001 PLANTA 01; - CENTRO'
], $wordWrapArray);
}

public function testWordwrapEspaco() {
$string = "AV VINTE E NOVE DE ABRIL, 965 - QDR &nbsp;LOTE 00001 PLANTA 01; - CENTRO";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,false);
$this->assertEquals(2, count($wordWrapArray));
$this->assertEquals([
'AV VINTE E NOVE DE ABRIL, 965 - QDR &nbsp;LOTE',
'00001 PLANTA 01; - CENTRO'
], $wordWrapArray);
}

Expand Down
22 changes: 21 additions & 1 deletion tests/WordwrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @author Ricardo Paes
* @since 16/01/2019 às 15:50
* @since 16/01/2019 �s 15:50
*/

namespace Like\Util\HtmlString\Tests;
Expand Down Expand Up @@ -74,4 +74,24 @@ public function testNaoFecharTagListadas(){
self::assertEquals(['<textarea>Teste Tag TextArea'], $wordWrapArray);
}

public function testWordwrapPontoVirgula() {
$string = "AV VINTE E NOVE DE ABRIL, 965 - QDR 0009;LOTE 00001 PLANTA 01; - CENTRO";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,true);
$this->assertEquals(2, count($wordWrapArray));
$this->assertEquals([
'AV VINTE E NOVE DE ABRIL, 965 - QDR 0009;LOTE 00',
'001 PLANTA 01; - CENTRO'
], $wordWrapArray);
}

public function testWordwrapEspaco() {
$string = "AV VINTE E NOVE DE ABRIL, 965 - QDR &nbsp;LOTE 00001 PLANTA 01; - CENTRO";
$wordWrapArray = HtmlString::get($string)->wordwrapArray(48,true);
$this->assertEquals(2, count($wordWrapArray));
$this->assertEquals([
'AV VINTE E NOVE DE ABRIL, 965 - QDR &nbsp;LOTE 0',
'0001 PLANTA 01; - CENTRO'
], $wordWrapArray);
}

}

0 comments on commit 3842b53

Please sign in to comment.