Node   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 22
eloc 128
c 2
b 0
f 0
dl 0
loc 279
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectValue() 0 3 1
A isNull() 0 3 1
A getValue() 0 3 1
A getLineNr() 0 3 1
A addChild() 0 3 1
A accept() 0 9 2
A getName() 0 3 1
A getChild() 0 9 3
A getChildren() 0 11 4
A getValueFrom() 0 3 1
A __construct() 0 5 1
A setName() 0 3 1
A hasChild() 0 9 3
A getType() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of byrokrat\autogiro.
5
 *
6
 * byrokrat\autogiro is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\autogiro is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\autogiro\Tree;
25
26
use byrokrat\autogiro\Visitor\VisitorInterface;
27
28
/**
29
 * Defines a node in the parse tree
30
 */
31
class Node
32
{
33
    public const ACCOUNT = 'Account';
34
    public const ACTIVE_YEAR = 'ActiveYear';
35
    public const ADRESS_1 = 'Adress1';
36
    public const ADRESS_2 = 'Adress2';
37
    public const ADRESS_3 = 'Adress3';
38
    public const ADRESS_4 = 'Adress4';
39
    public const AMOUNT = 'Amount';
40
    public const CITY = 'City';
41
    public const CLOSING = 'Closing';
42
    public const COMMENT = 'Comment';
43
    public const CREATED = 'Created';
44
    public const DATE = 'Date';
45
    public const DIRECTION = 'Direction';
46
    public const INFO = 'Info';
47
    public const INTERVAL = 'Interval';
48
    public const MAX_AMOUNT = 'MaxAmount';
49
    public const NEW_DATE = 'NewDate';
50
    public const NUMBER = 'Number';
51
    public const OBJ = 'Object';
52
    public const OPENING = 'Opening';
53
    public const PAYEE_BANKGIRO = 'PayeeBankgiro';
54
    public const PAYEE_BGC_NUMBER = 'PayeeBgcNumber';
55
    public const PAYER_NUMBER = 'PayerNumber';
56
    public const POST_CODE = 'Postcode';
57
    public const REFERENCE = 'Reference';
58
    public const REFUND_DATE = 'RefundDate';
59
    public const REPETITIONS = 'Repetitions';
60
    public const SERIAL_NUMBER = 'Serial';
61
    public const STATE_ID = 'StateId';
62
    public const STATUS = 'Status';
63
    public const TYPE = 'Type';
64
    public const UPDATED = 'Updated';
65
    public const VALID_FROM_DATE = 'ValidFromDate';
66
67
    public const AMENDMENT_FLAG = 'AmendmentFlag';
68
    public const CREATED_FLAG = 'CreatedFlag';
69
    public const DELETED_FLAG = 'DeletedFlag';
70
    public const ERROR_FLAG = 'ErrorFlag';
71
    public const FAILED_FLAG = 'FailedFlag';
72
    public const REVOCATION_FLAG = 'RevocationFlag';
73
    public const SUCCESSFUL_FLAG = 'SuccessfulFlag';
74
75
    public const AUTOGIRO_REQUEST_FILE = 'AutogiroRequestFile';
76
77
    public const MANDATE_REQUEST_SECTION = 'MandateRequestSection';
78
    public const ACCEPT_DIGITAL_MANDATE_REQUEST = 'AcceptDigitalMandateRequest';
79
    public const CREATE_MANDATE_REQUEST = 'CreateMandateRequest';
80
    public const DELETE_MANDATE_REQUEST = 'DeleteMandateRequest';
81
    public const REJECT_DIGITAL_MANDATE_REQUEST = 'RejectDigitalMandateRequest';
82
    public const UPDATE_MANDATE_REQUEST = 'UpdateMandateRequest';
83
    public const NEW_PAYER_NUMBER = 'NewPayerNumber';
84
    public const OLD_PAYER_NUMBER = 'OldPayerNumber';
85
    public const NEW_PAYEE_BANKGIRO = 'NewPayeeBankgiro';
86
    public const OLD_PAYEE_BANKGIRO = 'OldPayeeBankgiro';
87
88
    public const PAYMENT_REQUEST_SECTION = 'PaymentRequestSection';
89
    public const INCOMING_PAYMENT_REQUEST = 'IncomingPaymentRequest';
90
    public const OUTGOING_PAYMENT_REQUEST = 'OutgoingPaymentRequest';
91
92
    public const AMENDMENT_REQUEST_SECTION = 'AmendmentRequestSection';
93
    public const AMENDMENT_REQUEST = 'AmendmentRequest';
94
95
    public const AUTOGIRO_PAYMENT_RESPONSE_FILE = 'AutogiroPaymentResponseFile';
96
97
    public const INCOMING_PAYMENT_RESPONSE_SECTION = 'IncomingPaymentResponseSection';
98
    public const INCOMING_PAYMENT_RESPONSE_SECTION_OPENING = 'IncomingPaymentResponseSectionOpening';
99
    public const INCOMING_PAYMENT_COUNT = 'IncomingPaymentCount';
100
    public const FAILED_INCOMING_PAYMENT_RESPONSE = 'FailedIncomingPaymentResponse';
101
    public const SUCCESSFUL_INCOMING_PAYMENT_RESPONSE = 'SuccessfulIncomingPaymentResponse';
102
103
    public const OUTGOING_PAYMENT_RESPONSE_SECTION = 'OutgoingPaymentResponseSection';
104
    public const OUTGOING_PAYMENT_RESPONSE_SECTION_OPENING = 'OutgoingPaymentResponseSectionOpening';
105
    public const OUTGOING_PAYMENT_COUNT = 'OutgoingPaymentCount';
106
    public const SUCCESSFUL_OUTGOING_PAYMENT_RESPONSE = 'SuccessfulOutgoingPaymentResponse';
107
    public const FAILED_OUTGOING_PAYMENT_RESPONSE = 'FailedOutgoingPaymentResponse';
108
109
    public const REFUND_PAYMENT_RESPONSE_SECTION = 'RefundPaymentResponseSection';
110
    public const REFUND_PAYMENT_RESPONSE_SECTION_OPENING = 'RefundPaymentResponseSectionOpening';
111
    public const REFUND_PAYMENT_COUNT = 'RefundPaymentCount';
112
    public const REFUND_PAYMENT_RESPONSE = 'RefundPaymentResponse';
113
114
    public const AUTOGIRO_PAYMENT_RESPONSE_OLD_FILE = 'AutogiroPaymentResponseOldFile';
115
    public const OUTGOING_PAYMENT_RESPONSE = 'OutgoingPaymentResponse';
116
    public const INCOMING_PAYMENT_RESPONSE = 'IncomingPaymentResponse';
117
118
    public const AUTOGIRO_MANDATE_RESPONSE_FILE = 'AutogiroMandateResponseFile';
119
    public const MANDATE_RESPONSE = 'MandateResponse';
120
121
    public const DIGITAL_MANDATE_FILE = 'DigitalMandateFile';
122
    public const DIGITAL_MANDATE_COUNT = 'DigitalMandateCount';
123
    public const DIGITAL_MANDATE = 'DigitalMandate';
124
125
    public const AUTOGIRO_PAYMENT_REJECTION_FILE = 'AutogiroPaymentRejectionFile';
126
    public const INCOMING_PAYMENT_REJECTION_RESPONSE = 'IncomingPaymentRejectionResponse';
127
    public const OUTGOING_PAYMENT_REJECTION_RESPONSE = 'OutgoingPaymentRejectionResponse';
128
129
    public const AUTOGIRO_AMENDMENT_RESPONSE_FILE = 'AutogiroAmendmentResponseFile';
130
    public const SUCCESSFUL_INCOMING_AMENDMENT_RESPONSE = 'SuccessfulIncomingAmendmentResponse';
131
    public const SUCCESSFUL_OUTGOING_AMENDMENT_RESPONSE = 'SuccessfulOutgoingAmendmentResponse';
132
    public const SUCCESSFUL_AMENDMENT_RESPONSE = 'SuccessfulAmendmentResponse';
133
    public const FAILED_INCOMING_AMENDMENT_RESPONSE = 'FailedIncomingAmendmentResponse';
134
    public const FAILED_OUTGOING_AMENDMENT_RESPONSE = 'FailedOutgoingAmendmentResponse';
135
    public const FAILED_AMENDMENT_RESPONSE = 'FailedAmendmentResponse';
136
137
    public const AUTOGIRO_PAYMENT_EXTRACT_FILE = 'AutogiroPaymentExtractFile';
138
    public const INCOMING_PAYMENT_EXTRACT = 'IncomingPayment';
139
    public const OUTGOING_PAYMENT_EXTRACT = 'OutgoingPayment';
140
141
    public const AUTOGIRO_MANDATE_EXTRACT_FILE = 'AutogiroMandateExtractFile';
142
    public const MANDATE_EXTRACT = 'Mandate';
143
144
    /**
145
     * @var Node[]
146
     */
147
    private $children = [];
148
149
    /**
150
     * @var int
151
     */
152
    private $lineNr = 0;
153
154
    /**
155
     * @var string
156
     */
157
    private $name;
158
159
    /**
160
     * @var mixed
161
     */
162
    private $value;
163
164
    /**
165
     * @param mixed $value
166
     */
167
    public function __construct(int $lineNr = 0, $value = null)
168
    {
169
        $this->lineNr = $lineNr;
170
        $this->value = $value;
171
        $this->name = basename(str_replace('\\', '/', get_class($this)));
172
    }
173
174
    /**
175
     * Accept a visitor
176
     */
177
    public function accept(VisitorInterface $visitor): void
178
    {
179
        $visitor->visitBefore($this);
180
181
        foreach ($this->getChildren() as $node) {
182
            $node->accept($visitor);
183
        }
184
185
        $visitor->visitAfter($this);
186
    }
187
188
    /**
189
     * Get line number this node definition started at
190
     */
191
    public function getLineNr(): int
192
    {
193
        return $this->lineNr;
194
    }
195
196
    /**
197
     * Get node name
198
     */
199
    public function getName(): string
200
    {
201
        return $this->name;
202
    }
203
204
    /**
205
     * Set a custom node name
206
     */
207
    public function setName(string $name): void
208
    {
209
        $this->name = $name;
210
    }
211
212
    /**
213
     * Get node type identifier
214
     */
215
    public function getType(): string
216
    {
217
        return basename(str_replace('\\', '/', get_class()));
218
    }
219
220
    /**
221
     * Get raw value wrapped by node
222
     *
223
     * @return mixed Loaded node value
224
     */
225
    public function getValue()
226
    {
227
        return $this->value;
228
    }
229
230
    /**
231
     * Check if this is a null object implementation
232
     */
233
    public function isNull(): bool
234
    {
235
        return false;
236
    }
237
238
    /**
239
     * Set a child node
240
     */
241
    public function addChild(Node $node): void
242
    {
243
        $this->children[] = $node;
244
    }
245
246
    /**
247
     * Get child node
248
     */
249
    public function getChild(string $name): Node
250
    {
251
        foreach ($this->children as $node) {
252
            if (strcasecmp($node->getName(), $name) == 0) {
253
                return $node;
254
            }
255
        }
256
257
        return new NullNode();
258
    }
259
260
    /**
261
     * Check if child exists
262
     */
263
    public function hasChild(string $name): bool
264
    {
265
        foreach ($this->children as $node) {
266
            if (strcasecmp($node->getName(), $name) == 0) {
267
                return true;
268
            }
269
        }
270
271
        return false;
272
    }
273
274
    /**
275
     * Get registered child nodes
276
     *
277
     * @return Node[]
278
     */
279
    public function getChildren(string $name = ''): array
280
    {
281
        $nodes = [];
282
283
        foreach ($this->children as $node) {
284
            if (!$name || strcasecmp($node->getName(), $name) == 0) {
285
                $nodes[] = $node;
286
            }
287
        }
288
289
        return $nodes;
290
    }
291
292
    /**
293
     * Get raw value wrapped in child node
294
     *
295
     * @return mixed Loaded node value
296
     */
297
    public function getValueFrom(string $name)
298
    {
299
        return $this->getChild($name)->getValue();
300
    }
301
302
    /**
303
     * Get raw value wrapped in child 'object' node
304
     *
305
     * @return mixed Loaded node value
306
     */
307
    public function getObjectValue()
308
    {
309
        return $this->getChild(self::OBJ)->getValue();
310
    }
311
}
312