EncodeText::encodeXml()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 16
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @link      https://github.com/knut7/framework/ for the canonical source repository
12
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
13
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
14
 * @author    Marcio Zebedeu - [email protected]
15
 * @version   1.0.2
16
 */
17
18
namespace Ballybran\Core\Http;
19
20
class EncodeText
21
{
22
23
    protected static $version = '1.0';
24
    private static $encode = 'UTF-8';
25
26
27
    public static function encodeHtml($responseData): string
28
    {
29
30
        $htmlResponse = "<table border='1'>";
31
        foreach ($responseData as $key => $value) {
32
            $htmlResponse .= "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";
33
        }
34
        $htmlResponse .= "</table>";
35
        return $htmlResponse;
36
    }
37
38
    public static function encodeJson($responseData): string
39
    {
40
        $jsonResponse = json_encode($responseData);
41
        return $jsonResponse;
42
    }
43
44
    public static function encodeXml($responseData): string
45
    {
46
        // creating object of SimpleXMLElement
47
        header('Content-Type: text/xml');
48
        $version = self::$version;
49
        $encoding = self::$encode;
50
        $xml = new \SimpleXMLElement("<?xml version='$version' encoding='$encoding' ?>\n<mobile></mobile>");
51
52
        foreach ($responseData as $key => $variable) {
53
54
            foreach ($variable as $k => $v) {
55
                $xml->addChild($k, $v);
56
            }
57
58
        }
59
        return $xml->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $xml->asXML() could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
60
    }
61
62
    public static function encodeXmlSaft($responseData): string
63
    {
64
        // creating object of SimpleXMLElement
65
        header('Content-Type: text/xml');
66
        $version = self::$version;
0 ignored issues
show
Unused Code introduced by
The assignment to $version is dead and can be removed.
Loading history...
67
        $encoding = self::$encode;
0 ignored issues
show
Unused Code introduced by
The assignment to $encoding is dead and can be removed.
Loading history...
68
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
69
<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:AO_1.01_01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:OECD:StandardAuditFile-Tax:AO_1.01_01 https://raw.githubusercontent.com/assoft-portugal/SAF-T-AO/master/XSD/SAFTAO1.01_01.xsd">
70
 
71
</AuditFile>');
72
73
        foreach ($responseData as $key => $variable) {
74
75
            foreach ($variable as $k => $v) {
76
                $xml->addChild($k, $v);
77
            }
78
79
        }
80
        return $xml->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $xml->asXML() could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
}
83
84