public function ParameterBag::get

Same name in this branch

Returns a parameter by name.

@api

Parameters

string $path The key:

mixed $default The default value if the parameter key does not exist:

boolean $deep If true, a path like foo[bar] will find deeper items:

Return value

mixed

4 calls to ParameterBag::get()
ParameterBag::filter in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ParameterBag.php
Filter key.
ParameterBag::getAlnum in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ParameterBag.php
Returns the alphabetic characters and digits of the parameter value.
ParameterBag::getAlpha in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ParameterBag.php
Returns the alphabetic characters of the parameter value.
ParameterBag::getInt in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ParameterBag.php
Returns the parameter value converted to integer.

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ParameterBag.php, line 101

Class

ParameterBag
ParameterBag is a container for key/value pairs.

Namespace

Symfony\Component\HttpFoundation

Code

public function get($path, $default = null, $deep = false) {
  if (!$deep || false === ($pos = strpos($path, '['))) {
    return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
  }
  $root = substr($path, 0, $pos);
  if (!array_key_exists($root, $this->parameters)) {
    return $default;
  }
  $value = $this->parameters[$root];
  $currentKey = null;
  for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
    $char = $path[$i];
    if ('[' === $char) {
      if (null !== $currentKey) {
        throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
      }
      $currentKey = '';
    }
    elseif (']' === $char) {
      if (null === $currentKey) {
        throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
      }
      if (!is_array($value) || !array_key_exists($currentKey, $value)) {
        return $default;
      }
      $value = $value[$currentKey];
      $currentKey = null;
    }
    else {
      if (null === $currentKey) {
        throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
      }
      $currentKey .= $char;
    }
  }
  if (null !== $currentKey) {
    throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
  }
  return $value;
}