Passed
Push — main ( a21d08...ec9792 )
by Daniel
02:20
created

TraitBasic::getMultipleElementsStandard()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 15
c 1
b 1
f 0
nc 9
nop 1
dl 0
loc 22
rs 9.2222
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|null $arrayIn): array
42
    {
43
        $arrayToReturn = [];
44
        if (!is_null($arrayIn)) {
45
            if (count($arrayIn->children('cbc', true)) !== 0) { // checking if we have cbc elements
46
                foreach ($arrayIn->children('cbc', true) as $key => $value) {
47
                    $arrayToReturn[$key] = $this->getElementSingle($value);
48
                }
49
            }
50
            if (count($arrayIn->children('cac', true)) !== 0) { // checking if we have cac elements
51
                foreach ($arrayIn->children('cac', true) as $key => $value) {
52
                    $arrayToReturn[$key] = $this->getElements($value);
53
                }
54
            }
55
        }
56
        return $arrayToReturn;
57
    }
58
59
    private function getElementSingle(\SimpleXMLElement|null $value)
60
    {
61
        $arrayToReturn = [];
62
        if (!is_null($value)) {
63
            if (count($value->attributes()) === 0) {
64
                $arrayToReturn = $value->__toString();
65
            } else {
66
                $arrayToReturn['value'] = $value->__toString();
67
                foreach ($value->attributes() as $keyA => $valueA) {
68
                    $arrayToReturn[$keyA] = $valueA->__toString();
69
                }
70
            }
71
        }
72
        return $arrayToReturn;
73
    }
74
75
    private function getJsonFromFile(string $strFileName): array
76
    {
77
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . $strFileName;
78
        if (!file_exists($strFileName)) {
79
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
80
        }
81
        $fileHandle = fopen($strFileName, 'r');
82
        if ($fileHandle === false) {
83
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
84
        }
85
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
86
        fclose($fileHandle);
87
        $arrayToReturn = json_decode($fileContent, true);
88
        if (json_last_error() != JSON_ERROR_NONE) {
89
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
90
        }
91
        return $arrayToReturn;
92
    }
93
94
    private function getHierarchyTagOrder(): void
95
    {
96
        $this->arraySettings['CustomOrder'] = $this->getJsonFromFile('json/ElectronicInvoiceHierarchyTagOrder.json');
97
    }
98
99
    private function getLineStringFromNumber(int $intLineNo): string
100
    {
101
        return ($intLineNo < 10 ? '0' : '') . $intLineNo;
102
    }
103
104
    private function getMultipleElementsStandard(array|\SimpleXMLElement $arrayIn): array
105
    {
106
        $arrayToReturn = [];
107
        $intLineNo     = 0;
108
        foreach ($arrayIn as $child) {
109
            $intLineNo++;
110
            $intLineStr = $this->getLineStringFromNumber($intLineNo);
111
            foreach ($child->children('cbc', true) as $key2 => $value2) {
112
                if (count($value2->attributes()) === 0) {
113
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
114
                } else {
115
                    $arrayToReturn[$intLineStr][$key2]['value'] = $value2->__toString();
116
                    foreach ($value2->attributes() as $keyA => $valueA) {
117
                        $arrayToReturn[$intLineStr][$key2][$keyA] = $valueA->__toString();
118
                    }
119
                }
120
            }
121
            foreach ($child->children('cac', true) as $key2 => $value2) {
122
                $arrayToReturn[$intLineStr][$key2] = $this->getElements($value2);
123
            }
124
        }
125
        return $arrayToReturn;
126
    }
127
}
128