Search in sources :

Example 56 with ForeachIterator

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;
        }
    }));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory)

Example 57 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker)

Example 58 with ForeachIterator

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;
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator)

Example 59 with ForeachIterator

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);
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Invoker(php.runtime.invoke.Invoker)

Example 60 with ForeachIterator

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()));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) BinaryMemory(php.runtime.memory.BinaryMemory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3