Syntax.hx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 function coalesce<T>( left:T, right:T ) : T ;
  39. /**
  40. Generates `$left . $right`
  41. **/
  42. static inline function concat( left:String, right:String ) : String {
  43. return code('({0} . {1})', left, right);
  44. }
  45. /**
  46. Generates `$left == $right`
  47. **/
  48. static inline function equal( left:Dynamic, right:Dynamic ) : Bool {
  49. return code('({0} == {1})', left, right);
  50. }
  51. /**
  52. Generates `$left === $right`
  53. **/
  54. static inline function strictEqual( left:Dynamic, right:Dynamic ) : Bool {
  55. return code('({0} === {1})', left, right);
  56. }
  57. /**
  58. Generates `$left != $right`
  59. **/
  60. static inline function notEqual( left:Dynamic, right:Dynamic ) : Bool {
  61. return code('({0} != {1})', left, right);
  62. }
  63. /**
  64. Generates `$left !== $right`
  65. **/
  66. static inline function strictNotEqual( left:Dynamic, right:Dynamic ) : Bool {
  67. return code('({0} !== {1})', left, right);
  68. }
  69. /**
  70. Generates `$left + $right` for numbers.
  71. **/
  72. static inline function add<T:Float>( left:T, right:T ) : T {
  73. return code('({0} + {1})', left, right);
  74. }
  75. /**
  76. Generates `$left + $right` for php arrays.
  77. **/
  78. static inline function union( left:NativeArray, right:NativeArray ) : NativeArray {
  79. return codeDeref('({0} + {1})', left, right);
  80. }
  81. /**
  82. Generates `$left ** $right`
  83. **/
  84. static inline function exp<T:Float>( left:T, right:T ) : T {
  85. return code('({0} ** {1})', left, right);
  86. }
  87. /**
  88. Generates `$left % $right`
  89. **/
  90. static inline function mod( left:Float, right:Float ) : Int {
  91. return code('({0} % {1})', left, right);
  92. }
  93. /**
  94. Generates `$left ?: $right`
  95. **/
  96. static inline function shortTernary<T>( left:T, right:T ) : T {
  97. return codeDeref('({0} ?: {1})', left, right);
  98. }
  99. /**
  100. Generates `$left xor $right`
  101. **/
  102. static inline function xor( left:Bool, right:Bool ) : Bool {
  103. return code('({0} xor {1})', left, right);
  104. }
  105. /**
  106. Generates `(int)$value`
  107. **/
  108. static inline function int( value:Dynamic ) : Int {
  109. return code('(int)({0})', value);
  110. }
  111. /**
  112. Generates `(float)$value`
  113. **/
  114. static inline function float( value:Dynamic ) : Float {
  115. return code('(float)({0})', value);
  116. }
  117. /**
  118. Generates `(string)$value`
  119. **/
  120. static inline function string( value:Dynamic ) : String {
  121. return code('(string)({0})', value);
  122. }
  123. /**
  124. Generates `(bool)$value`
  125. **/
  126. static inline function bool( value:Dynamic ) : Bool {
  127. return code('(bool)({0})', value);
  128. }
  129. /**
  130. Generates `(object)$value`
  131. **/
  132. static inline function object( value:Dynamic ) : StdClass {
  133. return codeDeref('(object)({0})', value);
  134. }
  135. /**
  136. Generates `(array)$value`
  137. **/
  138. static inline function array( value:Dynamic ) : NativeArray {
  139. return codeDeref('(array)({0})', value);
  140. }
  141. /**
  142. Generates `$value instanceof $phpClassName`.
  143. Haxe generates `Std.is(value, Type)` calls as `$value instanceof Type` automatically where possible.
  144. So you may need this only if you have a `Class` stored in a variable.
  145. **/
  146. @:overload(function( value:AsVar<Dynamic>, phpClassName:AsVar<String> ) : Bool {})
  147. static function instanceof<V,C>( value:AsVar<V>, type:AsVar<Class<C>> ) : Bool;
  148. /**
  149. Generates PHP class name for a provided Haxe class.
  150. ```
  151. trace(Syntax.nativeClassName(php.Web)); // outputs: php\Web
  152. ```
  153. */
  154. static function nativeClassName<T>(cls:EitherType<Class<T>, Enum<T>>) : String;
  155. /**
  156. ```
  157. Syntax.foreach(collection, function(key, value) trace(key, value));
  158. ```
  159. generates:
  160. ```
  161. foreach($collection as $key => $value) {
  162. trace($key, $value);
  163. }
  164. ```
  165. **/
  166. static function foreach<TCollection,TKey,TValue>( collection:TCollection, body:TKey->TValue->Void ) : Void;
  167. /**
  168. Generates `new $className($arg1, ...$argN)`
  169. **/
  170. @:overload(function(className:AsVar<String>, args:Rest<Dynamic>):Dynamic {})
  171. static function construct<T>( cls:AsVar<Class<T>>, args:Rest<Dynamic>) : T;
  172. /**
  173. Generates instance field access for reading on `object`
  174. **/
  175. static function field<T>( object:AsVar<T>, fieldName:String ) : Dynamic;
  176. /**
  177. Generates instance field access for reading on `object`
  178. **/
  179. @:deprecated("php.Syntax.getFiled() is deprecated. Use php.Syntax.field() instead.")
  180. static function getField<T>( object:AsVar<T>, fieldName:String ) : Dynamic;
  181. /**
  182. Generates instance field access for writing on `object`
  183. **/
  184. static function setField<T>( object:AsVar<T>, fieldName:String, value:Dynamic ) : Void;
  185. /**
  186. Generates static field access for reading on `className`
  187. **/
  188. static function getStaticField( className:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:String ) : Dynamic;
  189. /**
  190. Generates static field access for writing on `object`
  191. **/
  192. static function setStaticField( object:AsVar<EitherType<Class<Dynamic>,String>>, fieldName:String, value:Dynamic ) : Void;
  193. /**
  194. Generates a call to instance method: `$object->{$methodName}(<args>)`
  195. **/
  196. static function call<T>( object:AsVar<T>, methodName:String, args:Rest<Dynamic> ) : Dynamic;
  197. /**
  198. Generates a call to static method: `$className::{$methodName}(<args>)`
  199. **/
  200. static function staticCall( className:AsVar<EitherType<Class<Dynamic>,String>>, methodName:String, args:Rest<Dynamic> ) : Dynamic;
  201. /**
  202. ```
  203. Syntax.arrayDecl(arg1, arg2, arg3);
  204. ```
  205. Generates native array declaration:
  206. ```
  207. [$arg1, $arg2, $arg3]
  208. ```
  209. **/
  210. static function arrayDecl<T>( args:Rest<T> ) : NativeIndexedArray<T>;
  211. /**
  212. ```
  213. Syntax.assocDecl({field1:'first', field2:2}});
  214. ```
  215. Generates native associative array declaration:
  216. ```
  217. ["field1" => "first", "field2" => 2];
  218. ```
  219. This method is not recursive.
  220. Accepts object declarations only.
  221. That means you can't pass an object stored in a variable to this method like `Syntax.assocDecl(someVar)`.
  222. Use `php.Lib.associativeArrayOfObject(someVar)` instead.
  223. **/
  224. static function assocDecl<T:{}>( ?arg:T ) : NativeAssocArray<Dynamic>;
  225. /**
  226. Don't let compiler to optimize away local var passed to this method.
  227. **/
  228. static function keepVar( localVar:Dynamic ) : Void;
  229. /**
  230. Adds `...` operator before `args`
  231. **/
  232. static inline function splat( args:EitherType<NativeArray, Traversable> ) : Rest<Dynamic> {
  233. return code('...{0}', args);
  234. }
  235. /**
  236. Add errors suppression operator `@` before `expression`
  237. **/
  238. static function suppress<T>( expression:T ) : T;
  239. /**
  240. Generates `clone $value`.
  241. @see http://php.net/manual/en/language.oop5.cloning.php
  242. */
  243. static inline function clone<T>(value:T):T {
  244. return Syntax.code('clone {0}', value);
  245. }
  246. /**
  247. Generates `yield $value`.
  248. @see http://php.net/manual/en/language.generators.syntax.php
  249. */
  250. static inline function yield(value:Dynamic):Dynamic {
  251. return Syntax.code('yield {0}', value);
  252. }
  253. /**
  254. Generates `yield $key => $value`.
  255. @see http://php.net/manual/en/language.generators.syntax.php
  256. */
  257. static inline function yieldPair(key:Dynamic, value:Dynamic):Dynamic {
  258. return Syntax.code('yield {0} => {1}', key, value);
  259. }
  260. /**
  261. Generates `yield for $value`.
  262. @see http://php.net/manual/en/language.generators.syntax.php
  263. */
  264. static inline function yieldFrom(value:Dynamic):Dynamic {
  265. return Syntax.code('yield from {0}', value);
  266. }
  267. }