File: src/eMacros/Runtime/String/StringScan.php

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

Contents

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

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

class
StringScan implements Applicable {
   
/**
     * Interprets a string with a given format
     * Usage: (sscanf "SN/2350001", "SN/%d") (sscanf "24\tLewis Carroll" "%d\t%s %s" _id _first _last)
     * Returns: array | int
     * (non-PHPdoc)
     * @see \eMacros\Applicable::apply()
     */
   
public function apply(Scope $scope, GenericList $arguments) {
       
$nargs = count($arguments);
       
        if (
$nargs == 0) {
            throw new \
BadFunctionCallException("StringScan: No parameters found.");
        }
        elseif (
$nargs == 1) {
            throw new \
BadFunctionCallException("StringScan: No format specified.");
        }
       
       
$str = $arguments[0]->evaluate($scope);
       
$format = $arguments[1]->evaluate($scope);
       
        if (
$nargs > 2) {
           
$arr = sscanf($str, $format);
           
            for (
$i = 0, $n = count($arr); $i < $n && $i < $nargs - 2; $i++) {
               
$target = $arguments[$i + 2];
               
                if (!(
$target instanceof Symbol)) {
                    throw new \
InvalidArgumentException(sprintf("StringScan: Unexpected %s found as additional parameter.", substr(strtolower(strstr(get_class($arguments[0]), '\\')), 1)));
                }
               
               
$ref = $target->symbol;
               
$scope->symbols[$ref] = $arr[$i];
            }
           
            return
count($arr);
        }
        else {
            return
sscanf($str, $format);
        }
    }
}
?>

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