Ejemplo n.º 1
0
        if ($self->hasOwnProperty($i)) {
            $result = $fn->call($context, $self->get($i), (double) $i, $self);
            $results->set($i, $result);
        }
    }
    return $results;
}, 'filter' => function ($fn, $context = null) {
    $self = Func::getContext();
    $results = new Arr();
    $len = $self->length;
    for ($i = 0; $i < $len; $i++) {
        if ($self->hasOwnProperty($i)) {
            $item = $self->get($i);
            $result = $fn->call($context, $item, (double) $i, $self);
            if (is($result)) {
                $results->push($item);
            }
        }
    }
    return $results;
}, 'sort' => function ($fn = null) {
    $self = Func::getContext();
    if ($fn instanceof Func) {
        $results = $self->toArray();
        $comparator = function ($a, $b) use(&$fn) {
            return $fn->call(null, $a, $b);
        };
        uasort($results, $comparator);
    } else {
        $results = array();
        $len = $self->length;
Ejemplo n.º 2
0
    $self = Func::getContext();
    $str = $self->value;
    if (!$regex instanceof RegExp) {
        $regex = $RegExp->construct($regex);
    }
    if (!$regex->globalFlag) {
        return $regex->callMethod('exec', $str);
    }
    $results = new Arr();
    $index = 0;
    $preg = $regex->toString(true);
    while (preg_match($preg, $str, $matches, PREG_OFFSET_CAPTURE, $index) === 1) {
        $foundAt = $matches[0][1];
        $foundStr = $matches[0][0];
        $index = $foundAt + strlen($foundStr);
        $results->push($foundStr);
    }
    return $results;
}, 'replace' => function ($search, $replace) {
    $self = Func::getContext();
    $str = $self->value;
    $isRegEx = $search instanceof RegExp;
    $limit = $isRegEx && $search->globalFlag ? -1 : 1;
    $search = $isRegEx ? $search->toString(true) : to_string($search);
    if ($replace instanceof Func) {
        if ($isRegEx) {
            $count = 0;
            $offset = 0;
            $result = array();
            $success = null;
            while (($limit === -1 || $count < $limit) && ($success = preg_match($search, $str, $matches, PREG_OFFSET_CAPTURE, $offset))) {
Ejemplo n.º 3
0
RegExp::$protoMethods = array('exec' => function ($str) {
    $self = Func::getContext();
    $str = to_string($str);
    //todo $offset
    $offset = 0;
    $result = preg_match($self->toString(true), $str, $matches, PREG_OFFSET_CAPTURE, $offset);
    if ($result === false) {
        throw new Ex(Error::create('Error executing Regular Expression: ' . $self->toString()));
    }
    if ($result === 0) {
        return Object::$null;
    }
    $index = $matches[0][1];
    $self->set('lastIndex', (double) ($index + strlen($matches[0][0])));
    $arr = new Arr();
    foreach ($matches as $match) {
        $arr->push($match[0]);
    }
    $arr->set('index', (double) $index);
    $arr->set('input', $str);
    return $arr;
}, 'test' => function ($str) {
    $self = Func::getContext();
    $result = preg_match($self->toString(true), to_string($str));
    return $result !== false;
}, 'toString' => function () {
    $self = Func::getContext();
    return $self->toString(false);
});
RegExp::$protoObject = new Object();
RegExp::$protoObject->setMethods(RegExp::$protoMethods, true, false, true);
Ejemplo n.º 4
0
Archivo: fs.php Proyecto: mk-pmb/js2php
     $result->set('dateLastAccessed', new Date($stat['atime'] * 1000));
     $result->set('dateLastModified', new Date($stat['mtime'] * 1000));
     $result->set('type', $isDir ? 'directory' : 'file');
     if (!$isDir) {
         $result->set('size', (double) $stat['size']);
     } else {
         if ($deep) {
             $size = 0.0;
             $children = new Arr();
             foreach (scandir($fullPath) as $item) {
                 if ($item === '.' || $item === '..') {
                     continue;
                 }
                 $child = $helpers['getInfo']($fullPath . DIRECTORY_SEPARATOR . $item, $deep);
                 $size += $child->get('size');
                 $children->push($child);
             }
             $result->set('children', $children);
             $result->set('size', $size);
         } else {
             $result->set('size', 0.0);
         }
     }
     return $result;
 }, 'deleteFile' => function ($fullPath) use(&$helpers) {
     try {
         $result = unlink($fullPath);
     } catch (Exception $e) {
         $helpers['handleException']($e, $fullPath);
     }
     //fallback for if set_error_handler didn't do it's thing
Ejemplo n.º 5
0
$JSON = call_user_func(function () {
    $decode = function ($value) use(&$decode) {
        if ($value === null) {
            return Object::$null;
        }
        $type = gettype($value);
        if ($type === 'integer') {
            return (double) $value;
        }
        if ($type === 'string' || $type === 'boolean' || $type === 'double') {
            return $value;
        }
        if ($type === 'array') {
            $result = new Arr();
            foreach ($value as $item) {
                $result->push($decode($item));
            }
        } else {
            $result = new Object();
            foreach ($value as $key => $item) {
                if ($key === '_empty_') {
                    $key = '';
                }
                $result->set($key, $decode($item));
            }
        }
        return $result;
    };
    $escape = function ($str) {
        return str_replace("\\/", "/", json_encode($str));
    };