Passed
Push — main ( 34ca28...996ebb )
by Daniel
02:20
created

TraitVersions::setElementComment()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 3
b 0
f 0
nc 4
nop 1
dl 0
loc 10
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, 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 && !array_key_exists('SchemaLocation', $arrayDocumentData)) {
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('json/ElectronicInvoiceSettings.json');
80
        $this->getHierarchyTagOrder();
81
        $this->arraySettings['Comments'] = [
82
            'CAC' => [],
83
            'CBC' => [],
84
        ];
85
        if ($bolComments) {
86
            $this->getCommentsFromFileIntoSetting();
87
        }
88
    }
89
90
    private function getCommentsFromFileIntoSetting(): void
91
    {
92
        $strGlue                = ' | ';
93
        $arrayFlattenedComments = [];
94
        $arrayComments          = $this->getCommentsFromFileAsArray();
95
        foreach ($arrayComments as $key => $value) {
96
            $strComment = implode($strGlue, [
97
                    $key,
98
                    $value['OperationalTerm']['ro_RO'],
99
                    $value['RequirementID'],
100
                ])
101
                . (array_key_exists('SemanticDataType', $value) ? $strGlue . $value['SemanticDataType'] : '');
102
            if (is_array($value['HierarchycalTagName'])) {
103
                foreach ($value['HierarchycalTagName'] as $value2) {
104
                    $arrayFlattenedComments[$value2] = $strComment;
105
                }
106
            } else {
107
                $arrayFlattenedComments[$value['HierarchycalTagName']] = $strComment;
108
            }
109
        }
110
        $this->arraySettings['Comments'] = $arrayFlattenedComments;
111
    }
112
}
113