Passed
Push — main ( 996ebb...3a3fc1 )
by Daniel
12:26
created

TraitBasic::getElementSingle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
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('json/ElectronicInvoiceComments.json');
39
    }
40
41
    private function getElements(\SimpleXMLElement $arrayIn): array
42
    {
43
        $arrayToReturn = [];
44
        if (count($arrayIn->children('cbc', true)) !== 0) { // checking if we have cbc elements
45
            foreach ($arrayIn->children('cbc', true) as $key => $value) {
46
                $arrayToReturn[$key] = $this->getElementSingle($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $value of danielgp\efactura\TraitBasic::getElementSingle() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                $arrayToReturn[$key] = $this->getElementSingle(/** @scrutinizer ignore-type */ $value);
Loading history...
47
            }
48
        }
49
        if (count($arrayIn->children('cac', true)) !== 0) { // checking if we have cac elements
50
            foreach ($arrayIn->children('cac', true) as $key => $value) {
51
                $arrayToReturn[$key] = $this->getElements($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $arrayIn of danielgp\efactura\TraitBasic::getElements() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                $arrayToReturn[$key] = $this->getElements(/** @scrutinizer ignore-type */ $value);
Loading history...
52
            }
53
        }
54
        return $arrayToReturn;
55
    }
56
57
    private function getElementSingle(\SimpleXMLElement $value)
58
    {
59
        $arrayToReturn = [];
60
        if (count($value->attributes()) === 0) {
61
            $arrayToReturn = $value->__toString();
62
        } else {
63
            foreach ($value->attributes() as $keyA => $valueA) {
64
                $arrayToReturn = [
65
                    $keyA   => $valueA->__toString(),
66
                    'value' => $value->__toString(),
67
                ];
68
            }
69
        }
70
        return $arrayToReturn;
71
    }
72
73
    private function getJsonFromFile(string $strFileName): array
74
    {
75
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . $strFileName;
76
        if (!file_exists($strFileName)) {
77
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
78
        }
79
        $fileHandle = fopen($strFileName, 'r');
80
        if ($fileHandle === false) {
81
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
82
        }
83
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
84
        fclose($fileHandle);
85
        $arrayToReturn = json_decode($fileContent, true);
86
        if (json_last_error() != JSON_ERROR_NONE) {
87
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
88
        }
89
        return $arrayToReturn;
90
    }
91
92
    private function getHierarchyTagOrder(): void
93
    {
94
        $this->arraySettings['CustomOrder'] = $this->getJsonFromFile('json/ElectronicInvoiceHierarchyTagOrder.json');
95
    }
96
97
    private function getMultipleElementsStandard(array|\SimpleXMLElement $arrayIn): array
98
    {
99
        $arrayToReturn = [];
100
        $intLineNo     = 0;
101
        foreach ($arrayIn as $child) {
102
            $intLineNo++;
103
            $intLineStr = ($intLineNo < 10 ? '0' : '') . $intLineNo;
104
            foreach ($child->children('cbc', true) as $key2 => $value2) {
105
                if (count($value2->attributes()) === 0) {
106
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
107
                } else {
108
                    foreach ($value2->attributes() as $keyA => $valueA) {
109
                        $arrayToReturn[$intLineStr][$key2] = [
110
                            $keyA   => $valueA->__toString(),
111
                            'value' => $value2->__toString(),
112
                        ];
113
                    }
114
                }
115
            }
116
            foreach ($child->children('cac', true) as $key2 => $value2) {
117
                foreach ($value2->children('cbc', true) as $key3 => $value3) {
118
                    $arrayToReturn[$intLineStr][$key2][$key3] = $value3->__toString();
119
                }
120
                foreach ($value2->children('cac', true) as $key3 => $value3) {
121
                    foreach ($value3->children('cbc', true) as $key4 => $value4) {
122
                        $arrayToReturn[$intLineStr][$key2][$key3][$key4] = $value4->__toString();
123
                    }
124
                }
125
            }
126
        }
127
        return $arrayToReturn;
128
    }
129
130
    private function getTagWithCurrencyParameter($childLineExtensionAmount): array
131
    {
132
        return [
133
            'currencyID' => $childLineExtensionAmount->attributes()->currencyID->__toString(),
134
            'value'      => (float) $childLineExtensionAmount->__toString(),
135
        ];
136
    }
137
138
    private function getTagWithUnitCodeParameter($childLineExtensionAmount): array
139
    {
140
        return [
141
            'unitCode' => $childLineExtensionAmount->attributes()->unitCode->__toString(),
142
            'value'    => (float) $childLineExtensionAmount->__toString(),
143
        ];
144
    }
145
}
146