use of php.runtime.memory.ArrayMemory 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();
}
use of php.runtime.memory.ArrayMemory 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.memory.ArrayMemory in project jphp by jphp-compiler.
the class WrapTime method __debugInfo.
@Signature
public Memory __debugInfo(Environment env, Memory... args) {
ArrayMemory r = new ArrayMemory();
r.refOfIndex("*time").assign(date.getTime());
r.refOfIndex("*timeZone").assign(timeZone.getID());
return r.toConstant();
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class WrapTime method add.
@Signature(@Arg(value = "args", type = HintType.ARRAY))
public Memory add(Environment env, Memory... args) {
ArrayMemory arg = args[0].toValue(ArrayMemory.class);
Memory year = arg.getByScalar("year");
Memory month = arg.getByScalar("month");
Memory day = arg.getByScalar("day");
Memory hour = arg.getByScalar("hour");
Memory min = arg.getByScalar("min");
Memory sec = arg.getByScalar("sec");
Memory millis = arg.getByScalar("millis");
Calendar calendar1 = (Calendar) calendar.clone();
if (year != null)
calendar1.add(Calendar.YEAR, year.toInteger());
if (month != null)
calendar1.add(Calendar.MONTH, month.toInteger());
if (day != null)
calendar1.add(Calendar.DAY_OF_MONTH, day.toInteger());
if (hour != null)
calendar1.add(Calendar.HOUR_OF_DAY, hour.toInteger());
if (min != null)
calendar1.add(Calendar.MINUTE, min.toInteger());
if (sec != null)
calendar1.add(Calendar.SECOND, sec.toInteger());
if (millis != null)
calendar1.add(Calendar.MILLISECOND, millis.toInteger());
return new ObjectMemory(new WrapTime(env, calendar1.getTime(), timeZone));
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class WrapTimeFormat method __debugInfo.
@Signature
public Memory __debugInfo(Environment env, Memory... args) {
ArrayMemory r = new ArrayMemory();
r.refOfIndex("*format").assign(format);
return r.toConstant();
}
Aggregations