@@ -39,7 +39,7 @@ class JsonRpcClientGenerator {
39
39
* @param array $smd SMD Schema
40
40
* @param string $className
41
41
*/
42
- public function __construct ( $ url , $ smd , $ className ) {
42
+ public function __construct ( string $ url , array $ smd , string $ className ) {
43
43
$ this ->url = $ url ;
44
44
$ this ->smd = $ smd ;
45
45
$ this ->className = $ className ;
@@ -56,12 +56,22 @@ private function getHeader() {
56
56
$ date = date ( 'd.m.Y G:i ' );
57
57
$ result = <<<php
58
58
<?php
59
+ /**
60
+ * PHP RPC Client by JsonRpcClientGenerator
61
+ * @date {$ date }
62
+ */
63
+
64
+ namespace JsonRpcClient;
65
+
66
+ use EazyJsonRpc\BaseJsonRpcClient;
67
+ use EazyJsonRpc\BaseJsonRpcException;
68
+ use GuzzleHttp\Exception\GuzzleException;
69
+ use JsonMapper_Exception;
70
+
59
71
/**
60
72
* {$ description }
61
- * @author JsonRpcClientGenerator
62
- * @date {$ date }
63
73
*/
64
- class {$ this ->className } extends \EazyJsonRpc\ BaseJsonRpcClient {
74
+ class {$ this ->className } extends BaseJsonRpcClient {
65
75
php ;
66
76
67
77
return $ result ;
@@ -73,121 +83,163 @@ class {$this->className} extends \EazyJsonRpc\BaseJsonRpcClient {
73
83
* @param $methodData
74
84
* @return string
75
85
*/
76
- public function getMethod ( $ methodName , $ methodData ) {
77
- $ newDocLine = PHP_EOL . str_repeat ( ' ' , 9 ) . '* ' ;
78
- $ description = !empty ( $ methodData ['description ' ] ) ? $ methodData ['description ' ] : $ methodName ;
86
+ public function getMethod ( $ methodName , $ methodData ): string {
87
+ $ newDocLine = PHP_EOL . str_repeat ( ' ' , 8 ) . '* ' ;
88
+ $ description = sprintf ( '<%s> RPC method ' , $ methodName );
89
+ $ description .= !empty ( $ methodData ['description ' ] ) ? $ newDocLine . ' ' . trim ( $ methodData ['description ' ] ) : '' ;
90
+ $ description = str_replace ( "\n" , PHP_EOL , $ description );
79
91
$ strDocParams = '' ;
80
- $ strParamsArr = [ ];
81
- $ callParamsArr = [ ];
92
+ $ strParamsArr = [];
93
+ $ callParamsArr = [];
82
94
$ methodName = str_replace ( '. ' , '_ ' , $ methodName );
83
95
84
- // Add Default Parameter = IsNotification
85
- $ methodData ['parameters ' ][] = [
86
- 'name ' => 'isNotification ' ,
87
- 'optional ' => 'true ' ,
88
- 'type ' => 'bool ' ,
89
- 'default ' => false ,
90
- 'description ' => 'set to true if call is notification ' ,
91
- ];
92
-
93
96
// params
94
97
if ( !empty ( $ methodData ['parameters ' ] ) ) {
95
98
// Set Doc Params
96
99
foreach ( $ methodData ['parameters ' ] as $ param ) {
97
- $ name = $ param ['name ' ];
98
- $ strParam = '$ ' . $ name ;
99
- $ strDocParamsArr = [ $ newDocLine ];
100
- $ strDocParamsArr [] = '@param ' ;
101
-
100
+ $ name = $ param ['name ' ];
101
+ $ strDocParam = $ newDocLine ;
102
+ $ strDocParam .= " @param " ;
102
103
if ( !empty ( $ param ['type ' ] ) ) {
103
- $ strDocParamsArr [] = $ param ['type ' ];
104
+ $ strDocParam .= " " . $ this ->getPhpType ( $ param ['type ' ] );
105
+ }
106
+ $ strParam = $ this ->getPhpType ( $ param ['type ' ] ) . ' $ ' . $ name ;
107
+ $ optionalParam = !empty ( $ param ['optional ' ] );
108
+ if ( $ optionalParam ) {
109
+ $ strDocParam .= '|null ' ;
104
110
}
105
111
106
- $ strDocParamsArr [] = '$ ' . $ name ;
107
- if ( ! empty ( $ param [ ' optional ' ] ) ) {
108
- $ strDocParamsArr [] = '[optional] ' ;
112
+ $ strDocParam . = ' $ ' . $ name ;
113
+ if ( $ optionalParam ) {
114
+ $ strDocParam . = ' [optional] ' ;
109
115
}
110
116
111
117
if ( !empty ( $ param ['description ' ] ) ) {
112
- $ strDocParamsArr [] = $ param ['description ' ];
118
+ $ strDocParam .= " " . $ param ['description ' ];
113
119
}
114
120
115
121
if ( array_key_exists ( 'default ' , $ param ) ) {
116
122
$ strParam .= sprintf ( ' = %s ' , var_export ( $ param ['default ' ], true ) );
123
+ } else {
124
+ if ( $ optionalParam ) {
125
+ $ strParam .= ' = null ' ;
126
+ }
117
127
}
118
128
119
- $ strDocParams .= rtrim ( implode ( ' ' , $ strDocParamsArr ) );
129
+ $ strDocParams .= rtrim ( $ strDocParam );
120
130
$ strParamsArr [] = $ strParam ;
121
131
$ callParamsArr [$ name ] = sprintf ( "'%s' => $%s " , $ name , $ name );
122
132
}
123
133
}
124
-
125
- $ strParams = ' ' . trim (
126
- str_replace (
127
- [ "\n" , ',) ' , 'array ( ' ],
128
- [ '' , ') ' , 'array( ' ],
129
- implode ( ', ' , $ strParamsArr )
130
- ), ', ' ) . ' ' ;
131
-
132
- unset( $ callParamsArr ['isNotification ' ] );
133
-
134
+ $ strDocParams .= $ newDocLine . ' @param bool $isNotification [optional] set to true if call is notification ' ;
135
+
136
+ $ strParams = str_replace (
137
+ [ "\n" , ',) ' , 'array ( ' ],
138
+ [ '' , ') ' , 'array( ' ],
139
+ implode ( ', ' , $ strParamsArr )
140
+ );
141
+
142
+ $ strParams .= ', $isNotification = false ' ;
143
+ $ strParams = ' ' . trim ( $ strParams , ', ' ) . ' ' ;
144
+ $ returnType = '' ;
145
+ $ optionalReturn = '' ;
146
+ $ strDocReturns = $ newDocLine . ' @return mixed ' ;
147
+ $ strReturnType = '' ;
134
148
// returns
135
- if ( !empty ( $ methodData ['returns ' ] ) && !empty ( $ methodData ['returns ' ]['type ' ] ) && is_string ( $ methodData ['returns ' ]['type ' ] ) ) {
136
- $ strDocParams .= $ newDocLine . ' @return \EazyJsonRpc\BaseJsonRpcCall (result: ' . $ methodData ['returns ' ]['type ' ] . ') ' ;
149
+ if ( !empty ( $ methodData ['returns ' ] ) ) {
150
+ $ strDocReturns = '' ;
151
+ if ( !empty ( $ methodData ['returns ' ]['type ' ] ) && is_string ( $ methodData ['returns ' ]['type ' ] ) ) {
152
+ $ returnType = $ this ->getPhpType ( $ methodData ['returns ' ]['type ' ] );
153
+ $ strDocReturns .= $ newDocLine . ' @return ' . $ returnType ;
154
+ }
155
+ if ( !empty ( $ methodData ['returns ' ]['optional ' ] ) ) {
156
+ $ optionalReturn = '? ' ;
157
+ $ strDocReturns .= '|null ' ;
158
+ }
159
+ if ( !empty ( $ methodData ['returns ' ]['description ' ] ) ) {
160
+ $ strDocReturns .= ' ' . $ methodData ['returns ' ]['description ' ];
161
+ }
162
+ if ( $ returnType != 'mixed ' ) {
163
+ $ strReturnType = sprintf ( ': %s%s ' , $ optionalReturn , $ returnType );
164
+ }
137
165
}
138
-
166
+ $ strDocParams .= $ strDocReturns ;
139
167
$ callParamsStr = implode ( ', ' , $ callParamsArr );
140
168
if ( !empty ( $ callParamsStr ) ) {
141
169
$ callParamsStr = sprintf ( ' %s ' , $ callParamsStr );
142
170
}
143
171
144
172
145
- $ result = <<<php
173
+ return <<<php
146
174
/**
147
- * {$ description }{$ strDocParams }
148
- */
149
- public function {$ methodName }( {$ strParams }) {
150
- return \$this->call( __FUNCTION__, array( {$ callParamsStr }), \$this->getRequestId( \$isNotification ) );
175
+ * {$ description }{$ strDocParams }
176
+ * @throws BaseJsonRpcException
177
+ * @throws GuzzleException
178
+ * @throws JsonMapper_Exception
179
+ */
180
+ public function {$ methodName }( {$ strParams }) $ strReturnType {
181
+ return \$this->call( __FUNCTION__, ' $ returnType', [ {$ callParamsStr }], \$this->getRequestId( \$isNotification ) );
151
182
}
152
183
153
184
php ;
154
- return $ result ;
155
185
156
186
}
157
187
158
188
159
189
/**
160
190
* Get Footer
161
191
*/
162
- private function getFooter () {
163
- $ url = $ this ->url ;
164
- $ urlInfo = parse_url ( $ url );
192
+ private function getFooter (): string {
193
+ $ rpcUrl = $ this ->url ;
194
+ $ urlInfo = parse_url ( $ rpcUrl );
165
195
if ( !empty ( $ urlInfo ) ) {
166
- $ url = sprintf ( '%s://%s%s ' , $ urlInfo ['scheme ' ], $ urlInfo ['host ' ], $ this ->smd ['target ' ] );
196
+ $ rpcUrl = sprintf ( '%s://%s%s ' , $ urlInfo ['scheme ' ], $ urlInfo ['host ' ], $ this ->smd ['target ' ] );
167
197
}
168
198
169
- $ result = <<<php
199
+ return <<<php
170
200
171
201
172
202
/**
173
203
* Get Instance
204
+ * @param \$url string
174
205
* @return {$ this ->className }
175
206
*/
176
- public static function GetInstance() {
177
- return new self( ' { $ url} ' );
207
+ public static function GetInstance( string \$ url ): { $ this -> className } {
208
+ return new self( \ $url );
178
209
}
179
210
180
211
}
181
212
php ;
213
+ }
182
214
183
- return $ result ;
215
+
216
+ /**
217
+ * Return PHP type from SMD type
218
+ * @param string $smdType
219
+ * @return string
220
+ */
221
+ private function getPhpType ( string $ smdType ): string {
222
+ switch ( $ smdType ) {
223
+ case "string " :
224
+ return "string " ;
225
+ case "object " :
226
+ case "array " :
227
+ return "array " ;
228
+ case "boolean " :
229
+ return "bool " ;
230
+ case "float " :
231
+ return "float " ;
232
+ case "integer " :
233
+ return "int " ;
234
+ }
235
+ return "mixed " ;
184
236
}
185
237
186
238
187
239
/**
188
240
* Save to File
189
241
*/
190
- public function Generate () {
242
+ public function Generate (): string {
191
243
$ this ->result = $ this ->getHeader ();
192
244
193
245
foreach ( $ this ->smd ['services ' ] as $ methodName => $ methodData ) {
@@ -205,7 +257,7 @@ public function Generate() {
205
257
* Save To File
206
258
* @return int
207
259
*/
208
- public function SaveToFile () {
260
+ public function SaveToFile (): int {
209
261
return file_put_contents ( $ this ->className . '.php ' , $ this ->Generate () );
210
262
}
211
263
}
0 commit comments