Completed
Push — master ( 9ba612...ba7d13 )
by Daniel
02:01
created

InputValidation   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 22
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 127
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A applyYMvalidations() 0 15 2
A buildYMvalues() 0 12 2
A dateRangesInScope() 0 18 2
B determineCrtMinWage() 0 16 6
A establishFilterParameters() 0 20 3
A establishValidValue() 0 12 2
A getValidOption() 0 8 2
A processFormInputDefaults() 0 15 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 establishFilterParameters($inMny, $validOpts)
100
    {
101
        $validation = FILTER_DEFAULT;
102
        switch ($inMny['VFR']['validation_filter']) {
103
            case "int":
104
                $inVFR                             = $inMny['VFR']['validation_options'];
105
                $validOpts['options']['max_range'] = $this->getValidOption($inMny['value'], $inVFR, 'max_range');
106
                $validOpts['options']['min_range'] = $this->getValidOption($inMny['value'], $inVFR, 'min_range');
107
                $validation                        = FILTER_VALIDATE_INT;
108
                break;
109
            case "float":
110
                $validOpts['options']['decimal']   = $inMny['VFR']['validation_options']['decimals'];
111
                $validation                        = FILTER_VALIDATE_FLOAT;
112
                break;
113
        }
114
        return [
115
            'validation'         => $validation,
116
            'validation_options' => $validOpts,
117
        ];
118
    }
119
120
    private function establishValidValue(\Symfony\Component\HttpFoundation\Request $tCSG, $key, $inMny)
121
    {
122
        $validOpts                       = [];
123
        $validOpts['options']['default'] = $inMny['value'];
124
        if (in_array($key, ['sm', 'sn'])) {
125
            $validOpts['options']['default']                 = $inMny['MW'];
126
            $inMny['VFR']['validation_options']['min_range'] = $inMny['MW'];
127
        }
128
        $vOptions   = $this->establishFilterParameters($inMny, $validOpts);
129
        $validValue = filter_var($tCSG->get($key), $vOptions['validation'], $vOptions['validation_options']);
130
        return $validValue;
131
    }
132
133
    private function getValidOption($value, $inValuesFilterRules, $optionLabel)
134
    {
135
        $valReturn = $inValuesFilterRules[$optionLabel];
136
        if ($valReturn == 'default') {
137
            $valReturn = $value;
138
        }
139
        return $valReturn;
140
    }
141
142
    protected function processFormInputDefaults(\Symfony\Component\HttpFoundation\Request $tCSG, $aMultiple)
143
    {
144
        foreach ($aMultiple['VFR'] as $key => $value) {
145
            $validValue = trim($tCSG->get($key));
146
            if (array_key_exists('validation_options', $value)) {
147
                $validValue = $this->establishValidValue($tCSG, $key, [
148
                    'value'    => $aMultiple['VFR'][$key]['default'],
149
                    'MW'       => $aMultiple['MW'],
150
                    'VFR'      => $aMultiple['VFR'][$key],
151
                    'YM range' => $aMultiple['YM range'],
152
                ]);
153
            }
154
            $tCSG->request->set($key, $validValue);
155
        }
156
    }
157
}
158