Skip to content

Instantly share code, notes, and snippets.

@nadar
Last active November 12, 2015 11:36
Show Gist options
  • Save nadar/297afcb96dbd2d296842 to your computer and use it in GitHub Desktop.
Save nadar/297afcb96dbd2d296842 to your computer and use it in GitHub Desktop.
Call class/object method and sanitize massive assigned arguments in PHP
<?php
/**
* Call a class method with arguments and verify the arguments if they are in the list of
* method arguments or not.
*
* ```
* $response = ObjectHelper::callMethodSanitizeArguments((new MyClass()), 'methodToGet', ['paramName' => 'paramValue']);
* ```
*
* @param object $object The class object where the method must be found.
* @param string $method The class method to call inside the object
* @param array $argumentsList A massiv assigned list of array items, where the key is bind
* to the method argument and the value to be passed in the method on call.
*
* @throws Exception Throws an exception if a argument coult not be found.
*
* @return object
*/
public static function callMethodSanitizeArguments($object, $method, array $argumentsList = [])
{
// get class reflection object
$reflection = new ReflectionMethod($object, $method);
// array where the sanitized arguemnts will be stored
$methodArgs = [];
foreach ($reflection->getParameters() as $param) {
// add the argument into the method list when existing
if (array_key_exists($param->name, $argumentsList)) {
$methodArgs[] = $argumentsList[$param->name];
}
// check if the provided arguemnt is optional or not
if (!$param->isOptional() && !array_key_exists($param->name, $argumentsList)) {
throw new Exception(sprintf("The argument '%s' is required for method '%s' in class '%s'.", $param->name, $method, get_class($object)));
}
}
return call_user_func_array([$object, $method], $methodArgs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment