PHP Classes

File: src/eMacros/Runtime/Callback/CallFunction.php

Recommend this page to a friend!
  Classes of Emmanuel Antico   eMacros   src/eMacros/Runtime/Callback/CallFunction.php   Download  
File: src/eMacros/Runtime/Callback/CallFunction.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 10 years ago
Size: 985 bytes
 

Contents

Class file image Download
<?php
namespace eMacros\Runtime\Callback;

use
eMacros\Applicable;
use
eMacros\Scope;
use
eMacros\GenericList;

class
CallFunction implements Applicable {
    public function
apply(Scope $scope, GenericList $arguments) {
        if (
count($arguments) == 0) {
            throw new \
BadFunctionCallException("CallFunction: No parameters found.");
        }
       
       
$callback = $arguments[0]->evaluate($scope);
       
       
//check for valid callback
       
if (!is_callable($callback)) {
            throw new \
InvalidArgumentException("CallFunction: Argument is not a valid callback.");
        }
       
        if (
$callback instanceof Applicable) {
           
$args = $arguments->shift();
           
            if (
is_null($args)) {
               
$args = new GenericList();
            }
           
            return
$callback->apply($scope, $args);
        }
       
       
//extract parameters
       
$args = array_slice($arguments->getArrayCopy(), 1);
       
$param_array = array();
       
        foreach (
$args as $arg) {
           
$param_array[] = $arg->evaluate($scope);
        }
       
        return
call_user_func_array($callback, $param_array);
    }
}
?>