Passed
Push — master ( 051020...3a085f )
by Roberto
04:32 queued 02:24
created

Strings::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NFePHP\Common;
4
5
/**
6
 * Classe auxiliar para o tratamento de strings
7
 * @category   NFePHP
8
 * @package    NFePHP\Common\Strings
9
 * @copyright  Copyright (c) 2008-2017
10
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
11
 * @author     Roberto L. Machado <linux dot rlm at gmail dot com>
12
 * @link       http://github.com/nfephp-org/nfephp for the canonical source repository
13
 */
14
15
use ForceUTF8\Encoding;
16
17
class Strings
18
{
19
    /**
20
     * Replace all specials characters from string and retuns only 128 basics
21
     * NOTE: only for UTF-8
22
     * @param string $string
23
     * @return  string
24
     */
25 1
    public static function replaceSpecialsChars($string)
26
    {
27 1
        $string = self::squashCharacters($string);
28 1
        $string = str_replace('&', 'e', $string);
29 1
        $string = preg_replace("/[^a-zA-Z0-9 @#,-_.;:$%\/]/", "", $string);
30 1
        return preg_replace("/[<>]/", "", $string);
31
    }
32
    
33
    /**
34
     * Clear inputs for build in XML
35
     * Only UTF-8 characters is acceptable
36
     * & isolated, less than, greater than, quotation marks and apostrophes
37
     * should be replaced by their html equivalent
38
     * Carriage Return, Tab and Line Feed is not acceptable in strings
39
     * Multiple spaces is not acceptable in strings
40
     * And no other control character is acceptable either
41
     * @param string|null $input
42
     * @return string|null
43
     */
44 1
    public static function replaceUnacceptableCharacters($input)
45
    {
46 1
        if (empty($input)) {
47
            return $input;
48
        }
49
        //& isolated, less than, greater than, quotation marks and apostrophes
50
        //should be replaced by their html equivalent
51 1
        $input = str_replace(
52 1
            ['& ','<','>','"',"'"],
53 1
            ['&amp; ','&lt;','&gt;','&quot;','&#39;'],
54
            $input
55
        );
56 1
        $input = self::normalize($input);
57 1
        return trim($input);
58
    }
59
    
60
    /**
61
     * Converts all UTF-8 remains in ASCII
62
     * @param string $input
63
     * @return string
64
     */
65
    public static function toASCII($input)
66
    {
67
        $input = self::normalize($input);
68
        $input = self::squashCharacters($input);
69
        return mb_convert_encoding($input, 'ascii');
70
    }
71
    
72
    /**
73
     * Replaces all accented characters of their ASCII equivalents
74
     * @param string $input
75
     * @return string
76
     */
77 1
    public static function squashCharacters($input)
78
    {
79 1
        $input = trim($input);
80 1
        $aFind = ['á','à','ã','â','é','ê','í','ó','ô','õ','ú','ü',
81
            'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ú','Ü','Ç'];
82 1
        $aSubs = ['a','a','a','a','e','e','i','o','o','o','u','u',
83
            'c','A','A','A','A','E','E','I','O','O','O','U','U','C'];
84 1
        return str_replace($aFind, $aSubs, $input);
85
    }
86
    
87
    /**
88
     * Replace all non-UTF-8 chars to UTF-8
89
     * Remove all control chars
90
     * Remove all multiple spaces
91
     * @param string $input
92
     * @return string
93
     */
94 1
    public static function normalize($input)
95
    {
96
        //Carriage Return, Tab and Line Feed is not acceptable in strings
97 1
        $input = str_replace(["\r","\t","\n"], "", $input);
98
        //Multiple spaces is not acceptable in strings
99 1
        $input = preg_replace('/(?:\s\s+)/', ' ', $input);
100
        //Only UTF-8 characters is acceptable
101 1
        $input = Encoding::fixUTF8($input);
102 1
        $input = preg_replace(
103
            '/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
104
            '|[\x00-\x7F][\x80-\xBF]+'.
105
            '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
106
            '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
107 1
            '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
108 1
            '',
109
            $input
110
        );
111 1
        $input = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
112 1
            '|\xED[\xA0-\xBF][\x80-\xBF]/S', '', $input);
113
        //And no other control character is acceptable either
114 1
        return preg_replace('/[[:cntrl:]]/', '', $input);
115
    }
116
117
    /**
118
     * Remove all non numeric characters from string
119
     * @param string $string
120
     * @return string
121
     */
122 1
    public static function onlyNumbers($string)
123
    {
124 1
        return preg_replace("/[^0-9]/", "", $string);
125
    }
126
    
127
    /**
128
     * Remove unwanted attributes, prefixes, sulfixes and other control
129
     * characters like \r \n \s \t
130
     * @param string $string
131
     * @param boolean $removeEncodingTag remove encoding tag from a xml
132
     * @return string
133
     */
134 2
    public static function clearXmlString($string, $removeEncodingTag = false)
135
    {
136
        $aFind = array(
137 2
            'xmlns:default="http://www.w3.org/2000/09/xmldsig#"',
138
            ' standalone="no"',
139
            'default:',
140
            ':default',
141
            "\n",
142
            "\r",
143
            "\t"
144
        );
145 2
        $retXml = str_replace($aFind, "", $string);
146 2
        $retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml);
147 2
        if ($removeEncodingTag) {
148 1
            $retXml = self::deleteAllBetween($retXml, '<?xml', '?>');
149
        }
150 2
        return $retXml;
151
    }
152
    
153
    /**
154
     * Remove all characters between markers
155
     * @param string $string
156
     * @param string $beginning
157
     * @param string $end
158
     * @return string
159
     */
160 2
    public static function deleteAllBetween($string, $beginning, $end)
161
    {
162 2
        $beginningPos = strpos($string, $beginning);
163 2
        $endPos = strpos($string, $end);
164 2
        if ($beginningPos === false || $endPos === false) {
165
            return $string;
166
        }
167 2
        $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
168 2
        return str_replace($textToDelete, '', $string);
169
    }
170
    
171
    /**
172
     * Clears the xml after adding the protocol, removing repeated namespaces
173
     * @param string $string
174
     * @return string
175
     */
176 1
    public static function clearProtocoledXML($string)
177
    {
178 1
        $procXML = self::clearXmlString($string);
179 1
        $aApp = array('nfe','cte','mdfe');
180 1
        foreach ($aApp as $app) {
181 1
            $procXML = str_replace(
182 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"',
183 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'"',
184
                $procXML
185
            );
186
        }
187 1
        return $procXML;
188
    }
189
    
190
    /**
191
     * Remove some alien chars from txt
192
     * @param string $txt
193
     * @return string
194
     */
195 1
    public static function removeSomeAlienCharsfromTxt($txt)
196
    {
197
        //remove CRs and TABs
198 1
        $txt = str_replace(["\r","\t"], "", $txt);
199
        //remove multiple spaces
200 1
        $txt = preg_replace('/(?:\s\s+)/', ' ', $txt);
201
        //remove spaces at begin and end of fields
202 1
        $txt = str_replace(["| "," |"], "|", $txt);
203 1
        return $txt;
204
    }
205
    
206
    /**
207
     * Creates a string ramdomically with the specified length
208
     * @param int $length
209
     * @return string
210
     */
211 1
    public static function randomString($length)
212
    {
213
        $keyspace = '0123456789abcdefghijklmnopqr'
214 1
            . 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
215 1
        $str = '';
216 1
        $max = mb_strlen($keyspace, '8bit') - 1;
217 1
        for ($i = 0; $i < $length; ++$i) {
218 1
            $str .= $keyspace[rand(0, $max)];
219
        }
220 1
        return $str;
221
    }
222
}
223