NativeArray.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package php;
  23. import haxe.extern.EitherType;
  24. using php.Global;
  25. /**
  26. Native PHP array.
  27. **/
  28. @:coreType @:runtimeValue @:semantics(value) abstract NativeArray {
  29. public inline function new()
  30. this = Syntax.arrayDecl();
  31. @:arrayAccess function getByInt(key:Int):Dynamic;
  32. @:arrayAccess function setByInt(key:Int, val:Dynamic):Dynamic;
  33. @:arrayAccess function getByFloat(key:Float):Dynamic;
  34. @:arrayAccess function setByFloat(key:Float, val:Dynamic):Dynamic;
  35. @:arrayAccess function getByString(key:String):Dynamic;
  36. @:arrayAccess function setByString(key:String, val:Dynamic):Dynamic;
  37. @:arrayAccess function getByBool(key:Bool):Dynamic;
  38. @:arrayAccess function setByBool(key:Bool, val:Dynamic):Dynamic;
  39. public inline function iterator()
  40. return Global.array_values(this).iterator();
  41. public inline function keyValueIterator():NativeArrayKeyValueIterator
  42. return new NativeArrayKeyValueIterator(this);
  43. }
  44. private class NativeArrayKeyValueIterator {
  45. var length:Int;
  46. var current:Int = 0;
  47. var keys:NativeIndexedArray<EitherType<String, Int>>;
  48. var values:NativeIndexedArray<Dynamic>;
  49. public inline function new(data:NativeArray) {
  50. length = Global.count(data);
  51. this.keys = Global.array_keys(data);
  52. this.values = Global.array_values(data);
  53. }
  54. public inline function hasNext():Bool {
  55. return current < length;
  56. }
  57. public inline function next():{key:EitherType<String, Int>, value:Dynamic} {
  58. return {key: keys[current], value: values[current++]};
  59. }
  60. }