Search in sources :

Example 66 with ForeachIterator

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

Example 67 with ForeachIterator

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

Example 68 with ForeachIterator

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());
            }
        }
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) KeyValueMemory(php.runtime.memory.KeyValueMemory) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 69 with ForeachIterator

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

Example 70 with ForeachIterator

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