TraitVersions   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 97
c 6
b 0
f 0
dl 0
loc 153
ccs 107
cts 107
cp 1
rs 9.92
wmc 31

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultsIntoDataSet() 0 22 4
A setManageComment() 0 17 3
A getSettingsFromFileIntoMemory() 0 10 2
A loadSettingsAndManageDefaults() 0 14 3
A setNumericValue() 0 16 4
A getCommentsFromFileIntoSetting() 0 21 5
A setCategorizedVerifications() 0 19 6
A establishCurrentVersion() 0 15 4
1
<?php
2
3
/*
4
 * Copyright (c) 2024 - 2025 Daniel Popiniuc.
5
 * All rights reserved. This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * which accompanies this distribution, and is available at
8
 * http://www.eclipse.org/legal/epl-v10.html
9
 *
10
 * Contributors:
11
 *    Daniel Popiniuc
12
 */
13
14
namespace danielgp\efactura;
15
16
trait TraitVersions
17
{
18
    use TraitBasic;
19
20 16
    private function establishCurrentVersion(array $arrayKnownVersions): array
21
    {
22 16
        $arrayVersionToReturn = [];
23 16
        foreach ($arrayKnownVersions as $value) {
24 16
            $dtValidityStart = new \DateTime($value['Validity']['Start']);
25 16
            $dtValidityEnd   = new \DateTime($value['Validity']['End']);
26 16
            $dtValidityNow   = new \DateTime();
27 16
            if (($dtValidityNow >= $dtValidityStart) && ($dtValidityNow <= $dtValidityEnd)) {
28 16
                $arrayVersionToReturn = [
29 16
                    'UBL'     => $value['UBL'],
30 16
                    'CIUS-RO' => $value['CIUS-RO'],
31 16
                ];
32
            }
33
        }
34 16
        return $arrayVersionToReturn;
35
    }
36
37 16
    private function getDefaultsIntoDataSet(array $arrayDocumentData, bool $bolSchemaLocation): array
38
    {
39 16
        $arrayVersions = $this->establishCurrentVersion($this->arraySettings['Versions']);
40 16
        $arrayOutput   = [];
41 16
        if (!array_key_exists('DocumentNameSpaces', $arrayDocumentData)) {
42 3
            $arrayOutput = [
43 3
                'Root'    => [
44 3
                    'DocumentNameSpaces' => $this->arraySettings['Defaults']['DocumentNameSpaces'],
45 3
                ],
46 3
                'UBL'     => $arrayVersions['UBL'],
47 3
                'CIUS-RO' => $arrayVersions['CIUS-RO'],
48 3
            ];
49
        }
50 16
        if ($bolSchemaLocation && !array_key_exists('SchemaLocation', $arrayDocumentData)) {
51 1
            $arrayOutput['Root']['SchemaLocation'] = vsprintf($this->arraySettings['Defaults']['SchemaLocation'], [
52 1
                $arrayDocumentData['DocumentTagName'],
53 1
                $arrayVersions['UBL'],
54 1
                $arrayDocumentData['DocumentTagName'],
55 1
                $arrayVersions['UBL'],
56 1
            ]);
57
        }
58 16
        return $arrayOutput;
59
    }
60
61 16
    private function getSettingsFromFileIntoMemory(bool $bolComments): void
62
    {
63 16
        $this->loadSettingsFromFile();
64 16
        $this->getHierarchyTagOrder();
65 16
        $this->arraySettings['Comments'] = [
66 16
            'CAC' => [],
67 16
            'CBC' => [],
68 16
        ];
69 16
        if ($bolComments) {
70 2
            $this->getCommentsFromFileIntoSetting();
71
        }
72
    }
73
74 2
    private function getCommentsFromFileIntoSetting(): void
75
    {
76 2
        $strGlue            = ' | ';
77 2
        $arrayFlattComments = [];
78 2
        $arrayComments      = $this->getCommentsFromFileAsArray();
79 2
        foreach ($arrayComments as $key => $value) {
80 2
            $strComment = implode($strGlue, [
81 2
                    $key,
82 2
                    $value['OperationalTerm']['ro_RO'],
83 2
                    $value['RequirementID'],
84 2
                ])
85 2
                . (array_key_exists('SemanticDataType', $value) ? $strGlue . $value['SemanticDataType'] : '');
86 2
            if (is_array($value['HierarchycalTagName'])) {
87 2
                foreach ($value['HierarchycalTagName'] as $value2) {
88 2
                    $arrayFlattComments[$value2] = $strComment;
89
                }
90
            } else {
91 2
                $arrayFlattComments[$value['HierarchycalTagName']] = $strComment;
92
            }
93
        }
94 2
        $this->arraySettings['Comments'] = $arrayFlattComments;
95
    }
96
97 16
    private function loadSettingsAndManageDefaults(array $arrayData, array $arrayFeatures): array
98
    {
99
        // if no DocumentNameSpaces seen take Default ones from local configuration
100 16
        $this->getSettingsFromFileIntoMemory($arrayFeatures['Comments']);
101 16
        $arrayDefaults = $this->getDefaultsIntoDataSet($arrayData, $arrayFeatures['SchemaLocation']);
102 16
        if ($arrayDefaults !== []) {
103 3
            $arrayData = array_merge($arrayData, $arrayDefaults['Root']);
104 3
            if (!array_key_exists('CustomizationID', $arrayData['Header']['CommonBasicComponents-2'])) {
105 3
                $arrayData['Header']['CommonBasicComponents-2']['CustomizationID'] = 'urn:cen.eu:en16931:2017'
106 3
                    . '#compliant#urn:efactura.mfinante.ro:CIUS-RO:' . $arrayDefaults['CIUS-RO'];
107 3
                $arrayData['Header']['CommonBasicComponents-2']['UBLVersionID']    = $arrayDefaults['UBL'];
108
            }
109
        }
110 16
        return $arrayData;
111
    }
112
113 16
    private function setCategorizedVerifications(array $arrayDataIn)
114
    {
115 16
        $strCategoryToReturn = '';
116 16
        $key                 = implode('_', [$arrayDataIn['commentParentKey'], $arrayDataIn['tag']]);
117 16
        $arrayVarious        = $this->arrayProcessing['WritingCategorization'];
118 16
        if (array_key_exists($key, $arrayVarious['Key'])) {
119 6
            $strCategoryToReturn = $arrayVarious['Key'][$key];
120 16
        } elseif (array_key_exists($arrayDataIn['tag'], $arrayVarious['Tag'])) {
121 16
            $strCategoryToReturn = $arrayVarious['Tag'][$arrayDataIn['tag']];
122 16
        } elseif (in_array($arrayDataIn['commentParentKey'], $arrayVarious['CommentParrentKey'])) {
123 12
            $strCategoryToReturn = 'SingleElementWithAttribute';
124 16
        } elseif ($arrayDataIn['matches'] !== []) {
125 16
            $strCategoryToReturn = 'SingleElementWithAttribute';
126 16
        } elseif (is_array($arrayDataIn['data'])) {
127 16
            $strCategoryToReturn = 'ElementsOrdered';
128
        } else {
129 16
            $strCategoryToReturn = 'SingleElementWithAttribute';
130
        }
131 16
        return $strCategoryToReturn;
132
    }
133
134 16
    private function setManageComment(string $strCommentParentKey, array $arrayIn): string
135
    {
136 16
        if (str_starts_with($strCommentParentKey, 'AllowanceCharge')) {
137 3
            $arrayCommentPieces = explode('_', $strCommentParentKey);
138
            // carefully manage a child to decide on comment tag
139 3
            $strChargeIndicator = $arrayIn['ChargeIndicator'];
140 3
            if (in_array($strChargeIndicator, ['0', '1'])) {
141 1
                $strChargeIndicator = [
142 1
                    '0' => 'false',
143 1
                    '1' => 'true',
144 1
                    ][$arrayIn['ChargeIndicator']];
145
            }
146 3
            array_splice($arrayCommentPieces, 0, 1, 'AllowanceCharge~ChargeIndicator'
147 3
                . ucfirst($strChargeIndicator));
148 3
            $strCommentParentKey = implode('_', $arrayCommentPieces);
149
        }
150 16
        return $strCommentParentKey;
151
    }
152
153 16
    protected function setNumericValue(string $strTag, array $arrayDataIn): string | float
154
    {
155 16
        $sReturn      = $arrayDataIn['value'];
156 16
        $arrayRawTags = ['CreditedQuantity', 'EndpointID', 'InvoicedQuantity', 'ItemClassificationCode', 'PriceAmount'];
157 16
        if (is_numeric($arrayDataIn['value']) && !in_array($strTag, $arrayRawTags)) {
158 16
            $fmt = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL);
159 16
            $fmt->setAttribute(\NumberFormatter::GROUPING_USED, 0);
160 16
            $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
161
            // if contains currencyID consider 2 decimals as minimum
162 16
            if (in_array('currencyID', array_keys($arrayDataIn))) {
163 16
                $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2);
164
            }
165 16
            $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2);
166 16
            $sReturn = $fmt->format($arrayDataIn['value']);
167
        }
168 16
        return $sReturn;
169
    }
170
}
171