Passed
Push — main ( 7b6852...4f122a )
by Daniel
02:58
created

getBasicOrAggregateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
20
    use TraitBasic;
21
    use TraitTax;
22
    use TraitLines;
23
24 13
    private function getAccountingCustomerOrSupplierParty(array $arrayIn): array
25
    {
26 13
        $arrayOut = [];
27 13
        foreach ($this->arraySettings['CustomOrder'][$arrayIn['type']] as $strElement) {
28 13
            if (isset($arrayIn['data']->children('cac', true)->$strElement)) {
29 13
                if ($strElement === 'PartyTaxScheme') {
30 12
                    $arrayOut[$strElement] = $this->getMultipleElementsByKey($arrayIn['data']
31 12
                            ->children('cac', true)->$strElement);
32
                } else {
33 13
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cac', true)->$strElement);
34
                }
35
            }
36 13
            if (isset($arrayIn['data']->children('cbc', true)->$strElement)) {
37 3
                if ($strElement === 'EndpointID') {
38 3
                    $arrayOut['EndpointID'] = [
39 3
                        'schemeID' => $arrayIn['data']->children('cbc', true)->EndpointID
40 3
                            ->attributes()->schemeID->__toString(),
41 3
                        'value'    => $arrayIn['data']->children('cbc', true)->EndpointID->__toString(),
42 3
                    ];
43
                } else {
44
                    $arrayOut[$strElement] = $this->getElements($arrayIn['data']->children('cbc', true)->$strElement);
45
                }
46
            }
47
        }
48 13
        return $arrayOut;
49
    }
50
51 13
    private function getBasicOrAggregateKey(array $arrayDocNmSp, string $strBasOrAggr): string
52
    {
53 13
        $arrayPieces = explode(':', $arrayDocNmSp[$strBasOrAggr]);
54 13
        return $arrayPieces[count($arrayPieces) - 1];
55
    }
56
57 13
    private function getDocumentRoot(object $objFile): array
58
    {
59 13
        $arrayDocument = [
60 13
            'DocumentTagName'    => $objFile->getName(),
61 13
            'DocumentNameSpaces' => $objFile->getDocNamespaces(true),
62 13
        ];
63 13
        if (array_key_exists('xsi', $arrayDocument['DocumentNameSpaces'])) {
64 11
            if (isset($objFile->attributes('xsi', true)->schemaLocation)) {
65 10
                $arrayDocument['SchemaLocation'] = $objFile->attributes('xsi', true)->schemaLocation;
66
            }
67
        }
68 13
        return $arrayDocument;
69
    }
70
71 13
    private function getElementsOrdered(array $arrayDataIn): array
72
    {
73 13
        $arrayOutput = [];
74 13
        foreach ($this->arraySettings['CustomOrder']['Header_CBC'] as $value) {
75 13
            if (isset($arrayDataIn['data']->$value)) {
76 13
                $arrayOutput[$value] = $arrayDataIn['data']->$value->__toString();
77
            }
78
        }
79 13
        return $arrayOutput;
80
    }
81
82 13
    private function getHeader(array $arrayParams): array
83
    {
84 13
        $arrayDocument = [
85 13
            'TaxTotal' => $this->getTax($arrayParams['CAC']->TaxTotal),
86 13
        ];
87 13
        foreach ($this->arraySettings['CustomOrder']['Header_CAC'] as $key => $value) {
88 13
            if (isset($arrayParams['CAC']->$key)) {
89 13
                $arrayDocument[$key] = $this->getHeaderComponents($arrayParams, $key, $value);
90
            }
91
        }
92 13
        return $arrayDocument;
93
    }
94
95 13
    private function getHeaderComponents(array $arrayParams, string $key, string $value): array|string
96
    {
97 13
        $arrayDocument = [];
98 13
        if ($value === 'SingleCompany') {
99 13
            $arrayDocument = [
100 13
                'Party' => $this->getAccountingCustomerOrSupplierParty([
101 13
                    'data' => $arrayParams['CAC']->$key->children('cac', true)->Party,
102 13
                    'type' => $key,
103 13
                ])
104 13
            ];
105
        } else {
106 13
            $arrayMapping  = [
107 13
                'Multiple'                  => [
108 13
                    'getMultipleElementsByKey'
109 13
                    , $arrayParams['data']->children('cac', true)->$key
110 13
                ],
111 13
                'MultipleStandard'          => ['getMultipleElementsStandard', $arrayParams['CAC']->$key],
112 13
                'Single'                    => ['getElements', $arrayParams['CAC']->$key],
113 13
                'SingleCompanyWithoutParty' => [
114 13
                    'getAccountingCustomerOrSupplierParty'
115 13
                    , [
116 13
                        'data' => $arrayParams['CAC']->$key,
117 13
                        'type' => $key,
118 13
                    ]
119 13
                ],
120 13
            ];
121 13
            $arrayDocument = $this->getRightMethod($arrayMapping[$value][0], $arrayMapping[$value][1]);
122
        }
123 13
        return $arrayDocument;
124
    }
125
126 13
    public function readElectronicInvoice(string $strFile): array
127
    {
128 13
        $this->getProcessingDetails();
129 13
        $this->getHierarchyTagOrder();
130 13
        $flags                   = LIBXML_PARSEHUGE | LIBXML_BIGLINES | LIBXML_NOERROR;
131 13
        $bolIsLocal              = is_file($strFile);
132 13
        $objFile                 = new \SimpleXMLElement($strFile, $flags, $bolIsLocal);
133 13
        $arrayDocument           = $this->getDocumentRoot($objFile);
134 13
        $arrayBasics             = $this->getElementsOrdered([
135 13
            'data'          => $objFile->children('cbc', true),
136 13
            'namespace_cbc' => $arrayDocument['DocumentNameSpaces']['cbc'],
137 13
        ]);
138 13
        $arrayAggregates         = $this->getHeader([
139 13
            'CAC'  => $objFile->children('cac', true),
140 13
            'data' => $objFile,
141 13
        ]);
142 13
        $arrayDocument['Header'] = [
143 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], 'cbc') => $arrayBasics,
144 13
            $this->getBasicOrAggregateKey($arrayDocument['DocumentNameSpaces'], 'cac') => $arrayAggregates,
145 13
        ];
146 13
        $arrayDocument['Lines']  = $this->getDocumentLines($objFile, $arrayDocument['DocumentTagName']);
147 13
        return $arrayDocument;
148
    }
149
}
150