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;
}
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();
}
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;
}
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();
}
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;
}
Aggregations