Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php] Implement native iterator on arrays #8821

Merged
merged 4 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions std/php/ArrayIterator.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package php;

/**
@see https://www.php.net/manual/en/class.arrayiterator.php
**/
@:native('ArrayIterator')
extern class ArrayIterator<K, V> implements php.ArrayAccess<K, V> implements SeekableIterator<K, V> implements Countable implements Serializable {
static final STD_PROP_LIST:Int;
benmerckx marked this conversation as resolved.
Show resolved Hide resolved
static final ARRAY_AS_PROPS:Int;

function new(?array:NativeArray, ?flags:Int);
function append(value:V):Void;
function asort():Void;
function count():Int;
function current():V;
function getArrayCopy():NativeArray;
function getFlags():Int;
function key():K;
function ksort():Void;
function natcasesort():Void;
function natsort():Void;
function next():Void;
function offsetExists(offset:K):Bool;
function offsetGet(offset:K):V;
function offsetSet(offset:K, value:V):Void;
function offsetUnset(offset:K):Void;
function rewind():Void;
function seek(position:Int):Void;
function serialize():String;
function setFlags(flags:Int):Void;
function uasort(cmp_function:(a:V, b:V) -> Int):Void;
function uksort(cmp_function:(a:K, b:K) -> Int):Void;
function unserialize(serialized:String):Void;
function valid():Bool;
}
31 changes: 31 additions & 0 deletions std/php/Countable.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package php;

/**
@see https://www.php.net/manual/en/class.countable.php
**/
@:native('Countable')
extern interface Countable {
function count():Int;
}
8 changes: 5 additions & 3 deletions std/php/IteratorAggregate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

package php;

/**
@see https://www.php.net/manual/en/class.iteratoraggregate.php
**/
@:native('IteratorAggregate')
extern interface IteratorAggregate<T> {
extern interface IteratorAggregate<T> extends Traversable {
/**
This method is not public to not induce Haxe users to use it ;)
Use iterator() instead.
The return type would be Aggregator that is unusable in Haxe
**/
private function getIterator():Iterator<T>; //
private function getIterator():Traversable;

}
36 changes: 36 additions & 0 deletions std/php/NativeIterator.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package php;

/**
Native PHP interface.
@see https://www.php.net/manual/en/class.iterator.php
**/
@:native('Iterator')
extern interface NativeIterator<K, V> extends Traversable {
function current():V;
function key():K;
function next():Void;
function rewind():Void;
function valid():Bool;
}
31 changes: 31 additions & 0 deletions std/php/SeekableIterator.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package php;

/**
@see https://www.php.net/manual/en/class.seekableiterator.php
**/
@:native('SeekableIterator')
extern interface SeekableIterator<K, V> extends NativeIterator<K, V> {
function seek(position:Int):Void;
}
32 changes: 32 additions & 0 deletions std/php/Serializable.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package php;

/**
@see https://www.php.net/manual/en/class.serializable.php
**/
@:native('Serializable')
extern interface Serializable {
function serialize():String;
function unserialize(serialized:String):Void;
}
10 changes: 8 additions & 2 deletions std/php/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
*/

import php.*;
import php.ArrayIterator as NativeArrayIterator;

using php.Global;

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

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

@:noCompletion @:keep
private function getIterator():Traversable {
return new NativeArrayIterator(arr);
}

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