Passed
Push — main ( 38ac7b...8cd4e0 )
by Daniel
02:08
created

TraitVersions::getCommentsFromFileAsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
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 TraitComments;
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, bool $bolSchemaLocation): array
54
    {
55
        $arrayVersions = $this->establishCurrentVersion($this->arraySettings['Versions']);
56
        $arrayOutput   = [];
57
        if (!array_key_exists('DocumentNameSpaces', $arrayDocumentData)) {
58
            $arrayOutput = [
59
                'Root'    => [
60
                    'DocumentNameSpaces' => $this->arraySettings['Defaults']['DocumentNameSpaces'],
61
                ],
62
                'UBL'     => $arrayVersions['UBL'],
63
                'CIUS-RO' => $arrayVersions['CIUS-RO'],
64
            ];
65
        }
66
        if ($bolSchemaLocation) {
67
            $arrayOutput['Root']['SchemaLocation'] = vsprintf($this->arraySettings['Defaults']['SchemaLocation'], [
68
                $arrayDocumentData['DocumentTagName'],
69
                $arrayVersions['UBL'],
70
                $arrayDocumentData['DocumentTagName'],
71
                $arrayVersions['UBL'],
72
            ]);
73
        }
74
        return $arrayOutput;
75
    }
76
77
    private function getSettingsFromFileIntoMemory(bool $bolComments): void
78
    {
79
        $this->arraySettings             = $this->getJsonFromFile('ElectronicInvoiceSettings.json');
80
        $this->arraySettings['Comments'] = [
81
            'CAC' => [],
82
            'CBC' => [],
83
        ];
84
        if ($bolComments) {
85
            $this->getCommentsFromFileIntoSetting();
86
        }
87
    }
88
89
    private function getCommentsFromFileIntoSetting(): void
90
    {
91
        $strGlue                = ' | ';
92
        $arrayFlattenedComments = [];
93
        $arrayComments          = $this->getCommentsFromFileAsArray();
94
        foreach ($arrayComments as $key => $value) {
95
            $strComment = implode($strGlue, [
96
                        $key,
97
                        $value['OperationalTerm']['ro_RO'],
98
                        $value['RequirementID'],
99
                    ])
100
                    . (array_key_exists('SemanticDataType', $value) ? $strGlue . $value['SemanticDataType'] : '');
101
            if (is_array($value['HierarchycalTagName'])) {
102
                foreach ($value['HierarchycalTagName'] as $value2) {
103
                    $arrayFlattenedComments[$value2] = $strComment;
104
                }
105
            } else {
106
                $arrayFlattenedComments[$value['HierarchycalTagName']] = $strComment;
107
            }
108
        }
109
        $this->arraySettings['Comments'] = $arrayFlattenedComments;
110
    }
111
112
    private function setElementComment(string $strKey): void
113
    {
114
        if (array_key_exists($strKey, $this->arraySettings['Comments'])) {
115
            $elementComment = $this->arraySettings['Comments'][$strKey];
116
            if (is_array($elementComment)) {
117
                foreach ($elementComment as $value) {
118
                    $this->objXmlWriter->writeComment($value);
119
                }
120
            } else {
121
                $this->objXmlWriter->writeComment($elementComment);
122
            }
123
        }
124
    }
125
}
126