Passed
Push — main ( be0efc...81eb9b )
by Daniel
02:08
created

TraitVersions::getCommentsFromFileIntoSetting()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 19
rs 9.4555
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 TraitVersions
32
{
33
34
    use TraitBasic;
35
36
    private function establishCurrentVersion(array $arrayKnownVersions): array {
37
        $arrayVersionToReturn = [];
38
        foreach ($arrayKnownVersions as $value) {
39
            $dtValidityStart = new \DateTime($value['Validity']['Start']);
40
            $dtValidityEnd   = new \DateTime($value['Validity']['End']);
41
            $dtValidityNow   = new \DateTime();
42
            if (($dtValidityNow >= $dtValidityStart) && ($dtValidityNow <= $dtValidityEnd)) {
43
                $arrayVersionToReturn = [
44
                    'UBL'     => $value['UBL'],
45
                    'CIUS-RO' => $value['CIUS-RO'],
46
                ];
47
            }
48
        }
49
        return $arrayVersionToReturn;
50
    }
51
52
    private function getDefaultsIntoDataSet(array $arrayDocumentData): array {
53
        $arrayOutput = [];
54
        if (!array_key_exists('DocumentNameSpaces', $arrayDocumentData)) {
55
            $arrayVersions = $this->establishCurrentVersion($this->arraySettings['Versions']);
56
            $arrayOutput   = [
57
                'Root'    => [
58
                    'DocumentNameSpaces' => $this->arraySettings['Defaults']['DocumentNameSpaces'],
59
                    'SchemaLocation'     => vsprintf($this->arraySettings['Defaults']['SchemaLocation'], [
60
                        $arrayDocumentData['DocumentTagName'],
61
                        $arrayVersions['UBL'],
62
                        $arrayDocumentData['DocumentTagName'],
63
                        $arrayVersions['UBL'],
64
                    ]),
65
                ],
66
                'UBL'     => $arrayVersions['UBL'],
67
                'CIUS-RO' => $arrayVersions['CIUS-RO'],
68
            ];
69
        }
70
        return $arrayOutput;
71
    }
72
73
    private function getSettingsFromFileIntoMemory(bool $bolComments): void {
74
        $this->arraySettings             = $this->getJsonFromFile('ElectronicInvoiceSettings.json');
75
        $this->arraySettings['Comments'] = [
76
            'CAC' => [],
77
            'CBC' => [],
78
        ];
79
        if ($bolComments) {
80
            $this->getCommentsFromFileIntoSetting();
81
        }
82
    }
83
84
    private function getCommentsFromFileIntoSetting(): void {
85
        $strGlue                = ' | ';
86
        $arrayFlattenedComments = [];
87
        $arrayComments          = $this->getJsonFromFile('ElectronicInvoiceComments.json');
88
        foreach ($arrayComments as $key => $value) {
89
            $strComment = implode($strGlue, [
90
                    $key,
91
                    $value['OperationalTerm'],
92
                    $value['RequirementID'],
93
                ]) . (array_key_exists('SemanticDataType', $value) ? $strGlue . $value['SemanticDataType'] : '');
94
            if (is_array($value['HierarchycalTagName'])) {
95
                foreach ($value['HierarchycalTagName'] as $value2) {
96
                    $arrayFlattenedComments[$value2] = $strComment;
97
                }
98
            } else {
99
                $arrayFlattenedComments[$value['HierarchycalTagName']] = $strComment;
100
            }
101
        }
102
        $this->arraySettings['Comments'] = $arrayFlattenedComments;
103
    }
104
105
    private function setElementComment(string $strKey): void {
106
        if (array_key_exists($strKey, $this->arraySettings['Comments'])) {
107
            $elementComment = $this->arraySettings['Comments'][$strKey];
108
            if (is_array($elementComment)) {
109
                foreach ($elementComment as $value) {
110
                    $this->objXmlWriter->writeComment($value);
111
                }
112
            } else {
113
                $this->objXmlWriter->writeComment($elementComment);
114
            }
115
        }
116
    }
117
}
118