PHP Classes

File: src/eMacros/Runtime/Regex/RegexReplaceCallback.php

Recommend this page to a friend!
  Classes of Emmanuel Antico   eMacros   src/eMacros/Runtime/Regex/RegexReplaceCallback.php   Download  
File: src/eMacros/Runtime/Regex/RegexReplaceCallback.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 10 years ago
Size: 1,427 bytes
 

Contents

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

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

class
RegexReplaceCallback implements Applicable {
   
/**
     * Replaces all matches specified by a pattern on a given subject
     * Usage: see RegexPackageTest class
     * Returns: array | string
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
       
$nargs = count($arguments);
       
        if (
$nargs < 3) {
            throw new \
BadFunctionCallException("RegexReplaceCallback: Function expects at least 3 parameters.");
        }
       
       
$args = array();
       
        for (
$i = 0; $i < $nargs; $i++) {
            if (
$i == 4) break;
           
$args[] = $arguments[$i]->evaluate($scope);
        }
       
        if (!
is_callable($args[1])) {
            throw new \
InvalidArgumentException("RegexReplaceCallback: Second parameter is not a valid callback.");
        }
       
        if (
$nargs < 4) {
            return
call_user_func_array('preg_replace_callback', $args);
        }
        else {
           
$target = $arguments[4];
               
            if (!(
$target instanceof Symbol)) {
                throw new \
InvalidArgumentException(sprintf("RegexReplaceCallback: Expected symbol as fourth parameter but %s was found instead.", substr(strtolower(strstr(get_class($arguments[4]), '\\')), 1)));
            }
               
           
$ref = $target->symbol;
               
           
$result = preg_replace_callback($args[0], $args[1], $args[2], $args[3], $count);
           
$scope->symbols[$ref] = $count;
            return
$result;
        }
    }
}
?>