Passed
Push — main ( fea1d9...10585a )
by Daniel
02:09
created

TraitBasic::getJsonFromFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.8666
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
    protected array $arraySettings   = [];
34
    protected array $arrayProcessing = [];
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 getMultipleElementsByKey(\SimpleXMLElement $child3, string $strKey): array
105
    {
106
        $arrayOutput = [];
107
        $intLineNo   = 0;
108
        foreach ($child3->children('cac', true)->$strKey as $value2) {
109
            $intLineNo++;
110
            $intLineStr               = $this->getLineStringFromNumber($intLineNo);
111
            $arrayOutput[$intLineStr] = $this->getElements($value2);
112
        }
113
        return $arrayOutput;
114
    }
115
116
    private function getMultipleElementsStandard(array|\SimpleXMLElement $arrayIn): array
117
    {
118
        $arrayToReturn = [];
119
        $intLineNo     = 0;
120
        foreach ($arrayIn as $child) {
121
            $intLineNo++;
122
            $intLineStr = $this->getLineStringFromNumber($intLineNo);
123
            foreach ($child->children('cbc', true) as $key2 => $value2) {
124
                if (count($value2->attributes()) === 0) {
125
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
126
                } else {
127
                    $arrayToReturn[$intLineStr][$key2]['value'] = $value2->__toString();
128
                    foreach ($value2->attributes() as $keyA => $valueA) {
129
                        $arrayToReturn[$intLineStr][$key2][$keyA] = $valueA->__toString();
130
                    }
131
                }
132
            }
133
            foreach ($child->children('cac', true) as $key2 => $value2) {
134
                $arrayToReturn[$intLineStr][$key2] = $this->getElements($value2);
135
            }
136
        }
137
        return $arrayToReturn;
138
    }
139
140
    private function getProcessingDetails(): void
141
    {
142
        $this->arrayProcessing = $this->getJsonFromFile('json/ElectronicInvoiceProcessingDetails.json');
143
    }
144
}
145