Skip to content

Commit 2e8b7ff

Browse files
committed
[UPD] Strings
1 parent 1d0bb62 commit 2e8b7ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+80
-5
lines changed

.editorconfig

100644100755
File mode changed.

.gitattributes

100644100755
File mode changed.

CHANGELOG.md

100644100755
File mode changed.

CONDUCT.md

100644100755
File mode changed.

CONTRIBUTING.md

100644100755
File mode changed.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"ext-openssl": "*",
3030
"ext-mbstring": "*",
3131
"psr/log": "^1.0",
32-
"league/flysystem": "^1.0"
32+
"league/flysystem": "^1.0",
33+
"neitanod/forceutf8": "^2.0"
3334
},
3435
"require-dev": {
3536
"squizlabs/php_codesniffer": "^2.8",

docs/Certificate.md

100644100755
File mode changed.

docs/Certificate/Asn1.md

100644100755
File mode changed.

docs/Certificate/CertificationChain.md

100644100755
File mode changed.

docs/Certificate/PrivateKey.md

100644100755
File mode changed.

docs/Certificate/PublicKey.md

100644100755
File mode changed.

docs/DOMImproved.md

100644100755
File mode changed.

docs/Keys.md

100644100755
File mode changed.

docs/README.md

100644100755
File mode changed.

docs/Signer.md

100644100755
File mode changed.

docs/Soap/SoapCurl.md

100644100755
File mode changed.

docs/Soap/SoapNative.md

100644100755
File mode changed.

docs/Strings.md

100644100755
+23-2

docs/TimeZoneByUF.md

100644100755
File mode changed.

docs/UFList.md

100644100755
File mode changed.

docs/Validator.md

100644100755
File mode changed.

src/Certificate.php

100644100755
File mode changed.

src/Certificate/Asn1.php

100644100755
File mode changed.

src/Certificate/CertificationChain.php

100644100755
File mode changed.

src/Certificate/Exception/Expired.php

100644100755
File mode changed.

src/Certificate/Oids.php

100644100755
File mode changed.

src/Certificate/PrivateKey.php

100644100755
File mode changed.

src/Certificate/PublicKey.php

100644100755
File mode changed.

src/Certificate/SignatureInterface.php

100644100755
File mode changed.

src/Certificate/VerificationInterface.php

100644100755
File mode changed.

src/Certificate/oids.json

100644100755
File mode changed.

src/DOMImproved.php

100644100755
File mode changed.

src/Exception/CertificateException.php

100644100755
File mode changed.

src/Exception/ExceptionCollection.php

100644100755
File mode changed.

src/Exception/ExceptionInterface.php

100644100755
File mode changed.

src/Exception/IOException.php

100644100755
File mode changed.

src/Exception/InvalidArgumentException.php

100644100755
File mode changed.

src/Exception/LogicException.php

100644100755
File mode changed.

src/Exception/RuntimeException.php

100644100755
File mode changed.

src/Exception/SignerException.php

100644100755
File mode changed.

src/Exception/SoapException.php

100644100755
File mode changed.

src/Exception/UnexpectedValueException.php

100644100755
File mode changed.

src/Exception/ValidatorException.php

100644100755
File mode changed.

src/Keys.php

100644100755
File mode changed.

src/Signer.php

100644100755
File mode changed.

src/Soap/SoapBase.php

100644100755
File mode changed.

src/Soap/SoapClientExtended.php

100644100755
File mode changed.

src/Soap/SoapCode.php

100644100755
File mode changed.

src/Soap/SoapCurl.php

100644100755
File mode changed.

src/Soap/SoapFake.php

100644100755
File mode changed.

src/Soap/SoapInterface.php

100644100755
File mode changed.

src/Soap/SoapNative.php

100644100755
File mode changed.

src/Soap/httpcodes.json

100644100755
File mode changed.

src/Strings.php

100644100755
+45-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @link http://github.com/nfephp-org/nfephp for the canonical source repository
1313
*/
1414

15+
use ForceUTF8\Encoding;
16+
1517
class Strings
1618
{
1719
/**
@@ -28,10 +30,52 @@ public static function replaceSpecialsChars($string)
2830
$aSubs = ['e','a','a','a','a','e','e','i','o','o','o','u','u',
2931
'c','A','A','A','A','E','E','I','O','O','O','U','U','C'];
3032
$newstr = str_replace($aFind, $aSubs, $string);
31-
$newstr = preg_replace("/[^a-zA-Z0-9 @,-_.;:\/]/", "", $newstr);
33+
$newstr = preg_replace("/[^a-zA-Z0-9 @#,-_.;:$%\/]/", "", $newstr);
34+
$newstr = preg_replace("/[<>]/", "", $newstr);
3235
return $newstr;
3336
}
3437

38+
/**
39+
* Clear inputs for build in XML
40+
* Only UTF-8 characters is acceptable
41+
* & isolated, less than, greater than, quotation marks and apostrophes
42+
* should be replaced by their html equivalent
43+
* Carriage Return, Tab and Line Feed is not acceptable in strings
44+
* Multiple spaces is not acceptable in strings
45+
* And no other control character is acceptable either
46+
* @param string|null $input
47+
* @return string|null
48+
*/
49+
public static function replaceUnacceptableCharacters($input)
50+
{
51+
if (empty($input)) {
52+
return $input;
53+
}
54+
//& isolated, less than, greater than, quotation marks and apostrophes
55+
//should be replaced by their html equivalent
56+
$input = str_replace(['& ','<','>','"',"'"], ['&amp; ','&lt;','&gt;','&quot;','&#39;'], $input);
57+
//Carriage Return, Tab and Line Feed is not acceptable in strings
58+
$input = str_replace(["\r","\t","\n"], "", $input);
59+
//Multiple spaces is not acceptable in strings
60+
$input = preg_replace('/(?:\s\s+)/', ' ', $input);
61+
//Only UTF-8 characters is acceptable
62+
$input = Encoding::fixUTF8($input);
63+
$input = preg_replace(
64+
'/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
65+
'|[\x00-\x7F][\x80-\xBF]+'.
66+
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
67+
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
68+
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
69+
'',
70+
$input
71+
);
72+
$input = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
73+
'|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $input);
74+
//And no other control character is acceptable either
75+
$input = preg_replace('/[[:cntrl:]]/', '', $input);
76+
return trim($input);
77+
}
78+
3579
/**
3680
* Remove all non numeric characters from string
3781
* @param string $string

src/TimeZoneByUF.php

100644100755
File mode changed.

src/UFList.php

100644100755
File mode changed.

src/Validator.php

100644100755
File mode changed.

tests/DomImprovedTest.php

100644100755
File mode changed.

tests/KeysTest.php

100644100755
File mode changed.

tests/SignerTest.php

100644100755
File mode changed.

tests/Soap/SoapCodeTest.php

100644100755
File mode changed.

tests/Soap/SoapCurlTest.php

100644100755
File mode changed.

tests/Soap/SoapFakeTest.php

100644100755
File mode changed.

tests/Soap/SoapNativeTest.php

100644100755
File mode changed.

tests/StringsTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ class StringsTest extends \PHPUnit\Framework\TestCase
99
public function testReplaceSpecialsChars()
1010
{
1111
$txtSujo = "Esse é um código cheio de @$#$! , - . ; : / COISAS e 12093876486";
12-
$txtLimpo = "Esse e um codigo cheio de @ , - . ; : / COISAS e 12093876486";
12+
$txtLimpo = "Esse e um codigo cheio de @$#$ , - . ; : / COISAS e 12093876486";
1313
$resp = Strings::replaceSpecialsChars($txtSujo);
1414
$this->assertEquals($txtLimpo, $resp);
1515
}
1616

17+
public function testReplaceUnacceptableCharacters()
18+
{
19+
$txtSujo = "Contribuições R$ 200,00 @ # * IPI: 15% Caixa D'agua Rico & Rich < > \" \t \r \n ";
20+
$txtSujo .= mb_convert_encoding(" teste ç Á ã é ø", 'ISO-8859-1');
21+
$txtLimpo = "Contribuições R$ 200,00 @ # * IPI: 15% Caixa D&#39;agua Rico &amp; Rich &lt; &gt; &quot; teste ç Á ã é ø";
22+
$resp = Strings::replaceUnacceptableCharacters($txtSujo);
23+
$this->assertEquals($txtLimpo, $resp);
24+
}
25+
1726
public function testClearXmlString()
1827
{
1928
$xmlSujo = file_get_contents(__DIR__. self::TEST_XML_PATH . 'NFe/xml-sujo.xml');

tests/ValidatorTest.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)