Login   Register  
PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  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: 2014-01-13 05:23
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 $scopeGenericList $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;
    }
}
?>