Login   Register  
PHP Classes
elePHPant
Icontem

File: src/eMacros/Runtime/Collection/Car.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/Collection/Car.php  >  Download  
File: src/eMacros/Runtime/Collection/Car.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,279 bytes
 

Contents

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

use 
eMacros\Runtime\GenericFunction;

class 
Car extends GenericFunction {
    
/**
     * Returns the head element on a list
     * Usage: (Array:car (array 1 2 3))
     * Returns: mixed
     * (non-PHPdoc)
     * @see \eMacros\Runtime\GenericFunction::execute()
     */
    
public function execute(array $arguments) {
        if (empty(
$arguments)) {
            throw new \
BadFunctionCallException("Car: No parameters found.");
        }
        
        list(
$list) = $arguments;
        
        if (
$list instanceof \Iterator) {
            
$list->rewind();
            
$value $list->valid() ? $list->current() : null;
        }
        elseif (
$list instanceof \IteratorAggregate) {
            
$iter $list->getIterator();
            
$iter->rewind();
            
$value $iter->valid() ? $iter->current() : null;
        }
        elseif (
is_array($list)) {
            
$value = !empty($list) ? array_shift($list) : null;
        }
        elseif (
$list instanceof \ArrayAccess) {
            
//cannot ensure which is the first element, return index 0
            
$value = isset($list[0]) ? $list[0] : null;
        }
        else {
            throw new \
InvalidArgumentException('Car: Parameter is not a list.');
        }
        
        return 
$value;
    }
}