1 | <?php |
||
11 | class ValueBag |
||
12 | { |
||
13 | /** |
||
14 | * @param array $bag |
||
15 | */ |
||
16 | public function __construct(array $bag = []) |
||
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 |
||
31 | |||
32 | /** |
||
33 | * An array of parameter names. |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | public function keys(): array |
||
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) |
||
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 |
||
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 |
||
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 |
||
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 = []) |
||
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) |
||
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 |
||
147 | |||
148 | /** |
||
149 | * Returns the count of parameters. |
||
150 | * @return int |
||
151 | */ |
||
152 | public function count() |
||
156 | |||
157 | private $bag; |
||
158 | } |