Passed
Push — main ( 01772d...cee9ce )
by Daniel
15:10 queued 12:02
created

readElectronicXmlHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * Copyright (c) 2024, Daniel Popiniuc and its licensors.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of the Eclipse Public License v1.0
8
 * which accompanies this distribution, and is available at
9
 * http://www.eclipse.org/legal/epl-v20.html
10
 *
11
 * Contributors:
12
 *    Daniel Popiniuc
13
 */
14
15
namespace danielgp\efactura;
16
17
class ClassElectronicInvoiceRead
18
{
19
    use TraitBasic;
20
    use TraitTax;
21
    use TraitLines;
22
23 13
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
24
    {
25 13
        $arrayOut = [];
26 13
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
27 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cac'], true)->$strElement)) {
28 13
                if ($strElement === 'PartyTaxScheme') {
29 12
                    $arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data']
30 12
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
31
                } else {
32 13
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
33 13
                            ->children($this->arrayProcessing['mapping']['cac'], true)->$strElement);
34
                }
35
            }
36 13
            if (isset($arrayIn['data']->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement)) {
37 3
                if ($strElement === 'EndpointID') {
38 3
                    $arrayOut['EndpointID'] = [
39 3
                        'schemeID' => $arrayIn['data']
40 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
41 3
                            ->attributes()->schemeID->__toString(),
42 3
                        'value'    => $arrayIn['data']
43 3
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->EndpointID
44 3
                            ->__toString(),
45 3
                    ];
46
                } else {
47
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']
48
                            ->children($this->arrayProcessing['mapping']['cbc'], true)->$strElement);
49
                }
50
            }
51
        }
52 13
        return $arrayOut;
53
    }
54
55 13
    private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string
56
    {
57 13
        $arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]);
58 13
        return $arrayPieces[count($arrayPieces) - 1];
59
    }
60
61 13
    public function getDocumentRoot(object $objFile): array
62
    {
63 13
        $arrayDocument = [
64 13
            'DocumentTagName'    => $objFile->getName(),
65 13
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
66 13
        ];
67 13
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
68 11
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
69 10
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
70
            }
71
        }
72 13
        if ($arrayDocument['DocumentTagName'] === 'header') {
73
            foreach ($objFile->attributes() as $attributeName => $attributeValue) {
74
                $arrayDocument['header'][$attributeName] = $attributeValue->__toString();
75
            }
76
        }
77 13
        return $arrayDocument;
78
    }
79
80 13
    private function getElementsOrdered(array $arrayDataIn): array
81
    {
82 13
        $arrayOutput = [];
83 13
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
84 13
            if (isset($arrayDataIn['data']->$value)) {
85 13
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
86
            }
87
        }
88 13
        return $arrayOutput;
89
    }
90
91 13
    private function getHeader(array $arrayParams): array
92
    {
93 13
        $arrayDocument = [
94 13
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
95 13
        ];
96 13
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
97 13
            if (isset($arrayParams['CAC']->$key)) {
98 13
                $arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value);
99
            }
100
        }
101 13
        return $arrayDocument;
102
    }
103
104 13
    private function getHeaderComponents(array $arrayParams, string $key, string $value): array | string
105
    {
106 13
        $arrayDocument = [];
107 13
        if ($value === 'SingleCompany') {
108 13
            $arrayDocument = [
109 13
                'Party' => $this->getAccountingCustomerOrSupplierParty([
110 13
                    'data' => $arrayParams['CAC']->$key
111 13
                        ->children($this->arrayProcessing['mapping']['cac'], true)->Party,
112 13
                    'type' => $key,
113 13
                ])
114 13
            ];
115
        } else {
116 13
            $arrayMapping  = [
117 13
                'Multiple'                  => [
118 13
                    'getMultipleElementsByKey'
119 13
                    , $arrayParams['data']->children('cac', true)->$key
120 13
                ],
121 13
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
122 13
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
123 13
                'SingleCompanyWithoutParty' => [
124 13
                    'getAccountingCustomerOrSupplierParty'
125 13
                    , [
126 13
                        'data' => $arrayParams['CAC']->$key,
127 13
                        'type' => $key,
128 13
                    ]
129 13
                ],
130 13
            ];
131 13
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
132
        }
133 13
        return $arrayDocument;
134
    }
135
136 13
    public function readElectronicXmlHeader(string $strFile): \SimpleXMLElement
137
    {
138 13
        $this->getProcessingDetails();
139 13
        $this->getHierarchyTagOrder();
140 13
        $flags      = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR;
141 13
        $bolIsLocal = is_file($strFile);
142 13
        return new \SimpleXMLElement($strFile, $flags, $bolIsLocal);
143
    }
144
145 13
    public function readElectronicInvoice(string $strFile): array
146
    {
147
        /*$this->getProcessingDetails();
148
        $this->getHierarchyTagOrder();
149
        $flags                   = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR;
150
        $bolIsLocal              = is_file($strFile);
151
        $objFile                 = new \SimpleXMLElement($strFile, $flags, $bolIsLocal);
152
        $arrayDocument           = $this->getDocumentRoot($objFile);*/
153 13
        $objFile                 = $this->readElectronicXmlHeader($strFile);
154 13
        $arrayDocument           = $this->getDocumentRoot($objFile);
155 13
        $this->setArrayProcessing($arrayDocument['DocumentNameSpaces']);
156 13
        $strMap                  = $this->arrayProcessing['mapping'];
157 13
        $arrayBasics             = $this->getElementsOrdered([
158 13
            'data'          => $objFile->children($strMap['cbc'], true),
159 13
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces'][$strMap['cbc']],
160 13
        ]);
161 13
        $arrayAggregates         = $this->getHeader([
162 13
            'CAC'  => $objFile->children($strMap['cac'], true),
163 13
            'data' => $objFile,
164 13
        ]);
165 13
        $arrayDocument['Header'] = [
166 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cbc']) => $arrayBasics,
167 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], $strMap['cac']) => $arrayAggregates,
168 13
        ];
169 13
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
170 13
        return $arrayDocument;
171
    }
172
173 13
    private function setArrayProcessing(array $arrayDocumentNameSpaces): void
174
    {
175 13
        $bolMappingsNotSet = [
176 13
            'cac' => true,
177 13
            'cbc' => true,
178 13
        ];
179 13
        foreach ($arrayDocumentNameSpaces as $key => $value) {
180 13
            if (str_ends_with($value, ':CommonAggregateComponents-2') && $bolMappingsNotSet['cac']) {
181 13
                $this->arrayProcessing['mapping']['cac'] = $key;
182 13
                $bolMappingsNotSet['cac']                = false;
183
            }
184 13
            if (str_ends_with($value, ':CommonBasicComponents-2') && $bolMappingsNotSet['cbc']) {
185 13
                $this->arrayProcessing['mapping']['cbc'] = $key;
186 13
                $bolMappingsNotSet['cbc']                = false;
187
            }
188
        }
189
    }
190
}
191