File: src/eMacros/Runtime/Builder/InstanceBuilder.php

Recommend this page to a friend!
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Runtime/Builder/InstanceBuilder.php  >  Download  
File: src/eMacros/Runtime/Builder/InstanceBuilder.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 7 years ago
Size: 1,063 bytes
 

Contents

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

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

class
InstanceBuilder implements Applicable {
   
/**
     * Creates a new instance from a string
     * Usage: (instance _class (array 1 2 3))
     * Returns: object
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
        if (
count($arguments) == 0) {
            throw new \
BadFunctionCallException("InstanceBuilder: No arguments found.");
        }
       
       
$class = $arguments[0]->evaluate($scope);
       
        if (!
is_string($class)) {
            throw new \
InvalidArgumentException(sprintf("InstanceBuilder: Expected string as first argument but %s was found instead.", gettype($class)));
        }
       
       
//get additional arguments
       
$list = array_slice($arguments->getArrayCopy(), 1);
       
$args = array();
       
       
//build constructor parameters
       
foreach ($list as $el) {
           
$args[] = $el->evaluate($scope);
        }
       
       
$rc = new \ReflectionClass($class);
        return empty(
$args) ? $rc->newInstance() : $rc->newInstanceArgs($args);
    }
}
?>

For more information send a message to info at phpclasses dot org.