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