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