Response::toJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * xOrder Response.
5
 *
6
 * @package     craftt/xorder-sdk
7
 * @author      Ryan Stratton <[email protected]>
8
 * @copyright   Copyright (c) Ryan Stratton
9
 * @license     https://github.com/craftt/xorder-php-sdk/blob/master/LICENSE.md Apache 2.0
10
 * @link        https://github.com/craftt/xorder-php-sdk
11
 */
12
13
namespace XOrder;
14
15
use SimpleXMLElement;
16
use XOrder\Contracts\ResponseInterface;
17
18
/**
19
 * Session
20
 */
21
class Response implements ResponseInterface
22
{
23
24
    /**
25
     * @var \SimpleXMLElement
26
     */
27
    private $data;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $xml
33
     */
34 12
    public function __construct($xml)
35
    {
36 12
        $this->data = new SimpleXMLElement($xml);
37 12
    }
38
39
    /**
40
     * Property overloader.
41
     *
42
     * @param  string $property
43
     * @throws \ErrorException
44
     * @return mixed
45
     */
46 6
    public function __get($property)
47
    {
48 6
        if ($property === 'data') {
49 2
            return $this->data;
50
        }
51
52 4
        if (property_exists($this->data, $property)) {
53 2
            return $this->data->$property;
54
        }
55
56 2
        return null;
57
    }
58
59
    /**
60
     * Covert XML response data to array.
61
     *
62
     * @return array
63
     */
64 2
    public function toArray()
65
    {
66 2
        return json_decode($this->toJson(), true);
67
    }
68
69
    /**
70
     * Convery XML response data to JSON.
71
     *
72
     * @return string
73
     */
74 4
    public function toJson()
75
    {
76 4
        return json_encode($this->data);
77
    }
78
}
79