Passed
Push — main ( aaa3e3...d81296 )
by Daniel
12:10
created

TraitVersions::getCommentsFromFileAsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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