Last active
June 19, 2019 13:34
-
-
Save ahmedali5530/902a315b2027dcb026ae129262473a02 to your computer and use it in GitHub Desktop.
Recursive get() for symfony, ParameterBag modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a parameter by name, or use : to search in arrays($request->get('form:somefield:anotherfield')) | |
* $_POST[form][somefield][anotherfield] | |
* | |
* @param string $key The key | |
* @param mixed $default The default value if the parameter key does not exist | |
* | |
* @return mixed | |
*/ | |
public function get($key, $default = null) | |
{ | |
if(array_key_exists($key, $this->parameters)){ | |
return $this->parameters[$key]; | |
} | |
if(strstr($key, ':')){ | |
$parameters = $this->parameters; | |
foreach(explode(':', $key) as $segment){ | |
if(array_key_exists($segment, $parameters)){ | |
$parameters = $parameters[$segment]; | |
}else{ | |
return $default; | |
} | |
} | |
return $parameters; | |
} | |
return $default; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment