Search in sources :

Example 61 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class FsUtils method hasExt.

@Signature
public static boolean hasExt(Environment env, String path, Memory extensions, boolean ignoreCase) {
    Set<String> exts = new HashSet<>();
    if (extensions.isTraversable()) {
        ForeachIterator iterator = extensions.getNewIterator(env);
        while (iterator.next()) {
            String value = iterator.getValue().toString();
            if (ignoreCase) {
                value = value.toLowerCase();
            }
            exts.add(value);
        }
    } else {
        exts.add(ignoreCase ? extensions.toString().toLowerCase() : extensions.toString());
    }
    String ext = ext(path);
    if (ignoreCase && ext != null) {
        ext = ext.toLowerCase();
    }
    return exts.contains(ext);
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) HashSet(java.util.HashSet) Signature(php.runtime.annotation.Reflection.Signature)

Example 62 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ItemsUtils method combine.

@Signature({ @Arg(value = "keys", type = HintType.TRAVERSABLE), @Arg(value = "values", type = HintType.TRAVERSABLE) })
public static Memory combine(Environment env, Memory... args) {
    ForeachIterator keyIterator = args[0].getNewIterator(env);
    ForeachIterator valueIterator = args[1].getNewIterator(env);
    ArrayMemory r = new ArrayMemory();
    while (keyIterator.next()) {
        if (valueIterator.next()) {
            r.refOfIndex(keyIterator.getValue()).assign(valueIterator.getValue().toImmutable());
        } else {
            return Memory.NULL;
        }
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 63 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ItemsUtils method map.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "callback", type = HintType.CALLABLE) })
public static Memory map(Environment env, Memory... args) throws Throwable {
    ForeachIterator iterator = args[0].getNewIterator(env);
    if (iterator == null) {
        return Memory.NULL;
    }
    Invoker callback = Invoker.valueOf(env, null, args[1]);
    if (callback == null) {
        return Memory.NULL;
    }
    ArrayMemory r = new ArrayMemory();
    while (iterator.next()) {
        r.refOfIndex(iterator.getMemoryKey()).assign(callback.call(iterator.getValue()));
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker)

Example 64 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ItemsUtils method flatten.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "level", optional = @Optional("-1")) })
public static Memory flatten(Environment env, Memory... args) {
    ArrayMemory r = new ArrayMemory();
    int level = args[1].toInteger();
    ForeachIterator iterator = args[0].getNewIterator(env);
    if (iterator == null) {
        return Memory.NULL;
    }
    Set<Integer> used = new HashSet<Integer>();
    used.add(args[0].getPointer());
    flatten(env, iterator, used, r, 0, level);
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 65 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ItemsUtils method sortByKeys.

@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "comparator", type = HintType.CALLABLE, optional = @Optional("null")), @Arg(value = "saveKeys", optional = @Optional("false")) })
public static Memory sortByKeys(Environment env, Memory... args) {
    boolean saveKeys = args[2].toBoolean();
    List<KeyValueMemory> tmp = new ArrayList<KeyValueMemory>();
    ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
    while (iterator.next()) {
        tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
    }
    final Invoker invoker = args[0].isNull() ? null : Invoker.valueOf(env, null, args[1]);
    Collections.sort(tmp, new Comparator<KeyValueMemory>() {

        @Override
        public int compare(KeyValueMemory o1, KeyValueMemory o2) {
            if (invoker == null)
                return o1.key.compareTo(o2.key);
            else
                return invoker.callNoThrow(o1.key, o2.key).toInteger();
        }
    });
    ArrayMemory r = new ArrayMemory();
    Iterator<KeyValueMemory> iterator1 = tmp.iterator();
    while (iterator1.hasNext()) {
        if (saveKeys)
            r.add(iterator1.next());
        else
            r.add(iterator1.next().value);
        iterator1.remove();
    }
    return r.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) KeyValueMemory(php.runtime.memory.KeyValueMemory) Invoker(php.runtime.invoke.Invoker)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3