Search in sources :

Example 81 with ForeachIterator

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

the class ArrayFunctions method array_walk.

public static boolean array_walk(Environment env, TraceInfo trace, @Reference Memory input, Memory callback, Memory userData) throws Throwable {
    if (!expectingReference(env, trace, input, "array_walk"))
        return false;
    if (!expecting(env, trace, 1, input, ARRAY))
        return false;
    Invoker invoker = expectingCallback(env, trace, 2, callback);
    if (invoker == null)
        return false;
    ForeachIterator iterator = input.getNewIterator(env, true, false);
    while (iterator.next()) {
        Memory item = iterator.getValue();
        Memory key = iterator.getMemoryKey();
        invoker.call(item, key, userData);
    }
    return true;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 82 with ForeachIterator

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

the class ArrayFunctions method array_column.

public static Memory array_column(Environment env, TraceInfo trace, Memory input, Memory columnKey, Memory indexKey) {
    if (expecting(env, trace, 1, input, ARRAY)) {
        if (columnKey.isNull() && indexKey.isNull())
            return array_values(env, trace, input);
        ArrayMemory result = new ArrayMemory();
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        while (iterator.next()) {
            Memory value = iterator.getValue();
            if (indexKey.isNull()) {
                result.add(value.valueOfIndex(columnKey).toImmutable());
            } else {
                if (columnKey.isNull())
                    result.refOfIndex(value.valueOfIndex(indexKey)).assign(value.toImmutable());
                else
                    result.refOfIndex(value.valueOfIndex(indexKey)).assign(value.valueOfIndex(columnKey));
            }
        }
        return result.toConstant();
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory)

Example 83 with ForeachIterator

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

the class ArrayFunctions method array_change_key_case.

public static Memory array_change_key_case(Environment env, TraceInfo trace, Memory input, int _case) {
    if (expecting(env, trace, 1, input, ARRAY)) {
        ArrayMemory result = new ArrayMemory();
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        while (iterator.next()) {
            Object key = iterator.getKey();
            if (key instanceof String) {
                String str = (String) key;
                str = _case == 0 ? str.toLowerCase() : str.toUpperCase();
                result.put(str, iterator.getValue().toImmutable());
            } else
                result.put(key, iterator.getValue().toImmutable());
        }
        return result.toConstant();
    }
    return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 84 with ForeachIterator

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

the class SPLFunctions method iterator_count.

public static long iterator_count(Environment env, TraceInfo trace, Memory object) {
    if (expectingImplement(env, trace, 1, object, Traversable.class)) {
        IObject tmp = object.toValue(ObjectMemory.class).value;
        if (tmp instanceof Countable) {
            return ((Countable) tmp).count(env).toLong();
        } else {
            ForeachIterator iterator = object.getNewIterator(env, true, false);
            int i = 0;
            while (iterator.next()) i++;
            return i;
        }
    } else
        return 0;
}
Also used : Countable(php.runtime.lang.spl.Countable) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 85 with ForeachIterator

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

the class SPLFunctions method iterator_to_array.

public static Memory iterator_to_array(Environment env, TraceInfo trace, Memory object, boolean useKeys) {
    if (expectingImplement(env, trace, 1, object, Traversable.class)) {
        ArrayMemory result = new ArrayMemory();
        ForeachIterator iterator = object.getNewIterator(env, false, false);
        while (iterator.next()) {
            if (useKeys) {
                result.refOfIndex(iterator.getMemoryKey()).assign(iterator.getValue());
            } else {
                result.add(iterator.getValue());
            }
        }
        return result.toConstant();
    }
    return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

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