Elgg  Version 1.10
ParameterBag.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Http;
3 
36 class ParameterBag implements \IteratorAggregate, \Countable {
37 
43  protected $parameters;
44 
50  public function __construct(array $parameters = array()) {
51  $this->parameters = $parameters;
52  }
53 
59  public function all() {
60  return $this->parameters;
61  }
62 
68  public function keys() {
69  return array_keys($this->parameters);
70  }
71 
78  public function replace(array $parameters = array()) {
79  $this->parameters = $parameters;
80  }
81 
88  public function add(array $parameters = array()) {
89  // original uses array_replace. using array_merge for 5.2 BC
90  $this->parameters = array_merge($this->parameters, $parameters);
91  }
92 
104  public function get($path, $default = null, $deep = false) {
105  if (!$deep || false === $pos = strpos($path, '[')) {
106  return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
107  }
108 
109  $root = substr($path, 0, $pos);
110  if (!array_key_exists($root, $this->parameters)) {
111  return $default;
112  }
113 
114  $value = $this->parameters[$root];
115  $currentKey = null;
116  for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
117  $char = $path[$i];
118 
119  if ('[' === $char) {
120  if (null !== $currentKey) {
121  throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
122  }
123 
124  $currentKey = '';
125  } elseif (']' === $char) {
126  if (null === $currentKey) {
127  throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
128  }
129 
130  if (!is_array($value) || !array_key_exists($currentKey, $value)) {
131  return $default;
132  }
133 
134  $value = $value[$currentKey];
135  $currentKey = null;
136  } else {
137  if (null === $currentKey) {
138  throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
139  }
140 
141  $currentKey .= $char;
142  }
143  }
144 
145  if (null !== $currentKey) {
146  throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
147  }
148 
149  return $value;
150  }
151 
159  public function set($key, $value) {
160  $this->parameters[$key] = $value;
161  }
162 
170  public function has($key) {
171  return array_key_exists($key, $this->parameters);
172  }
173 
180  public function remove($key) {
181  unset($this->parameters[$key]);
182  }
183 
193  public function getAlpha($key, $default = '', $deep = false) {
194  return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
195  }
196 
206  public function getAlnum($key, $default = '', $deep = false) {
207  return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
208  }
209 
219  public function getDigits($key, $default = '', $deep = false) {
220  // we need to remove - and + because they're allowed in the filter
221  return str_replace(array('-', '+'), '', $this->filter($key, $default, $deep, FILTER_SANITIZE_NUMBER_INT));
222  }
223 
233  public function getInt($key, $default = 0, $deep = false) {
234  return (int) $this->get($key, $default, $deep);
235  }
236 
250  public function filter($key, $default = null, $deep = false, $filter = FILTER_DEFAULT, $options = array()) {
251  $value = $this->get($key, $default, $deep);
252 
253  // Always turn $options into an array - this allows filter_var option shortcuts.
254  if (!is_array($options) && $options) {
255  $options = array('flags' => $options);
256  }
257 
258  // Add a convenience check for arrays.
259  if (is_array($value) && !isset($options['flags'])) {
260  $options['flags'] = FILTER_REQUIRE_ARRAY;
261  }
262 
263  return filter_var($value, $filter, $options);
264  }
265 
271  public function getIterator() {
272  return new \ArrayIterator($this->parameters);
273  }
274 
280  public function count() {
281  return count($this->parameters);
282  }
283 
284 }
285 
__construct(array $parameters=array())
Constructor.
getIterator()
Returns an iterator for parameters.
$value
Definition: longtext.php:29
replace(array $parameters=array())
Replaces the current parameters by a new set.
$default
Definition: checkbox.php:36
getDigits($key, $default= '', $deep=false)
Returns the digits of the parameter value.
keys()
Returns the parameter keys.
filter($key, $default=null, $deep=false, $filter=FILTER_DEFAULT, $options=array())
Filter key.
$options
Definition: index.php:14
has($key)
Returns true if the parameter is defined.
getAlpha($key, $default= '', $deep=false)
Returns the alphabetic characters of the parameter value.
$key
Definition: summary.php:34
if(!$vars['title']&&$vars['title']!==false) if(isset($vars['filter_override'])) if(!isset($vars['filter'])&&elgg_is_logged_in()&&$context) $filter
Definition: content.php:62
add(array $parameters=array())
Adds parameters.
getAlnum($key, $default= '', $deep=false)
Returns the alphabetic characters and digits of the parameter value.
all()
Returns the parameters.
count()
Returns the number of parameters.
getInt($key, $default=0, $deep=false)
Returns the parameter value converted to integer.
$path
Definition: invalid.php:17