Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpFoundation;
13
 
14
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
15
 
16
/**
17
 * ParameterBag is a container for key/value pairs.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 */
21
class ParameterBag implements \IteratorAggregate, \Countable
22
{
23
    /**
24
     * Parameter storage.
25
     */
26
    protected $parameters;
27
 
28
    public function __construct(array $parameters = [])
29
    {
30
        $this->parameters = $parameters;
31
    }
32
 
33
    /**
34
     * Returns the parameters.
35
     *
36
     * @param string|null $key The name of the parameter to return or null to get them all
37
     *
38
     * @return array An array of parameters
39
     */
40
    public function all(/*string $key = null*/)
41
    {
42
        $key = \func_num_args() > 0 ? func_get_arg(0) : null;
43
 
44
        if (null === $key) {
45
            return $this->parameters;
46
        }
47
 
48
        if (!\is_array($value = $this->parameters[$key] ?? [])) {
49
            throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
50
        }
51
 
52
        return $value;
53
    }
54
 
55
    /**
56
     * Returns the parameter keys.
57
     *
58
     * @return array An array of parameter keys
59
     */
60
    public function keys()
61
    {
62
        return array_keys($this->parameters);
63
    }
64
 
65
    /**
66
     * Replaces the current parameters by a new set.
67
     */
68
    public function replace(array $parameters = [])
69
    {
70
        $this->parameters = $parameters;
71
    }
72
 
73
    /**
74
     * Adds parameters.
75
     */
76
    public function add(array $parameters = [])
77
    {
78
        $this->parameters = array_replace($this->parameters, $parameters);
79
    }
80
 
81
    /**
82
     * Returns a parameter by name.
83
     *
84
     * @param mixed $default The default value if the parameter key does not exist
85
     *
86
     * @return mixed
87
     */
88
    public function get(string $key, $default = null)
89
    {
90
        return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
91
    }
92
 
93
    /**
94
     * Sets a parameter by name.
95
     *
96
     * @param mixed $value The value
97
     */
98
    public function set(string $key, $value)
99
    {
100
        $this->parameters[$key] = $value;
101
    }
102
 
103
    /**
104
     * Returns true if the parameter is defined.
105
     *
106
     * @return bool true if the parameter exists, false otherwise
107
     */
108
    public function has(string $key)
109
    {
110
        return \array_key_exists($key, $this->parameters);
111
    }
112
 
113
    /**
114
     * Removes a parameter.
115
     */
116
    public function remove(string $key)
117
    {
118
        unset($this->parameters[$key]);
119
    }
120
 
121
    /**
122
     * Returns the alphabetic characters of the parameter value.
123
     *
124
     * @return string The filtered value
125
     */
126
    public function getAlpha(string $key, string $default = '')
127
    {
128
        return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
129
    }
130
 
131
    /**
132
     * Returns the alphabetic characters and digits of the parameter value.
133
     *
134
     * @return string The filtered value
135
     */
136
    public function getAlnum(string $key, string $default = '')
137
    {
138
        return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
139
    }
140
 
141
    /**
142
     * Returns the digits of the parameter value.
143
     *
144
     * @return string The filtered value
145
     */
146
    public function getDigits(string $key, string $default = '')
147
    {
148
        // we need to remove - and + because they're allowed in the filter
149
        return str_replace(['-', '+'], '', $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT));
150
    }
151
 
152
    /**
153
     * Returns the parameter value converted to integer.
154
     *
155
     * @return int The filtered value
156
     */
157
    public function getInt(string $key, int $default = 0)
158
    {
159
        return (int) $this->get($key, $default);
160
    }
161
 
162
    /**
163
     * Returns the parameter value converted to boolean.
164
     *
165
     * @return bool The filtered value
166
     */
167
    public function getBoolean(string $key, bool $default = false)
168
    {
169
        return $this->filter($key, $default, \FILTER_VALIDATE_BOOLEAN);
170
    }
171
 
172
    /**
173
     * Filter key.
174
     *
175
     * @param mixed $default Default = null
176
     * @param int   $filter  FILTER_* constant
177
     * @param mixed $options Filter options
178
     *
179
     * @see https://php.net/filter-var
180
     *
181
     * @return mixed
182
     */
183
    public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
184
    {
185
        $value = $this->get($key, $default);
186
 
187
        // Always turn $options into an array - this allows filter_var option shortcuts.
188
        if (!\is_array($options) && $options) {
189
            $options = ['flags' => $options];
190
        }
191
 
192
        // Add a convenience check for arrays.
193
        if (\is_array($value) && !isset($options['flags'])) {
194
            $options['flags'] = \FILTER_REQUIRE_ARRAY;
195
        }
196
 
197
        return filter_var($value, $filter, $options);
198
    }
199
 
200
    /**
201
     * Returns an iterator for parameters.
202
     *
203
     * @return \ArrayIterator An \ArrayIterator instance
204
     */
205
    public function getIterator()
206
    {
207
        return new \ArrayIterator($this->parameters);
208
    }
209
 
210
    /**
211
     * Returns the number of parameters.
212
     *
213
     * @return int The number of parameters
214
     */
215
    public function count()
216
    {
217
        return \count($this->parameters);
218
    }
219
}