luya\helpers\ObjectHelper::callMethodSanitizeArguments PHP Method

callMethodSanitizeArguments() public static method

Call a class method with arguments and verify the arguments if they are in the list of method arguments or not. php ObjectHelper::callMethodSanitizeArguments(new MyClass(), 'methodToCall', ['paramName' => 'paramValue']);
public static callMethodSanitizeArguments ( object $object, string $method, array $argumentsList = [] ) : object
$object object The class object where the method must be found.
$method string The class method to call inside the object.
$argumentsList array 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.
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);
    }

Usage Example

Example #1
0
 public function actionIndex($callback, $id)
 {
     $model = NavItemPageBlockItem::findOne($id);
     $block = Block::objectId($model->block_id, $model->id, 'callback');
     $method = 'callback' . Inflector::id2camel($callback);
     return ObjectHelper::callMethodSanitizeArguments($block, $method, Yii::$app->request->get());
 }
All Usage Examples Of luya\helpers\ObjectHelper::callMethodSanitizeArguments