Completed
Push — master ( ba7d13...90f2d9 )
by Daniel
01:57
created

InputValidation::establishValidValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 9
nc 2
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
        $intValue   = 0;
86
        $maxCounter = count($inMny['EMW']);
87
        for ($counter = 0; $counter < $maxCounter; $counter++) {
88
            $crtVal         = $inMny['EMW'][$counter];
89
            echo '<hr/>';
90
            print_r($inMny['EMW'][$counter]);
91
            echo '<hr/>';
92
            $crtDV          = \DateTime::createFromFormat('Y-n-j', $crtVal['Year'] . '-' . $crtVal['Month'] . '-1');
93
            $crtDateOfValue = (int) $crtDV->format('Ymd');
94
            if (($lngDate <= $inMny['YM range']['maximumInt']) && ($lngDate >= $crtDateOfValue)) {
95
                $intValue = $crtVal['Value'];
96
                $counter  = $maxCounter;
97
            }
98
        }
99
        return $intValue;
100
    }
101
102
    private function establishFilterParameters($inMny, $validOpts)
103
    {
104
        $validation = FILTER_DEFAULT;
105
        switch ($inMny['VFR']['validation_filter']) {
106
            case "int":
107
                $inVFR                             = $inMny['VFR']['validation_options'];
108
                $validOpts['options']['max_range'] = $this->getValidOption($inMny['value'], $inVFR, 'max_range');
109
                $validOpts['options']['min_range'] = $this->getValidOption($inMny['value'], $inVFR, 'min_range');
110
                $validation                        = FILTER_VALIDATE_INT;
111
                break;
112
            case "float":
113
                $validOpts['options']['decimal']   = $inMny['VFR']['validation_options']['decimals'];
114
                $validation                        = FILTER_VALIDATE_FLOAT;
115
                break;
116
        }
117
        return [
118
            'validation'         => $validation,
119
            'validation_options' => $validOpts,
120
        ];
121
    }
122
123
    private function establishValidValue(\Symfony\Component\HttpFoundation\Request $tCSG, $key, $inMny)
124
    {
125
        $validOpts                       = [];
126
        $validOpts['options']['default'] = $inMny['value'];
127
        if (in_array($key, ['sm', 'sn'])) {
128
            $validOpts['options']['default']                 = $inMny['MW'];
129
            $inMny['VFR']['validation_options']['min_range'] = $inMny['MW'];
130
        }
131
        $vOptions   = $this->establishFilterParameters($inMny, $validOpts);
132
        $validValue = filter_var($tCSG->get($key), $vOptions['validation'], $vOptions['validation_options']);
133
        return $validValue;
134
    }
135
136
    private function getValidOption($value, $inValuesFilterRules, $optionLabel)
137
    {
138
        $valReturn = $inValuesFilterRules[$optionLabel];
139
        if ($valReturn == 'default') {
140
            $valReturn = $value;
141
        }
142
        return $valReturn;
143
    }
144
145
    protected function processFormInputDefaults(\Symfony\Component\HttpFoundation\Request $tCSG, $aMultiple)
146
    {
147
        foreach ($aMultiple['VFR'] as $key => $value) {
148
            $validValue = trim($tCSG->get($key));
149
            if (array_key_exists('validation_options', $value)) {
150
                $validValue = $this->establishValidValue($tCSG, $key, [
151
                    'value'    => $aMultiple['VFR'][$key]['default'],
152
                    'MW'       => $aMultiple['MW'],
153
                    'VFR'      => $aMultiple['VFR'][$key],
154
                    'YM range' => $aMultiple['YM range'],
155
                ]);
156
            }
157
            $tCSG->request->set($key, $validValue);
158
        }
159
    }
160
}
161