VGTypes::integer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * VGetter types
4
 * User: moyo
5
 * Date: 2018/5/31
6
 * Time: 2:38 PM
7
 */
8
9
namespace Carno\Web\Chips\Controller;
10
11
trait VGTypes
12
{
13
    /**
14
     * @param string $name
15
     * @param string $default
16
     * @return string
17
     */
18
    public function string(string $name, string $default = '') : string
19
    {
20
        return (string) ($this->$name ?? $default);
21
    }
22
23
    /**
24
     * @param string $name
25
     * @param int $default
26
     * @return int
27
     */
28
    public function integer(string $name, int $default = 0) : int
29
    {
30
        return (int) ($this->$name ?? $default);
31
    }
32
33
    /**
34
     * @param string $name
35
     * @param bool $default
36
     * @return bool
37
     */
38
    public function boolean(string $name, bool $default = false) : bool
39
    {
40
        if (is_string($got = $this->$name)) {
41
            return filter_var($got, FILTER_VALIDATE_BOOLEAN);
42
        } else {
43
            return is_null($got) ? $default : (bool) $got;
44
        }
45
    }
46
}
47