Search in sources :

Example 76 with ForeachIterator

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

the class ArrayFunctions method array_count_values.

public static Memory array_count_values(Environment env, TraceInfo trace, Memory input) {
    if (expecting(env, trace, 1, input, ARRAY)) {
        ArrayMemory counts = new ArrayMemory();
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        boolean warning = false;
        while (iterator.next()) {
            Memory value = iterator.getValue();
            switch(value.getRealType()) {
                case INT:
                case STRING:
                    Memory count = counts.getOrCreate(value);
                    count.assign(count.inc());
                    break;
                default:
                    if (!warning) {
                        env.warning(trace, "array_count_values(): Can only count STRING and INTEGER values!");
                        warning = true;
                    }
            }
        }
        return counts.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 77 with ForeachIterator

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

the class ArrayFunctions method array_filter.

public static Memory array_filter(Environment env, TraceInfo trace, Memory input, Memory callback) throws Throwable {
    if (!expecting(env, trace, 1, input, ARRAY)) {
        return Memory.NULL;
    }
    Invoker invoker = null;
    if (callback != null && callback.toBoolean()) {
        invoker = expectingCallback(env, trace, 2, callback);
        if (invoker == null)
            return Memory.NULL;
    }
    ArrayMemory result = new ArrayMemory();
    ForeachIterator iterator = input.getNewIterator(env, true, false);
    while (iterator.next()) {
        Object key = iterator.getKey();
        Memory value = iterator.getValue();
        if (invoker == null) {
            if (!value.toBoolean())
                continue;
        } else if (!invoker.call(value).toBoolean())
            continue;
        result.put(key, value.toImmutable());
    }
    return result.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) 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 78 with ForeachIterator

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

the class ArrayFunctions method each.

public static Memory each(Environment env, TraceInfo trace, @Reference Memory array) {
    if (expectingReference(env, trace, array, "each")) {
        if (expecting(env, trace, 1, array, ARRAY)) {
            ForeachIterator iterator = array.toValue(ArrayMemory.class).getCurrentIterator();
            if (iterator.next()) {
                Memory value = iterator.getValue().toImmutable();
                Memory key = iterator.getMemoryKey();
                ArrayMemory result = new ArrayMemory();
                result.refOfIndex(1).assign(value);
                result.refOfIndex("value").assign(value);
                result.refOfIndex(0).assign(key);
                result.refOfIndex("key").assign(key);
                return result.toConstant();
            } else {
                return Memory.FALSE;
            }
        }
    }
    return Memory.FALSE;
}
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 79 with ForeachIterator

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

the class ArrayFunctions method array_flip.

public static Memory array_flip(Environment env, TraceInfo trace, Memory input) {
    if (!expecting(env, trace, 1, input, ARRAY))
        return Memory.NULL;
    ArrayMemory result = new ArrayMemory();
    ForeachIterator iterator = input.getNewIterator(env, false, false);
    while (iterator.next()) result.put(ArrayMemory.toKey(iterator.getValue()), iterator.getMemoryKey());
    return result.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 80 with ForeachIterator

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

the class ArrayFunctions method next.

public static Memory next(Environment env, TraceInfo trace, @Reference Memory array) {
    if (expectingReference(env, trace, array, "next")) {
        if (expecting(env, trace, 1, array, ARRAY)) {
            ArrayMemory memory = array.toValue(ArrayMemory.class);
            ForeachIterator iterator = memory.getCurrentIterator();
            if (iterator.next())
                return iterator.getValue().toImmutable();
            else
                return Memory.FALSE;
        }
    }
    return Memory.FALSE;
}
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