Test Failed
Push — main ( 0a6cd6...1eb369 )
by Daniel
02:10
created

TraitBasic::getMultipleElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
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
trait TraitBasic
32
{
33
34
    protected array $arraySettings = [];
35
36
    private function getCommentsFromFileAsArray(): array
37
    {
38
        return $this->getJsonFromFile('ElectronicInvoiceComments.json');
39
    }
40
41
    private function getElements(\SimpleXMLElement $arrayIn): array
42
    {
43
        $arrayToReturn = [];
44
        foreach ($arrayIn->children('cbc', true) as $key => $value) {
45
            if (count($value->attributes()) === 0) {
46
                $arrayToReturn[$key] = $value->__toString();
47
            } else {
48
                foreach ($value->attributes() as $keyA => $valueA) {
49
                    $arrayToReturn[$key] = [
50
                        $keyA   => $valueA->__toString(),
51
                        'value' => $value->__toString(),
52
                    ];
53
                }
54
            }
55
        }
56
        return $arrayToReturn;
57
    }
58
59
    private function getJsonFromFile(string $strFileName): array
60
    {
61
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . $strFileName;
62
        if (!file_exists($strFileName)) {
63
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
64
        }
65
        $fileHandle = fopen($strFileName, 'r');
66
        if ($fileHandle === false) {
67
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
68
        }
69
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
70
        fclose($fileHandle);
71
        $arrayToReturn = json_decode($fileContent, true);
72
        if (json_last_error() != JSON_ERROR_NONE) {
73
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
74
        }
75
        return $arrayToReturn;
76
    }
77
78
    private function getHierarchyTagOrder(): void
79
    {
80
        $this->arraySettings['CustomOrder'] = $this->getJsonFromFile('ElectronicInvoiceHierarchyTagOrder.json');
81
    }
82
83
    private function getMultipleElementsStandard(array|\SimpleXMLElement $arrayIn): array
84
    {
85
        $arrayToReturn = [];
86
        $intLineNo     = 0;
87
        foreach ($arrayIn as $child) {
88
            $intLineNo++;
89
            $intLineStr = ($intLineNo < 10 ? '0' : '') . $intLineNo;
90
            foreach ($child->children('cbc', true) as $key2 => $value2) {
91
                if (count($value2->attributes()) === 0) {
92
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
93
                } else {
94
                    foreach ($value2->attributes() as $keyA => $valueA) {
95
                        $arrayToReturn[$intLineStr][$key2] = [
96
                            $keyA   => $valueA->__toString(),
97
                            'value' => $value2->__toString(),
98
                        ];
99
                    }
100
                }
101
            }
102
            foreach ($child->children('cac', true) as $key2 => $value2) {
103
                foreach ($value2->children('cbc', true) as $key3 => $value3) {
104
                    $arrayToReturn[$intLineStr][$key2][$key3] = $value3->__toString();
105
                }
106
                foreach ($value2->children('cac', true) as $key3 => $value3) {
107
                    foreach ($value3->children('cbc', true) as $key4 => $value4) {
108
                        $arrayToReturn[$intLineStr][$key2][$key3][$key4] = $value4->__toString();
109
                    }
110
                }
111
            }
112
        }
113
        return $arrayToReturn;
114
    }
115
116
    private function getTagWithCurrencyParameter($childLineExtensionAmount): array
117
    {
118
        return [
119
            'currencyID' => $childLineExtensionAmount->attributes()->currencyID->__toString(),
120
            'value'      => (float) $childLineExtensionAmount->__toString(),
121
        ];
122
    }
123
124
    private function getTagWithCurrencyParameterAsString($childLineExtensionAmount): array
125
    {
126
        return [
127
            'currencyID' => $childLineExtensionAmount->attributes()->currencyID->__toString(),
128
            'value'      => $childLineExtensionAmount->__toString(),
129
        ];
130
    }
131
132
    private function getTagWithUnitCodeParameter($childLineExtensionAmount): array
133
    {
134
        return [
135
            'unitCode' => $childLineExtensionAmount->attributes()->unitCode->__toString(),
136
            'value'    => (float) $childLineExtensionAmount->__toString(),
137
        ];
138
    }
139
}
140