Completed
Push — master ( db914b...9ba612 )
by Daniel
01:53
created

InputValidation::establishValidValue()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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