Completed
Push — master ( dda000...a904dd )
by Daniel
02:11
created

InputValidation::applyCurrencyValidations()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 4
nop 3
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2017 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\salariu;
30
31
trait InputValidation
32
{
33
34
    private function applyCurrencyValidations(\Symfony\Component\HttpFoundation\Request $tCSG, $defaults, $allAllowed)
35
    {
36
        $valuesCurrency = $tCSG->get('xMoney'); // 0 or 1 or multiple values
37
        if (is_array($valuesCurrency)) {
38
            $tCSG->request->set('xMoney', array_intersect($valuesCurrency, $allAllowed));
39
        }
40
        if (is_null($valuesCurrency)) {
41
            $tCSG->request->set('xMoney', $defaults);
42
        }
43
    }
44
45
    private function applyYMvalidations(\Symfony\Component\HttpFoundation\Request $tCSG, $ymValues, $dtR)
46
    {
47
        $validOpt = [
48
            'options' => [
49
                'default'   => $dtR['default']->format('Ymd'),
50
                'max_range' => $dtR['maximum']->format('Ymd'),
51
                'min_range' => $dtR['minimum']->format('Ymd'),
52
            ]
53
        ];
54
        $validYM  = filter_var($tCSG->get('ym'), FILTER_VALIDATE_INT, $validOpt);
55
        if (!array_key_exists($validYM, $ymValues)) {
56
            $validYM = $validOpt['options']['default'];
57
        }
58
        $tCSG->request->set('ym', $validYM);
59
    }
60
61
    private function buildYMvalues($dtR)
62
    {
63
        $startDate = $dtR['minimum'];
64
        $endDate   = $dtR['maximumYM'];
65
        $temp      = [];
66
        while ($endDate >= $startDate) {
67
            $temp[$endDate->format('Ymd')] = $endDate->format('Y, m (')
68
                . strftime('%B', mktime(0, 0, 0, $endDate->format('n'), 1, $endDate->format('Y'))) . ')';
69
            $endDate->sub(new \DateInterval('P1M'));
70
        }
71
        return $temp;
72
    }
73
74
    private function dateRangesInScope()
75
    {
76
        $defaultDate = new \DateTime('first day of this month');
77
        $maxDate     = new \DateTime('first day of next month');
78
        $maxDateYM   = new \DateTime('first day of next month');
79
        if (date('d') <= 15) {
80
            $defaultDate->sub(new \DateInterval('P1M'));
81
            $maxDate->sub(new \DateInterval('P1M'));
82
            $maxDateYM->sub(new \DateInterval('P1M'));
83
        }
84
        return [
85
            'default'    => $defaultDate,
86
            'maximum'    => $maxDate,
87
            'maximumInt' => $maxDate->format('Ymd'),
88
            'maximumYM'  => $maxDateYM,
89
            'minimum'    => new \DateTime('2001-01-01'),
90
        ];
91
    }
92
93
    private function determineCrtMinWage(\Symfony\Component\HttpFoundation\Request $tCSG, $inMny)
94
    {
95
        $lngDate    = $tCSG->get('ym');
96
        $intValue   = 0;
97
        $maxCounter = count($inMny['EMW']);
98
        for ($counter = 0; $counter < $maxCounter; $counter++) {
99
            $crtVal         = $inMny['EMW'][$counter];
100
            $crtDV          = \DateTime::createFromFormat('Y-n-j', $crtVal['Year'] . '-' . $crtVal['Month'] . '-1');
101
            $crtDateOfValue = (int) $crtDV->format('Ymd');
102
            if (($lngDate <= $inMny['YM range']['maximumInt']) && ($lngDate >= $crtDateOfValue)) {
103
                $intValue = $crtVal['Value'];
104
                $counter  = $maxCounter;
105
            }
106
        }
107
        return $intValue;
108
    }
109
110
    private function establishFilterParameters($inMny, $validOpts)
111
    {
112
        $validation = FILTER_DEFAULT;
113
        switch ($inMny['VFR']['validation_filter']) {
114
            case "int":
115
                $inVFR                             = $inMny['VFR']['validation_options'];
116
                $validation                        = FILTER_VALIDATE_INT;
117
                $validOpts['options']['max_range'] = $this->getValidOption($inMny['value'], $inVFR, 'max_range');
118
                $validOpts['options']['min_range'] = $this->getValidOption($inMny['value'], $inVFR, 'min_range');
119
                break;
120
            case "float":
121
                $validOpts['options']['decimal']   = $inMny['VFR']['validation_options']['decimals'];
122
                $validation                        = FILTER_VALIDATE_FLOAT;
123
                break;
124
        }
125
        return [
126
            'validation'         => $validation,
127
            'validation_options' => $validOpts,
128
        ];
129
    }
130
131
    private function establishValidValue(\Symfony\Component\HttpFoundation\Request $tCSG, $key, $inMny)
132
    {
133
        $validOpts                       = [];
134
        $validOpts['options']['default'] = $inMny['value'];
135
        if ($key === 'fb') {
136
            $validOpts['options']['default'] = $inMny['fb'];
137
        }
138
        if (in_array($key, ['sm', 'sn'])) {
139
            $validOpts['options']['default']                 = $inMny['MW'];
140
            $inMny['VFR']['validation_options']['min_range'] = $inMny['MW'];
141
        }
142
        $vOptions   = $this->establishFilterParameters($inMny, $validOpts);
143
        $validValue = filter_var($tCSG->get($key), $vOptions['validation'], $vOptions['validation_options']);
144
        return $validValue;
145
    }
146
147
    private function getValidOption($value, $inValuesFilterRules, $optionLabel)
148
    {
149
        $valReturn = $inValuesFilterRules[$optionLabel];
150
        if ($valReturn == 'default') {
151
            $valReturn = $value;
152
        }
153
        return $valReturn;
154
    }
155
156
    protected function processFormInputDefaults(\Symfony\Component\HttpFoundation\Request $tCSG, $aMultiple)
157
    {
158
        foreach ($aMultiple['VFR'] as $key => $value) {
159
            $validValue = trim($tCSG->get($key));
160
            if (array_key_exists('validation_options', $value)) {
161
                $validValue = $this->establishValidValue($tCSG, $key, [
162
                    'value'    => $aMultiple['VFR'][$key]['default'],
163
                    'MW'       => $aMultiple['MW'],
164
                    'VFR'      => $aMultiple['VFR'][$key],
165
                    'YM range' => $aMultiple['YM range'],
166
                    'fb'       => $aMultiple['fb'],
167
                ]);
168
            }
169
            $tCSG->request->set($key, $validValue);
170
        }
171
    }
172
}
173