Login   Register  
PHP Classes
elePHPant
Icontem

File: src/eMacros/Runtime/Comparison/ComparisonPredicate.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/Comparison/ComparisonPredicate.php  >  Download  
File: src/eMacros/Runtime/Comparison/ComparisonPredicate.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: 921 bytes
 

Contents

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

use 
eMacros\Runtime\GenericFunction;

abstract class 
ComparisonPredicate extends GenericFunction {
    protected 
$logicalOr false;
    
    
/**
     * Compares various operands
     * Usage: (== 1 2) (>= 4 3) (=== "1" 1)
     * Returns: boolean
     * (non-PHPdoc)
     * @see \eMacros\Runtime\GenericFunction::execute()
     */
    
public function execute(array $operands) {
        if (empty(
$operands)) {
            throw new \
BadFunctionCallException(sprintf("%s: No objects to compare."get_class($this)));
        }
        
        if (!isset(
$operands[1])) {
            throw new \
BadFunctionCallException(sprintf("%s: Cannot compare single object."get_class($this)));
        }
        
        
$fst array_shift($operands);
        
$or $this->logicalOr;
        
        foreach (
$operands as $val) {
            if (
$or xor !$this->compare($fst$val)) {
                return 
$or;
            }
            
            
$fst $val;
        }
    
        return !
$or;
    }
    
    abstract protected function 
compare($a$b);
}
?>