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

Recommend this page to a friend!
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Runtime/Builder/ArrayBuilder.php  >  Download  
File: src/eMacros/Runtime/Builder/ArrayBuilder.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,001 bytes
 

Contents

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

use
eMacros\Runtime\GenericFunction;
use
eMacros\Applicable;
use
eMacros\GenericList;
use
eMacros\Scope;

class
ArrayBuilder implements Applicable {
   
/**
     * Builds an array with the specified elements
     * Usage: (array 1 null "Hello" (-7 56.25) ("key" "val"))
     * Returns: array
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
       
$values = array();
       
        foreach (
$arguments as $arg) {
            if (
$arg instanceof GenericList) {
                if (
count($arg) < 1) {
                    throw new \
InvalidArgumentException("ArrayBuilder: No key defined.");
                }
               
                if (
count($arg) < 2) {
                    throw new \
InvalidArgumentException("ArrayBuilder: No value defined.");
                }
               
               
//obtain symbol pair
               
list($key, $value) = $arg;
               
               
$key = $key->evaluate($scope);
               
$values[$key] = $value->evaluate($scope);
            }
            else {
               
$values[] = $arg->evaluate($scope);
            }
        }
   
        return
$values;
    }
}
?>

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