Completed
Push — master ( d9f7fe...0d6725 )
by Igor
02:53
created

ValueBag::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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