Skip to content

Commit

Permalink
[php] Implement native iterator on arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Sep 17, 2019
1 parent 51cc984 commit f1eeab2
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion std/php/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import php.*;
using php.Global;

@:coreApi
final class Array<T> implements ArrayAccess<Int, T> {
final class Array<T> implements ArrayAccess<Int, T> implements NativeIterator<Int, T> {
public var length(default, null):Int;

var arr:NativeIndexedArray<T>;
Expand Down Expand Up @@ -228,6 +228,31 @@ final class Array<T> implements ArrayAccess<Int, T> {
}
}

@:noCompletion @:keep
function current():T {
return Global.current(arr);
}

@:noCompletion @:keep
function key():Int {
return Global.key($this->arr);
}

@:noCompletion @:keep
function next() {
return Global.next($this->arr);
}

@:noCompletion @:keep
function rewind() {
return Global.reset($this->arr);
}

@:noCompletion @:keep
function valid() {
return length > 0 && Global.key(arr) != null;
}

static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
var a = new Array();
a.arr = arr;
Expand Down Expand Up @@ -273,3 +298,12 @@ private extern interface ArrayAccess<K, V> {
private function offsetSet(offset:K, value:V):Void;
private function offsetUnset(offset:K):Void;
}

@:native('Iterator')
private extern interface NativeIterator<K, V> {
private function current():V;
private function key():K;
private function next():Void;
private function rewind():Void;
private function valid():Bool;
}

0 comments on commit f1eeab2

Please sign in to comment.