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);
}