Completed
Pull Request — master (#51)
by Günter
02:20
created

Result::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP\Frame\Response;
13
14
use DOMElement;
15
use DOMNode;
16
17
class Result
18
{
19
    protected $code;
20
21
    protected $msg;
22
23
    protected $msgLang = 'en';
24
25
    protected $values = [];
26
27
    protected $extValues = [];
28
29
    public function __construct(DOMElement $node)
30
    {
31
        if ($node->hasAttribute('code')) {
32
            $this->code = (int) $node->getAttribute('code');
33
        }
34
35
        $this->parseResultNode($node);
36
    }
37
38
    public function success()
39
    {
40
        if ($this->code() >= 1000 && $this->code() < 2000) {
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    public function code()
48
    {
49
        return $this->code;
50
    }
51
52
    public function message()
53
    {
54
        return $this->msg;
55
    }
56
57
    public function messageLanguage()
58
    {
59
        return $this->msgLang;
60
    }
61
62
    public function values()
63
    {
64
        return $this->values;
65
    }
66
67
    public function extValues()
68
    {
69
        return $this->extValues;
70
    }
71
72
    protected function parseResultNode($node)
73
    {
74
        foreach ($node->childNodes as $each) {
75
            if ($each->nodeType !== XML_ELEMENT_NODE) {
76
                continue;
77
            }
78
79
            switch($each->localName) {
80
                case 'msg':
81
                    $this->msg = $each->nodeValue;
82
                    if ($each->hasAttribute('lang')) {
83
                        $this->msgLang = $each->getAttribute('lang');
84
                    }
85
                    break;
86
87
                case 'value':
88
                    $this->values = $this->nodeToArray($each);
89
                    break;
90
91
                case 'extValue':
92
                    break;
93
94
                default:
95
                    break;
96
            }
97
        }
98
    }
99
100
    private function nodeToArray(DOMNode $node)
101
    {
102
        $tmp = [];
103
        foreach ($node->childNodes as $each) {
104
            if ($each->nodeType !== XML_ELEMENT_NODE) {
105
                continue;
106
            }
107
108
            // if node only has a type attribute lets distinguish them directly
109
            // and then ignore the attribtue
110
            if ($each->hasAttribute('type')) {
111
                $key = $each->localName . '@' . $each->getAttribute('type');
112
                $ignore_attributes = true;
113
            } else {
114
                $key = $each->localName;
115
                $ignore_attributes = false;
116
            }
117
118
            // in case of special keys, always create array
119
            if (isset($this->n2a_force_array[$key])) {
0 ignored issues
show
Bug introduced by
The property n2a_force_array does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
120
                $current = &$tmp[$key][];
121
                end($tmp[$key]);
122
                $insert_key = key($tmp[$key]);
123
            }
124
            // if key already exists, dynamically create an array
125
            elseif (isset($tmp[$key])) {
126
                if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) {
127
                    $tmp[$key] = [$tmp[$key]];
128
                }
129
                $current = &$tmp[$key][];
130
                end($tmp[$key]);
131
                $insert_key = key($tmp[$key]);
132
            }
133
            // key was not yet set, so lets start off with a string
134
            else {
135
                $current = &$tmp[$key];
136
                $insert_key = null;
137
            }
138
139
            if ($each->hasChildNodes()) {
140
                $current = $this->nodeToArray($each);
141
            } else {
142
                $current = $each->nodeValue;
143
144
                if (!$ignore_attributes && !isset($this->n2a_ignore_attr[$each->localName]) && $each->hasAttributes()) {
0 ignored issues
show
Bug introduced by
The property n2a_ignore_attr does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
145
                    foreach ($each->attributes as $attr) {
146
147
                        // single attribute with empty node, use the attr-value directly
148
                        if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) {
149
                            $current = $attr->nodeValue;
150
                            break;
151
                        }
152
153
                        if ($insert_key) {
154
                            if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) {
155
                                $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]];
156
                            }
157
                            $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue;
158
                        } else {
159
                            $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue;
160
                        }
161
                    }
162
                }
163
            }
164
        }
165
166
        return $tmp;
167
    }
168
}
169