File: src/eMacros/Runtime/Collection/ArrayShuffle.php

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

Contents

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

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

class
ArrayShuffle implements Applicable {
   
/**
     * Randomizes the order of the elements in an array
     * Usage: (Array::shuffle _arr)
     * Returns: boolean
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
        if (
count($arguments) == 0) {
            throw new \
BadFunctionCallException("ArrayShuffle: No target specified.");
        }
       
       
$target = $arguments[0];
       
        if (!(
$target instanceof Symbol)) {
            throw new \
InvalidArgumentException(sprintf("ArrayShuffle: Expected symbol as first argument but %s was found instead.", substr(strtolower(strstr(get_class($arguments[0]), '\\')), 1)));
        }
       
       
$ref = $target->symbol;
       
        if (
$scope->symbols[$ref] instanceof \ArrayObject) {
           
$arr = $scope->symbols[$ref]->getArrayCopy();
           
$value = shuffle($arr);
           
$scope->symbols[$ref]->exchangeArray($arr);
            return
$value;
        }
       
        return
shuffle($scope->symbols[$ref]);
    }
}
?>

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