use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class FsUtils method hasExt.
@Signature
public static boolean hasExt(Environment env, String path, Memory extensions, boolean ignoreCase) {
Set<String> exts = new HashSet<>();
if (extensions.isTraversable()) {
ForeachIterator iterator = extensions.getNewIterator(env);
while (iterator.next()) {
String value = iterator.getValue().toString();
if (ignoreCase) {
value = value.toLowerCase();
}
exts.add(value);
}
} else {
exts.add(ignoreCase ? extensions.toString().toLowerCase() : extensions.toString());
}
String ext = ext(path);
if (ignoreCase && ext != null) {
ext = ext.toLowerCase();
}
return exts.contains(ext);
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method combine.
@Signature({ @Arg(value = "keys", type = HintType.TRAVERSABLE), @Arg(value = "values", type = HintType.TRAVERSABLE) })
public static Memory combine(Environment env, Memory... args) {
ForeachIterator keyIterator = args[0].getNewIterator(env);
ForeachIterator valueIterator = args[1].getNewIterator(env);
ArrayMemory r = new ArrayMemory();
while (keyIterator.next()) {
if (valueIterator.next()) {
r.refOfIndex(keyIterator.getValue()).assign(valueIterator.getValue().toImmutable());
} else {
return Memory.NULL;
}
}
return r.toConstant();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method map.
@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "callback", type = HintType.CALLABLE) })
public static Memory map(Environment env, Memory... args) throws Throwable {
ForeachIterator iterator = args[0].getNewIterator(env);
if (iterator == null) {
return Memory.NULL;
}
Invoker callback = Invoker.valueOf(env, null, args[1]);
if (callback == null) {
return Memory.NULL;
}
ArrayMemory r = new ArrayMemory();
while (iterator.next()) {
r.refOfIndex(iterator.getMemoryKey()).assign(callback.call(iterator.getValue()));
}
return r.toConstant();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method flatten.
@Signature({ @Arg(value = "collection", type = HintType.TRAVERSABLE), @Arg(value = "level", optional = @Optional("-1")) })
public static Memory flatten(Environment env, Memory... args) {
ArrayMemory r = new ArrayMemory();
int level = args[1].toInteger();
ForeachIterator iterator = args[0].getNewIterator(env);
if (iterator == null) {
return Memory.NULL;
}
Set<Integer> used = new HashSet<Integer>();
used.add(args[0].getPointer());
flatten(env, iterator, used, r, 0, level);
return r.toConstant();
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class ItemsUtils method sortByKeys.
@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 sortByKeys(Environment env, Memory... args) {
boolean saveKeys = args[2].toBoolean();
List<KeyValueMemory> tmp = new ArrayList<KeyValueMemory>();
ForeachIterator iterator = args[0].toImmutable().getNewIterator(env);
while (iterator.next()) {
tmp.add(new KeyValueMemory(iterator.getMemoryKey(), iterator.getValue().toImmutable()));
}
final Invoker invoker = args[0].isNull() ? null : Invoker.valueOf(env, null, args[1]);
Collections.sort(tmp, new Comparator<KeyValueMemory>() {
@Override
public int compare(KeyValueMemory o1, KeyValueMemory o2) {
if (invoker == null)
return o1.key.compareTo(o2.key);
else
return invoker.callNoThrow(o1.key, o2.key).toInteger();
}
});
ArrayMemory r = new ArrayMemory();
Iterator<KeyValueMemory> iterator1 = tmp.iterator();
while (iterator1.hasNext()) {
if (saveKeys)
r.add(iterator1.next());
else
r.add(iterator1.next().value);
iterator1.remove();
}
return r.toConstant();
}
Aggregations