Passed
Push — master ( cf47da...53c71b )
by James
21:48 queued 09:49
created

Request::dateTime()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 28
rs 9.4222
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
/**
3
 * Request.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Http\Requests;
24
25
use Carbon\Carbon;
26
use Carbon\Exceptions\InvalidDateException;
27
use Illuminate\Foundation\Http\FormRequest;
28
use Log;
29
30
/**
31
 * Class Request.
32
 *
33
 * @codeCoverageIgnore
34
 *
35
 * @SuppressWarnings(PHPMD.NumberOfChildren)
36
 */
37
class Request extends FormRequest
38
{
39
    /**
40
     * Return a boolean value.
41
     *
42
     * @param string $field
43
     *
44
     * @return bool
45
     */
46
    public function boolean(string $field): bool
47
    {
48
        if ('true' === (string)$this->input($field)) {
49
            return true;
50
        }
51
        if ('false' === (string)$this->input($field)) {
52
            return false;
53
        }
54
55
        return 1 === (int)$this->input($field);
56
    }
57
58
    /**
59
     * @param string $value
60
     *
61
     * @return bool
62
     */
63
    public function convertBoolean(string $value): bool
64
    {
65
        if ('true' === $value) {
66
            return true;
67
        }
68
        if (1 === $value) {
0 ignored issues
show
introduced by James Cole
The condition 1 === $value is always false.
Loading history...
69
            return true;
70
        }
71
        if ('1' === $value) {
72
            return true;
73
        }
74
        if (true === $value) {
0 ignored issues
show
introduced by James Cole
The condition true === $value is always false.
Loading history...
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Return floating value.
83
     *
84
     * @param string $field
85
     *
86
     * @return float|null
87
     */
88
    public function float(string $field): ?float
89
    {
90
        $res = $this->get($field);
91
        if (null === $res) {
92
            return null;
93
        }
94
95
        return (float)$res;
96
    }
97
98
    /**
99
     * Return integer value.
100
     *
101
     * @param string $field
102
     *
103
     * @return int
104
     */
105
    public function integer(string $field): int
106
    {
107
        return (int)$this->get($field);
108
    }
109
110
    /**
111
     * Return string value.
112
     *
113
     * @param string $field
114
     *
115
     * @return string
116
     *
117
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
118
     */
119
    public function string(string $field): string
120
    {
121
        return app('steam')->cleanString((string)($this->get($field) ?? ''));
0 ignored issues
show
Bug introduced by James Cole
The method cleanString() does not exist on FireflyIII\Support\Facades\Steam. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        return app('steam')->/** @scrutinizer ignore-call */ cleanString((string)($this->get($field) ?? ''));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
    }
123
124
    /**
125
     * Return date or NULL.
126
     *
127
     * @param string $field
128
     *
129
     * @return Carbon|null
130
     */
131
    protected function date(string $field): ?Carbon
132
    {
133
        return $this->get($field) ? new Carbon($this->get($field)) : null;
134
    }
135
136
    /**
137
     * Return date time or NULL.
138
     *
139
     * @param string $field
140
     *
141
     * @return Carbon|null
142
     */
143
    protected function dateTime(string $field): ?Carbon
144
    {
145
        if (null === $this->get($field)) {
146
            return null;
147
        }
148
        $value = (string)$this->get($field);
149
        if (10 === \strlen($value)) {
150
            // probably a date format.
151
            try {
152
                $result = Carbon::createFromFormat('Y-m-d', $value);
153
            } catch (InvalidDateException $e) {
154
                Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
155
156
                return null;
157
            }
158
159
            return $result;
160
        }
161
        // is an atom string, I hope?
162
        try {
163
            $result = Carbon::parse($value);
164
        } catch (InvalidDateException $e) {
165
            Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
166
167
            return null;
168
        }
169
170
        return $result;
171
    }
172
173
}
174