Login   Register  
PHP Classes
elePHPant
Icontem

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

Contents

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

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

class 
ArrayShift implements Applicable {
    
/**
     * Shifts the first value of an array off and returns it
     * Usage: (Array::shift _arr)
     * Returns: mixed
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
    
public function apply(Scope $scopeGenericList $arguments) {
        if (
count($arguments) == 0) {
            throw new \
BadFunctionCallException();
        }
        
        
$target $arguments[0];
        
        if (!(
$target instanceof Symbol)) {
            throw new \
InvalidArgumentException(sprintf("ArrayShift: Expected symbol as first argument but %s was found instead."substr(strtolower(strstr(get_class($arguments[0]), '\\')), 1)));
        }
        
        
$ref $target->symbol;
        
        if (
is_array($scope->symbols[$ref])) {
            return 
array_shift($scope->symbols[$ref]);
        }
        elseif (
$scope->symbols[$ref] instanceof \ArrayObject) {
            
$arr $scope->symbols[$ref]->getArrayCopy();
            
$value array_shift($arr);
            
            if (
count($scope->symbols[$ref]->getArrayCopy()) != 0) {
                
$scope->symbols[$ref]->exchangeArray($arr);
            }
            
            return 
$value;
        }
        
        return 
null;
    }
}
?>