use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method group.
@Signature({ @Arg(value = "callback", type = HintType.CALLABLE) })
public Memory group(Environment env, Memory... args) {
final Invoker invoker = Invoker.valueOf(env, null, args[0]);
final ForeachIterator iterator = getSelfIterator(env);
return new ObjectMemory(new WrapFlow(env, new ForeachIterator(false, false, false) {
protected Memory key;
@Override
protected boolean init() {
key = Memory.CONST_INT_M1;
return true;
}
@Override
protected boolean nextValue() {
Memory r = new ArrayMemory();
boolean done = false;
while (iterator.next()) {
done = true;
if (withKeys)
r.refOfIndex(iterator.getMemoryKey()).assign(iterator.getValue());
else
r.refOfPush().assign(iterator.getValue());
if (call(iterator, invoker).toBoolean())
break;
}
if (done) {
currentKeyMemory = key.inc();
currentKey = currentKeyMemory;
currentValue = r;
}
return done;
}
@Override
protected boolean prevValue() {
return false;
}
@Override
public void reset() {
iterator.reset();
key = Memory.CONST_INT_M1;
}
}));
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method findOne.
@Signature(@Arg(value = "filter", type = HintType.CALLABLE, optional = @Optional("NULL")))
public Memory findOne(Environment env, Memory... args) {
final Invoker invoker = Invoker.valueOf(env, null, args[0]);
ForeachIterator iterator = getSelfIterator(env);
while (iterator.next()) {
if (call(iterator, invoker).toBoolean())
return iterator.getValue();
}
return Memory.NULL;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method findValue.
@Signature({ @Arg(value = "value"), @Arg(value = "strict", optional = @Optional("false")) })
public Memory findValue(Environment env, Memory... args) {
ForeachIterator iterator = getSelfIterator(env);
boolean strict = args[1].toBoolean();
while (iterator.next()) {
if (strict && iterator.getValue().identical(args[0])) {
return iterator.getMemoryKey();
} else if (iterator.getValue().equal(args[0])) {
return iterator.getMemoryKey();
}
}
return Memory.NULL;
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class WrapFlow method each.
@Signature(@Arg(value = "callback", type = HintType.CALLABLE))
public Memory each(Environment env, Memory... args) {
ForeachIterator iterator = getSelfIterator(env);
Invoker invoker = Invoker.valueOf(env, null, args[0]);
int cnt = 0;
while (iterator.next()) {
cnt++;
if (call(iterator, invoker).toValue() == Memory.FALSE)
break;
}
return LongMemory.valueOf(cnt);
}
use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.
the class BinUtils method of.
@FastMethod
@Signature(@Arg("value"))
public static Memory of(Environment env, Memory... args) {
if (ParameterEntity.checkTypeHinting(env, args[0], HintType.TRAVERSABLE)) {
ForeachIterator iterator = args[0].getNewIterator(env, false, false);
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
while (iterator.next()) {
tmp.write(iterator.getValue().toInteger());
}
return new BinaryMemory(tmp.toByteArray());
}
return new BinaryMemory(args[0].getBinaryBytes(env.getDefaultCharset()));
}
Aggregations