TraitBasic::getCommentsFromFileAsArray()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * Copyright (c) 2024 - 2025 Daniel Popiniuc.
5
 * All rights reserved. This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 *
10
 * Contributors:
11
 *    Daniel Popiniuc
12
 */
13
14
namespace danielgp\efactura;
15
16
trait TraitBasic
17
{
18
    use \danielgp\io_operations\InputOutputFiles;
19
20
    protected array $arraySettings   = [];
21
    protected array $arrayProcessing = [];
22
23 2
    private function getCommentsFromFileAsArray(): array
24
    {
25 2
        return $this->getArrayFromJsonFile(__DIR__
26 2
                . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceComments.json');
27
    }
28
29 13
    private function getElements(\SimpleXMLElement | null $arrayIn): array
30
    {
31 13
        $arrayToReturn = [];
32 13
        if (!is_null($arrayIn)) {
33 13
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true)) !== 0) { // checking if we have cbc elements
34 13
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true) as $key => $value) {
35 13
                    $arrayToReturn[$key] = $this->getElementSingle($value);
36
                }
37
            }
38 13
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cac'], true)) !== 0) { // checking if we have cac elements
39 13
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cac'], true) as $key => $value) {
40 13
                    $arrayToReturn[$key] = $this->getElements($value);
41
                }
42
            }
43
        }
44 13
        return $arrayToReturn;
45
    }
46
47 13
    private function getElementSingle(\SimpleXMLElement | null $value)
48
    {
49 13
        $arrayToReturn = [];
50 13
        if (!is_null($value)) {
51 13
            if (count($value->attributes()) === 0) {
52 13
                $arrayToReturn = $value->__toString();
53
            } else {
54 13
                $arrayToReturn['value'] = $value->__toString();
55 13
                foreach ($value->attributes() as $keyA => $valueA) {
56 13
                    if (!is_null($valueA) && str_ends_with($valueA, ':CommonAggregateComponents-2')) {
57
                        // nothing
58
                    } else {
59 13
                        $arrayToReturn[$keyA] = $valueA->__toString();
60
                    }
61
                }
62
            }
63
        }
64 13
        return $arrayToReturn;
65
    }
66
67 16
    private function getHierarchyTagOrder(): void
68
    {
69 16
        $this->arraySettings['CustomOrder'] = $this->getArrayFromJsonFile(__DIR__
70 16
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceHierarchyTagOrder.json');
71
    }
72
73 13
    private function getLineStringFromNumber(int $intLineNo): string
74
    {
75 13
        return ($intLineNo < 10 ? '0' : '') . $intLineNo;
76
    }
77
78 13
    private function getMultipleElementsByKey(\SimpleXMLElement $arrayData): array
79
    {
80 13
        $arrayOutput = [];
81 13
        $intLineNo   = 0;
82 13
        foreach ($arrayData as $value2) {
83 13
            $intLineNo++;
84 13
            $intLineStr               = $this->getLineStringFromNumber($intLineNo);
85 13
            $arrayOutput[$intLineStr] = $this->getElements($value2);
86
        }
87 13
        return $arrayOutput;
88
    }
89
90 4
    private function getMultipleElementsStandard(array | \SimpleXMLElement $arrayIn): array
91
    {
92 4
        $arrayToReturn = [];
93 4
        $intLineNo     = 0;
94 4
        foreach ($arrayIn as $child) {
95 4
            $intLineNo++;
96 4
            $intLineStr = $this->getLineStringFromNumber($intLineNo);
97 4
            foreach ($child->children($this->arrayProcessing['mapping']['cbc'], true) as $key2 => $value2) {
98 4
                if (count($value2->attributes()) === 0) {
99 4
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
100
                } else {
101 3
                    $arrayToReturn[$intLineStr][$key2]['value'] = $value2->__toString();
102 3
                    foreach ($value2->attributes() as $keyA => $valueA) {
103 3
                        if (str_ends_with($valueA, ':CommonAggregateComponents-2')) {
104
                            // nothing
105
                        } else {
106 3
                            $arrayToReturn[$intLineStr][$key2][$keyA] = $valueA->__toString();
107
                        }
108
                    }
109
                }
110
            }
111 4
            foreach ($child->children($this->arrayProcessing['mapping']['cac'], true) as $key2 => $value2) {
112 3
                $arrayToReturn[$intLineStr][$key2] = $this->getElements($value2);
113
            }
114
        }
115 4
        return $arrayToReturn;
116
    }
117
118 16
    private function getProcessingDetails(): void
119
    {
120 16
        $this->arrayProcessing = $this->getArrayFromJsonFile(__DIR__
121 16
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceProcessingDetails.json');
122
    }
123
124 13
    public function getRightMethod(string $existingFunction, $givenParameters = null): array | string
125
    {
126
        try {
127 13
            if (is_array($givenParameters)) {
128 2
                return call_user_func_array([$this, $existingFunction], [$givenParameters]);
129
            } else {
130 13
                return call_user_func([$this, $existingFunction], $givenParameters);
131
            }
132
            // @codeCoverageIgnoreStart
133
        } catch (\Exception $ex) {
134
            error_log($ex->getTraceAsString());
135
            return false;
136
        }
137
        // @codeCoverageIgnoreEnd
138
    }
139
140 16
    protected function loadSettingsFromFile(): void
141
    {
142 16
        $this->arraySettings = $this->getArrayFromJsonFile(__DIR__
143 16
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceSettings.json');
144
    }
145
}
146