Passed
Push — master ( 94f1bd...3747ce )
by Jonathan
02:16
created

XmlRequest::getContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace CommerceGuys\AuthNet\Request;
4
5
class XmlRequest extends BaseRequest
6
{
7
    /**
8
     * The XSD for the API.
9
     *
10
     * This is not an absolute URL. If passed the absolute URL and error will
11
     * be thrown as E00045.
12
     */
13
    const XSD = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd';
14
15 14
    public function getContentType()
16
    {
17 14
        return 'text/xml';
18
    }
19
20 14
    public function getBody()
21
    {
22
        // The Authorize.net API does not accept an absolute URL for XMLNS.
23 14
        $xml = @new \SimpleXMLElement('<' . $this->type . ' xmlns="' . self::XSD . '"/>');
24 14
        $this->arrayToXml($this->data, $xml);
25 14
        return $xml->asXML();
26
    }
27
28 14
    public function arrayToXml($array, \SimpleXMLElement $xml)
29
    {
30 14
        foreach ($array as $key => $value) {
31 14
            if (is_array($value)) {
32 14
                if (!is_numeric($key)) {
33 14
                    $this->arrayToXml($value, $xml->addChild("$key"));
34 14
                } else {
35
                    $this->arrayToXml($value, $xml);
36
                }
37 14
            } else {
38
                // Make sure that the request data is always escaped.
39
                // See https://secure.php.net/manual/en/simplexmlelement.addchild.php#112204
40 14
                $xml->{$key} = $value;
41
            }
42 14
        }
43 14
    }
44
}
45