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