Syntax.hx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package php;
  2. import haxe.extern.Rest;
  3. import haxe.extern.AsVar;
  4. import haxe.extern.EitherType;
  5. /**
  6. Special extern class to support PHP language specifics.
  7. Don't use these functions unless you are really sure what you are doing.
  8. **/
  9. extern class Syntax {
  10. /**
  11. Embeds plain php code.
  12. `php` should be a string literal with php code.
  13. It can contain placeholders like `{0}`, `{1}` which will be replaced with corresponding arguments from `args`.
  14. E.g.:
  15. ```
  16. Syntax.code("var_dump({0}, {1})", a, b);
  17. ```
  18. will generate
  19. ```
  20. var_dump($a, $b);
  21. ```
  22. **/
  23. static function code(code:String, args:Rest<Dynamic>):Dynamic;
  24. /**
  25. The same as `code()`, but adds dereferencing
  26. when required to workaround "cannot use temporary expression in write context" php error.
  27. **/
  28. static function codeDeref(code:String, args:Rest<Dynamic>):Dynamic;
  29. /**
  30. Generates `$left <=> $right`
  31. **/
  32. static inline function spaceship( left:Dynamic, right:Dynamic ) : Int {
  33. return code('({0} <=> {1})', left, right);
  34. }
  35. /**
  36. Generates `$left ?? $right`
  37. **/
  38. static inline function coalesce<T>( left:T, right:T ) : T {
  39. return codeDeref('({0} ?? {1})', left, right);
  40. }
  41. /**
  42. Generates `$left . $right`
  43. **/
  44. static inline function concat( left:String, right:String ) : String {
  45. return code('({0} . {1})', left, right);
  46. }
  47. /**
  48. Generates `$left == $right`
  49. **/
  50. static inline function equal( left:Dynamic, right:Dynamic ) : Bool {
  51. return code('({0} == {1})', left, right);
  52. }
  53. /**
  54. Generates `$left === $right`
  55. **/
  56. static inline function strictEqual( left:Dynamic, right:Dynamic ) : Bool {
  57. return code('({0} === {1})', left, right);
  58. }
  59. /**
  60. Generates `$left != $right`
  61. **/
  62. static inline function notEqual( left:Dynamic, right:Dynamic ) : Bool {
  63. return code('({0} != {1})', left, right);
  64. }
  65. /**
  66. Generates `$left !== $right`
  67. **/
  68. static inline function strictNotEqual( left:Dynamic, right:Dynamic ) : Bool {
  69. return code('({0} !== {1})', left, right);
  70. }
  71. /**
  72. Generates `$left + $right` for numbers.
  73. **/
  74. static inline function add<T:Float>( left:T, right:T ) : T {
  75. return code('({0} + {1})', left, right);
  76. }
  77. /**
  78. Generates `$left + $right` for php arrays.
  79. **/
  80. static inline function union( left:NativeArray, right:NativeArray ) : NativeArray {
  81. return codeDeref('({0} + {1})', left, right);
  82. }
  83. /**
  84. Generates `$left ** $right`
  85. **/
  86. static inline function exp<T:Float>( left:T, right:T ) : T {
  87. return code('({0} ** {1})', left, right);
  88. }
  89. /**
  90. Generates `$left % $right`
  91. **/
  92. static inline function mod( left:Float, right:Float ) : Int {
  93. return code('({0} % {1})', left, right);
  94. }
  95. /**
  96. Generates `$left ?: $right`
  97. **/
  98. static inline function shortTernary<T>( left:T, right:T ) : T {
  99. return codeDeref('({0} ?: {1})', left, right);
  100. }
  101. /**
  102. Generates `$left xor $right`
  103. **/
  104. static inline function xor( left:Bool, right:Bool ) : Bool {
  105. return code('({0} xor {1})', left, right);
  106. }
  107. /**
  108. Generates `(int)$value`
  109. **/
  110. static inline function int( value:Dynamic ) : Int {
  111. return code('(int)({0})', value);
  112. }
  113. /**
  114. Generates `(float)$value`
  115. **/
  116. static inline function float( value:Dynamic ) : Float {
  117. return code('(float)({0})', value);
  118. }
  119. /**
  120. Generates `(string)$value`
  121. **/
  122. static inline function string( value:Dynamic ) : String {
  123. return code('(string)({0})', value);
  124. }
  125. /**
  126. Generates `(bool)$value`
  127. **/
  128. static inline function bool( value:Dynamic ) : Bool {
  129. return code('(bool)({0})', value);
  130. }
  131. /**
  132. Generates `(object)$value`
  133. **/
  134. static inline function object( value:Dynamic ) : StdClass {
  135. return codeDeref('(object)({0})', value);
  136. }
  137. /**
  138. Generates `(array)$value`
  139. **/
  140. static inline function array( value:Dynamic ) : NativeArray {
  141. return codeDeref('(array)({0})', value);
  142. }
  143. /**
  144. Generates `$value instanceof $phpClassName`.
  145. Haxe generates `Std.is(value, Type)` calls as `$value instanceof Type` automatically where possible.
  146. So you may need this only if you have a `Class` stored in a variable.
  147. **/
  148. @:overload(function( value:AsVar<Dynamic>, phpClassName:AsVar<String> ) : Bool {})
  149. static function instanceof<V,C>( value:AsVar<V>, type:AsVar<Class<C>> ) : Bool;
  150. /**
  151. Generates PHP class name for a provided Haxe class.
  152. ```
  153. trace(Syntax.nativeClassName(php.Web)); // outputs: php\Web
  154. ```
  155. */
  156. static function nativeClassName<T>(cls:EitherType<Class<T>, Enum<T>>) : String;
  157. /**
  158. ```
  159. Syntax.foreach(collection, function(key, value) trace(key, value));
  160. ```
  161. generates:
  162. ```
  163. foreach($collection as $key => $value) {
  164. trace($key, $value);
  165. }
  166. ```
  167. **/
  168. static function foreach<TCollection,TKey,TValue>( collection:TCollection, body:TKey->TValue->Void ) : Void;
  169. /**
  170. Generates `new $className($arg1, ...$argN)`
  171. **/
  172. @:overload(function(className:AsVar<String>, args:Rest<Dynamic>):Dynamic {})
  173. static function construct<T>( cls:AsVar<Class<T>>, args:Rest<Dynamic>) : T;
  174. /**
  175. Generates instance field access for reading on `object`
  176. **/
  177. static function field<T>( object:AsVar<T>, fieldName:AsVar<String> ) : Dynamic;
  178. /**
  179. Generates instance field access for reading on `object`
  180. **/
  181. @:deprecated("php.Syntax.getFiled() is deprecated. Use php.Syntax.field() instead.")
  182. static function getField<T>( object:AsVar<T>, fieldName:AsVar<String> ) : Dynamic;
  183. /**
  184. Generates instance field access for writing on `object`
  185. **/
  186. static function setField<T>( object:AsVar<T>, fieldName:AsVar<String>, value:Dynamic ) : Void;
  187. /**
  188. Generates static field access for reading on `className`
  189. **/
  190. static function getStaticField( className:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:AsVar<String> ) : Dynamic;
  191. /**
  192. Generates static field access for writing on `object`
  193. **/
  194. static function setStaticField( object:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:AsVar<String>, value:Dynamic ) : Void;
  195. /**
  196. Generates a call to instance method: `$object->{$methodName}(<args>)`
  197. **/
  198. static function call<T>( object:AsVar<T>, methodName:AsVar<String>, args:Rest<Dynamic> ) : Dynamic;
  199. /**
  200. Generates a call to static method: `$className::{$methodName}(<args>)`
  201. **/
  202. static function staticCall( className:AsVar<EitherType<Class<Dynamic>,String>>, methodName:AsVar<String>, args:Rest<Dynamic> ) : Dynamic;
  203. /**
  204. ```
  205. Syntax.arrayDecl(arg1, arg2, arg3);
  206. ```
  207. Generates native array declaration:
  208. ```
  209. [$arg1, $arg2, $arg3]
  210. ```
  211. **/
  212. static function arrayDecl<T>( args:Rest<T> ) : NativeIndexedArray<T>;
  213. /**
  214. ```
  215. Syntax.assocDecl({field1:'first', field2:2}});
  216. ```
  217. Generates native associative array declaration:
  218. ```
  219. ["field1" => "first", "field2" => 2];
  220. ```
  221. This method is not recursive.
  222. Accepts object declarations only.
  223. That means you can't pass an object stored in a variable to this method like `Syntax.assocDecl(someVar)`.
  224. Use `php.Lib.associativeArrayOfObject(someVar)` instead.
  225. **/
  226. static function assocDecl<T:{}>( ?arg:T ) : NativeAssocArray<Dynamic>;
  227. /**
  228. Don't let compiler to optimize away local var passed to this method.
  229. **/
  230. static function keepVar( localVar:Dynamic ) : Void;
  231. /**
  232. Adds `...` operator before `args`
  233. **/
  234. static inline function splat( args:EitherType<NativeArray, Traversable> ) : Rest<Dynamic> {
  235. return code('...{0}', args);
  236. }
  237. /**
  238. Add errors suppression operator `@` before `expression`
  239. **/
  240. static function suppress<T>( expression:T ) : T;
  241. }