Passed
Push — main ( 9731f3...a349e7 )
by Daniel
02:58
created

TraitBasic::getMultipleElementsStandard()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 7
eloc 17
nc 11
nop 1
dl 0
loc 26
ccs 16
cts 16
cp 1
crap 7
rs 8.8333
c 2
b 2
f 0
1
<?php
2
3
/*
4
 * Copyright (c) 2024, Daniel Popiniuc and its licensors.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of the Eclipse Public License v1.0
8
 * which accompanies this distribution, and is available at
9
 * http://www.eclipse.org/legal/epl-v20.html
10
 *
11
 * Contributors:
12
 *    Daniel Popiniuc
13
 */
14
15
namespace danielgp\efactura;
16
17
trait TraitBasic
18
{
19
    protected array $arraySettings   = [];
20
    protected array $arrayProcessing = [];
21
22 1
    private function getCommentsFromFileAsArray(): array
23
    {
24 1
        return $this->getJsonFromFile('config/ElectronicInvoiceComments.json');
25
    }
26
27 13
    private function getElements(\SimpleXMLElement | null $arrayIn): array
28
    {
29 13
        $arrayToReturn = [];
30 13
        if (!is_null($arrayIn)) {
31 13
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true)) !== 0) { // checking if we have cbc elements
32 13
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true) as $key => $value) {
33 13
                    $arrayToReturn[$key] = $this->getElementSingle($value);
34
                }
35
            }
36 13
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cac'], true)) !== 0) { // checking if we have cac elements
37 13
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cac'], true) as $key => $value) {
38 13
                    $arrayToReturn[$key] = $this->getElements($value);
39
                }
40
            }
41
        }
42 13
        return $arrayToReturn;
43
    }
44
45 13
    private function getElementSingle(\SimpleXMLElement | null $value)
46
    {
47 13
        $arrayToReturn = [];
48 13
        if (!is_null($value)) {
49 13
            if (count($value->attributes()) === 0) {
50 13
                $arrayToReturn = $value->__toString();
51
            } else {
52 13
                $arrayToReturn['value'] = $value->__toString();
53 13
                foreach ($value->attributes() as $keyA => $valueA) {
54 13
                    if (str_ends_with($valueA, ':CommonAggregateComponents-2')) {
0 ignored issues
show
Bug introduced by
It seems like $valueA can also be of type null; however, parameter $haystack of str_ends_with() does only seem to accept string, 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

54
                    if (str_ends_with(/** @scrutinizer ignore-type */ $valueA, ':CommonAggregateComponents-2')) {
Loading history...
55
                        // nothing
56
                    } else {
57 13
                        $arrayToReturn[$keyA] = $valueA->__toString();
58
                    }
59
                }
60
            }
61
        }
62 13
        return $arrayToReturn;
63
    }
64
65 13
    private function getJsonFromFile(string $strFileName): array
66
    {
67 13
        $strFileName = __DIR__ . DIRECTORY_SEPARATOR . $strFileName;
68 13
        if (!file_exists($strFileName)) {
69
            // @codeCoverageIgnoreStart
70
            throw new \RuntimeException(sprintf('File %s does not exists!', $strFileName));
71
            // @codeCoverageIgnoreEnd
72
        }
73 13
        $fileHandle = fopen($strFileName, 'r');
74 13
        if ($fileHandle === false) {
75
            // @codeCoverageIgnoreStart
76
            throw new \RuntimeException(sprintf('Unable to open file %s for read purpose!', $strFileName));
77
            // @codeCoverageIgnoreEnd
78
        }
79 13
        $fileContent   = fread($fileHandle, ((int) filesize($strFileName)));
80 13
        fclose($fileHandle);
81 13
        $arrayToReturn = json_decode($fileContent, true);
82 13
        if (json_last_error() != JSON_ERROR_NONE) {
83
            // @codeCoverageIgnoreStart
84
            throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $strFileName));
85
            // @codeCoverageIgnoreEnd
86
        }
87 13
        return $arrayToReturn;
88
    }
89
90 13
    private function getHierarchyTagOrder(): void
91
    {
92 13
        $this->arraySettings['CustomOrder'] = $this->getJsonFromFile('config/ElectronicInvoiceHierarchyTagOrder.json');
93
    }
94
95 13
    private function getLineStringFromNumber(int $intLineNo): string
96
    {
97 13
        return ($intLineNo < 10 ? '0' : '') . $intLineNo;
98
    }
99
100 13
    private function getMultipleElementsByKey(\SimpleXMLElement $arrayData): array
101
    {
102 13
        $arrayOutput = [];
103 13
        $intLineNo   = 0;
104 13
        foreach ($arrayData as $value2) {
105 13
            $intLineNo++;
106 13
            $intLineStr               = $this->getLineStringFromNumber($intLineNo);
107 13
            $arrayOutput[$intLineStr] = $this->getElements($value2);
108
        }
109 13
        return $arrayOutput;
110
    }
111
112 4
    private function getMultipleElementsStandard(array | \SimpleXMLElement $arrayIn): array
113
    {
114 4
        $arrayToReturn = [];
115 4
        $intLineNo     = 0;
116 4
        foreach ($arrayIn as $child) {
117 4
            $intLineNo++;
118 4
            $intLineStr = $this->getLineStringFromNumber($intLineNo);
119 4
            foreach ($child->children($this->arrayProcessing['mapping']['cbc'], true) as $key2 => $value2) {
120 4
                if (count($value2->attributes()) === 0) {
121 4
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
122
                } else {
123 3
                    $arrayToReturn[$intLineStr][$key2]['value'] = $value2->__toString();
124 3
                    foreach ($value2->attributes() as $keyA => $valueA) {
125 3
                        if (str_ends_with($valueA, ':CommonAggregateComponents-2')) {
126
                            // nothing
127
                        } else {
128 3
                            $arrayToReturn[$intLineStr][$key2][$keyA] = $valueA->__toString();
129
                        }
130
                    }
131
                }
132
            }
133 4
            foreach ($child->children($this->arrayProcessing['mapping']['cac'], true) as $key2 => $value2) {
134 3
                $arrayToReturn[$intLineStr][$key2] = $this->getElements($value2);
135
            }
136
        }
137 4
        return $arrayToReturn;
138
    }
139
140 13
    private function getProcessingDetails(): void
141
    {
142 13
        $this->arrayProcessing = $this->getJsonFromFile('config/ElectronicInvoiceProcessingDetails.json');
143
    }
144
145 13
    public function getRightMethod(string $existingFunction, $givenParameters = null): array | string
146
    {
147
        try {
148 13
            if (is_array($givenParameters)) {
149 2
                return call_user_func_array([$this, $existingFunction], [$givenParameters]);
150
            } else {
151 13
                return call_user_func([$this, $existingFunction], $givenParameters);
152
            }
153
            // @codeCoverageIgnoreStart
154
        } catch (\Exception $ex) {
155
            echo $ex->getMessage();
156
            return false;
157
        }
158
        // @codeCoverageIgnoreEnd
159
    }
160
}
161