Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

Strings::clearXmlString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 14
nc 2
nop 2
crap 2
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
class Strings
16
{
17
    /**
18
     * Replace all specials characters from string and retuns only 128 basics
19
     * NOTE: only for UTF-8
20
     * @param string $string
21
     * @return  string
22
     */
23 1
    public static function replaceSpecialsChars($string)
24
    {
25 1
        $string = trim($string);
26 1
        $aFind = ['&','á','à','ã','â','é','ê','í','ó','ô','õ','ú','ü',
27
            'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ú','Ü','Ç'];
28 1
        $aSubs = ['e','a','a','a','a','e','e','i','o','o','o','u','u',
29
            'c','A','A','A','A','E','E','I','O','O','O','U','U','C'];
30 1
        $newstr = str_replace($aFind, $aSubs, $string);
31 1
        $newstr = preg_replace("/[^a-zA-Z0-9 @,-.;:\/]/", "", $newstr);
32 1
        return $newstr;
33
    }
34
    
35
    /**
36
     * Remove unwanted attributes, prefixes, sulfixes and other control
37
     * characters like \r \n \s \t
38
     * @param string $string
39
     * @param boolean $removeEncodingTag remove encoding tag from a xml
40
     * @return string
41
     */
42 2
    public static function clearXmlString($string, $removeEncodingTag = false)
43
    {
44
        $aFind = array(
45 2
            'xmlns:default="http://www.w3.org/2000/09/xmldsig#"',
46
            ' standalone="no"',
47
            'default:',
48
            ':default',
49
            "\n",
50
            "\r",
51
            "\t"
52
        );
53 2
        $retXml = str_replace($aFind, "", $string);
54 2
        $retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml);
55 2
        if ($removeEncodingTag) {
56 1
            $retXml = self::deleteAllBetween($retXml, '<?xml', '?>');
57
        }
58 2
        return $retXml;
59
    }
60
    
61
    /**
62
     * Remove all characters between markers
63
     * @param string $string
64
     * @param string $beginning
65
     * @param string $end
66
     * @return string
67
     */
68 2
    public static function deleteAllBetween($string, $beginning, $end)
69
    {
70 2
        $beginningPos = strpos($string, $beginning);
71 2
        $endPos = strpos($string, $end);
72 2
        if ($beginningPos === false || $endPos === false) {
73
            return $string;
74
        }
75 2
        $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
76 2
        return str_replace($textToDelete, '', $string);
77
    }
78
    
79
    /**
80
     * Clears the xml after adding the protocol, removing repeated namespaces
81
     * @param string $string
82
     * @return string
83
     */
84 1
    public static function clearProtocoledXML($string)
85
    {
86 1
        $procXML = self::clearXmlString($string);
87 1
        $aApp = array('nfe','cte','mdfe');
88 1
        foreach ($aApp as $app) {
89 1
            $procXML = str_replace(
90 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"',
91 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'"',
92
                $procXML
93
            );
94
        }
95 1
        return $procXML;
96
    }
97
    
98
    /**
99
     * Creates a string ramdomically with the specified length
100
     * @param int $length
101
     * @return string
102
     */
103 1
    public static function randomString($length)
104
    {
105
        $keyspace = '0123456789abcdefghijklmnopqr'
106 1
            . 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
107 1
        $str = '';
108 1
        $max = mb_strlen($keyspace, '8bit') - 1;
109 1
        for ($i = 0; $i < $length; ++$i) {
110 1
            $str .= $keyspace[rand(0, $max)];
111
        }
112 1
        return $str;
113
    }
114
}
115