Created
September 13, 2011 15:21
-
-
Save djpate/1214090 to your computer and use it in GitHub Desktop.
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
/** | |
* Cette fonction valide que les arguments necessaire a l'execution d'une action | |
* sur un controllert sont présent et qu'il respecte le typage fournie par le docblock | |
* de l'action | |
* @param string $method | |
* @param array $arguments | |
* @throws \BadMethodCallException | |
* @throws \InvalidArgumentException | |
*/ | |
public function checkArguments($method, $arguments){ | |
$class = new \ReflectionClass(get_called_class()); | |
$method = $class->getMethod($method); | |
// creation d'un array avec les parametres optionnels | |
$optional_parameters = array(); | |
$parameters = $method->getParameters(); | |
foreach($parameters as $parameter){ | |
if($parameter->isOptional()){ | |
$optional_parameters[] = $parameter->getName(); | |
} | |
} | |
$doc = $method->getDocComment(); | |
$lines = explode("\n",$doc); | |
foreach($lines as $line){ | |
if(preg_match('^@param^',$line)){ | |
$info = explode(" ",$line); | |
$type = $info[3]; | |
$name = trim(str_replace("$","",$info[4])); | |
if( !array_key_exists($name, $arguments) && !in_array($name,$optional_parameters) ){ | |
throw new \BadMethodCallException("Argument ".$name." was not set"); | |
} | |
if( array_key_exists($name, $arguments) && !Type::$type($arguments[$name]) ){ | |
throw new \InvalidArgumentException($arguments[$name]." does not validate as ".$type); | |
} | |
} | |
} | |
// si on arrive ici c'est que tout est bon | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment