Passed
Push — master ( 1167f2...7f7b1f )
by Manuel
01:41
created

Response::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the GestPayWS library.
5
 *
6
 * (c) Manuel Dalla Lana <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EndelWar\GestPayWS\Response;
13
14
use EndelWar\GestPayWS\ArrayAccessTrait;
15
use Exception;
16
17
/**
18
 * Class Response
19
 */
20
abstract class Response implements \ArrayAccess
21
{
22
    use ArrayAccessTrait;
23
24
    /**
25
     * @param \SimpleXMLElement $xml
26
     * @throws Exception
27
     */
28
    public function __construct($xml)
29
    {
30
        $array = json_decode(json_encode($xml), true);
31
        $array = array_map(function ($value) {
32
            if (is_array($value) && empty($value)) {
33
                return '';
34
            }
35
36
            return $value;
37
        }, $array);
38
        $this->fromArray($array);
39
    }
40
41
    /**
42
     * @return mixed
43
     */
44
    public function toXML()
45
    {
46
        $data = $this->toArray();
47
        $xml = new \SimpleXMLElement('<GestPayCryptDecrypt/>');
48
        array_walk_recursive($data, function ($value, $key) use ($xml) {
49
            $xml->addChild($key, $value);
50
        });
51
52
        $dom = new \DOMDocument('1.0');
53
        $dom->preserveWhiteSpace = false;
54
        $dom->formatOutput = true;
55
        $dom->loadXML($xml->asXML());
56
57
        return $dom->saveXML(null, LIBXML_NOEMPTYTAG);
58
    }
59
60
    public function isOK()
61
    {
62
        return $this->get('TransactionResult') === 'OK';
63
    }
64
}
65