Passed
Push — main ( 414ba2...ff44d7 )
by Daniel
02:12
created

electornicInvoiceWrite::setDocumentBasicElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2024 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\efactura;
30
31
class electornicInvoiceWrite
32
{
33
34
    use traitVersions;
35
36
    protected $objXmlWriter;
37
38
    private function setDocumentBasicElements(array $arrayElementWithData): void {
39
        foreach ($arrayElementWithData as $key => $value) {
40
            if (array_key_exists($key, $this->arraySettings['Defaults']['Comments']['CBC'])) {
41
                $this->objXmlWriter->writeComment($this->arraySettings['Defaults']['Comments']['CBC'][$key]);
42
            }
43
            $this->objXmlWriter->writeElement('cbc:' . $key, $value);
44
        }
45
    }
46
47
    private function setDocumentHeader(array $arrayDocumentData): void {
48
        $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']);
49
        foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) {
50
            if ($key === '') {
51
                $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']);
52
                $this->objXmlWriter->writeAttributeNS(NULL, 'xmlns', NULL, $strValue);
53
            } else {
54
                $this->objXmlWriter->writeAttributeNS('xmlns', $key, NULL, $value);
55
            }
56
        }
57
        if (array_key_exists('SchemaLocation', $arrayDocumentData)) {
58
            $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']);
59
        }
60
    }
61
62
    public function writeElectronicInvoice(string $strFile, array $arrayDocumentData): void {
63
        $this->objXmlWriter = new \XMLWriter();
64
        $this->objXmlWriter->openURI($strFile);
65
        $this->objXmlWriter->setIndent(true);
66
        $this->objXmlWriter->setIndentString(str_repeat(' ', 4));
67
        $this->objXmlWriter->startDocument('1.0', 'UTF-8');
68
        // if no DocumentNameSpaces seen take Default ones from local configuration
69
        if (!array_key_exists('DocumentNameSpaces', $arrayDocumentData)) {
70
            $this->getSettingsFromFileIntoMemory();
71
            $arrayDocumentData['DocumentNameSpaces'] = $this->arraySettings['Defaults']['DocumentNameSpaces'];
72
            $arrayDocumentData['SchemaLocation']     = vsprintf($this->arraySettings['Defaults']['SchemaLocation'], [
73
                $arrayDocumentData['DocumentTagName'],
74
                $arrayDocumentData['UBLVersionID'],
75
                $arrayDocumentData['DocumentTagName'],
76
                $arrayDocumentData['UBLVersionID'],
77
            ]);
78
        }
79
        $this->setDocumentHeader($arrayDocumentData);
80
        $this->setDocumentBasicElements($arrayDocumentData['Header']['CommonBasicComponents-2']);
81
        // TODO: add logic for each section
82
        $this->objXmlWriter->endElement(); // Invoice or CreditNote
83
        $this->objXmlWriter->flush();
84
    }
85
}
86