ValueBag::fetch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\Bag;
6
7
/**
8
 * Class ValueBag
9
 * @package Pivasic\Bundle\Common\Bag
10
 */
11
class ValueBag
12
{
13
    /**
14
     * @param array $bag
15
     */
16
    public function __construct(array $bag = [])
17
    {
18
        $this->bag = $bag;
19
    }
20
21
    /**
22
     * Returns true if parameter is defined.
23
     *
24
     * @param string $key
25
     * @return bool
26
     */
27
    public function has(string $key): bool
28
    {
29
        return array_key_exists($key, $this->bag);
30
    }
31
32
    /**
33
     * An array of parameter names.
34
     *
35
     * @return array
36
     */
37
    public function keys(): array
38
    {
39
        return array_keys($this->bag);
40
    }
41
42
    /**
43
     * Get value by parameter name.
44
     *
45
     * @param string $key
46
     * @param mixed|null $default The default value if parameter does not exist
47
     * @return mixed|null
48
     */
49
    public function fetch(string $key, $default = null)
50
    {
51
        $val = $this->bag[$key] ?? $default;
52
        if (!is_array($val)) {
53
            return trim($val);
54
        } else {
55
            return $val;
56
        }
57
    }
58
59
    /**
60
     * Get parameter value converted to integer.
61
     *
62
     * @param string $key
63
     * @param int $default The default value if parameter does not exist
64
     * @return int
65
     */
66
    public function fetchInt(string $key, int $default = 0): int
67
    {
68
        return intval($this->fetch($key, $default));
69
    }
70
71
    /**
72
     * Get parameter value converted to float with precision.
73
     *
74
     * @param string $key
75
     * @param float $default The default value if parameter does not exist
76
     * @param int $precision The optional number of decimal digits to round to
77
     * @return float
78
     */
79
    public function fetchFloat(string $key, float $default = 0.0, int $precision = 2): float
80
    {
81
        return round(floatval($this->fetch($key, $default)), $precision);
82
    }
83
84
    /**
85
     * Get parameter value converted to boolean.
86
     *
87
     * @param string $key
88
     * @param bool|false $default The default value if parameter does not exist
89
     * @return mixed
90
     */
91
    public function fetchBool(string $key, bool $default = false): bool
92
    {
93
        return $this->fetchFilter($key, $default, FILTER_VALIDATE_BOOLEAN);
94
    }
95
96
    /**
97
     * Filter value.
98
     *
99
     * @param string $key
100
     * @param mixed|null $default The default value if parameter does not exist
101
     * @param int $filter         FILTER_* constant
102
     * @param array $options      Filter options
103
     * @see http://php.net/manual/en/function.filter-var.php
104
     * @return mixed
105
     */
106
    public function fetchFilter(string $key, $default = null, $filter = FILTER_DEFAULT, $options = [])
107
    {
108
        $value = $this->fetch($key, $default);
109
110
        if (!is_array($options) && $options) {
111
            $options = ['flags' => $options];
112
        }
113
114
        if (is_array($value) && !isset($options['flags'])) {
115
            $options['flags'] = FILTER_REQUIRE_ARRAY;
116
        }
117
118
        return filter_var($value, $filter, $options);
119
    }
120
121
    /**
122
     * Use custom function to process value.
123
     *
124
     * @param string $key
125
     * @param callable $callback
126
     * @param mixed|null $default
127
     * @return mixed
128
     */
129
    public function fetchCustom(string $key, $callback, $default = null)
130
    {
131
        return $callback($this->fetch($key, $default));
132
    }
133
134
    /**
135
     * Use mysql escape function.
136
     *
137
     * @param $key
138
     * @param \mysqli $db
139
     * @param string $default
140
     *
141
     * @return string
142
     */
143
    public function fetchEscape(string $key, \mysqli $db, string $default = ''): string
144
    {
145
        return $db->real_escape_string($this->fetch($key, $default));
146
    }
147
148
    /**
149
     * Returns the count of parameters.
150
     * @return int
151
     */
152
    public function count()
153
    {
154
        return count($this->bag);
155
    }
156
157
    private $bag;
158
}