File: src/eMacros/Runtime/Type/IsInstanceOf.php

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

Contents

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

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

class
IsInstanceOf implements Applicable {
   
/**
     * Determines if a value is an instance of a given class
     * Usage: (instance-of Acme\CustomClass _obj)
     * Returns: boolean
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
       
//check number of parameters
       
if (count($arguments) == 0) {
            throw new \
BadFunctionCallException("IsInstanceOf: No arguments found.");
        }
       
        if (
count($arguments) == 1) {
            throw new \
BadFunctionCallException("IsInstanceOf: No class specified.");
        }
       
       
//obtain instance
       
$instance = $arguments[0]->evaluate($scope);
       
        if (!
is_object($instance)) {
            return
false;
        }
       
        if (!(
$arguments[1] instanceof Symbol)) {
            throw new \
InvalidArgumentException(sprintf("InstanceOf: Expected symbol as second argument but %s was found instead.", substr(strtolower(strstr(get_class($arguments[1]), '\\')), 1)));
        }
       
       
$class = $arguments[1]->symbol;
       
       
$rc = new \ReflectionClass($class);
        return
$rc->isInstance($instance);
    }
}
?>

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