use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method sort.
@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 sort(Environment env, Memory... args) {
boolean saveKeys = args[2].toBoolean();
Memory[] sortTmp;
if (!saveKeys && args[0].isArray()) {
Memory[] original = args[0].toValue(ArrayMemory.class).values(true);
sortTmp = Arrays.copyOf(original, original.length);
} else {
ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
List<Memory> tmp = new ArrayList<Memory>();
while (iterator.next()) {
if (saveKeys)
tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
else
tmp.add(iterator.getValue().toImmutable());
}
sortTmp = tmp.toArray(new Memory[tmp.size()]);
tmp.clear();
}
if (args[1].isNull()) {
Arrays.sort(sortTmp, new Comparator<Memory>() {
@Override
public int compare(Memory o1, Memory o2) {
return o1.compareTo(o2);
}
});
} else {
final Invoker invoker = Invoker.valueOf(env, null, args[1]);
Arrays.sort(sortTmp, new Comparator<Memory>() {
@Override
public int compare(Memory o1, Memory o2) {
return invoker.callNoThrow(o1, o2).toInteger();
}
});
}
ArrayMemory r = new ArrayMemory();
for (Memory el : sortTmp) {
r.add(el);
}
return r.toConstant();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method __construct.
@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE) })
public Memory __construct(Environment env, Memory... args) {
ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
this.iterator = iterator;
this.worker = new Worker() {
@Override
public boolean next(Environment env) {
return WrapFlow.this.iterator.next();
}
};
this.worker.setIterator(iterator);
return Memory.NULL;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method flatten.
protected static void flatten(Environment env, ForeachIterator iterator, Set<Integer> used, ArrayMemory array, int level, int maxLevel) {
while (iterator.next()) {
Memory el = iterator.getValue();
ForeachIterator innerIterator = el.getNewIterator(env);
if (innerIterator == null || (level >= maxLevel && maxLevel > -1)) {
array.add(el.toImmutable());
} else {
if (used.add(el.getPointer())) {
flatten(env, innerIterator, used, array, level + 1, maxLevel);
used.remove(el.getPointer());
}
}
}
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method keys.
@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE) })
public static Memory keys(Environment env, Memory... args) {
ForeachIterator iterator = args[0].getNewIterator(env);
if (iterator == null) {
return Memory.NULL;
}
ArrayMemory r = new ArrayMemory();
while (iterator.next()) r.add(iterator.getMemoryKey());
return r.toConstant();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class StrUtils method join.
@FastMethod
@Signature({ @Arg("collection"), @Arg("separator"), @Arg(value = "limit", optional = @Optional(value = "0", type = HintType.INT)) })
public static Memory join(Environment env, Memory... args) {
String separator = args[1].toString();
StringBuilder builder = new StringBuilder();
int limit = args[2].toInteger();
int i = 0;
if (args[0].isArray()) {
ArrayMemory array = args[0].toValue(ArrayMemory.class);
int size = array.size();
if (limit > 0 && limit < size)
size = limit;
for (Memory el : array) {
builder.append(el);
if (i != size - 1)
builder.append(separator);
i++;
if (i == size)
break;
}
return new StringMemory(builder.toString());
} else {
ParameterEntity.validateTypeHinting(env, 1, args, HintType.TRAVERSABLE, false);
ForeachIterator iterator = args[0].getNewIterator(env);
while (iterator.next()) {
builder.append(iterator.getValue());
builder.append(separator);
i++;
if (limit > 0 && i == limit)
break;
}
int length = builder.length();
if (length > 0) {
builder.delete(length - separator.length(), length);
} else
return Memory.CONST_EMPTY_STRING;
return new StringMemory(builder.toString());
}
}
Aggregations