22
33namespace Takuya \BacklogApiDocScraping ;
44
5- require_once ' helpers .php ' ;
5+ require_once __DIR__ . ' /../../../vendor/autoload .php ' ;
66class BacklogDocToClass {
77
88 public static function cache_path ($ file ){
@@ -18,8 +18,10 @@ public static function execute():int {
1818 $ ret = $ self ->api_list_json ();
1919 $ class_def = $ self ->generate_class_def ($ ret );
2020 $ html = $ self ->render_table ($ ret );
21- file_put_contents (__DIR__ .'/../../../api.html ' , $ html );
22- file_put_contents (__DIR__ .'/../../../src/BacklogAPIv2Methods.php ' , $ class_def );
21+ file_put_contents (__DIR__ .'/BacklogAPIv2Methods.php ' ,$ class_def );
22+
23+ //file_put_contents(__DIR__.'/../../../api.html', $html);
24+ //file_put_contents(__DIR__.'/../../../src/BacklogAPIv2Methods.php', $class_def);
2325 return 0 ;
2426 }
2527 protected function api_list_json () {
@@ -113,6 +115,18 @@ protected function parse_page( $url ) {
113115 }, iterator_to_array ($ trs ));
114116 $ info ['query_args ' ] = $ args ;
115117 }
118+ // リクエスト・パラメタ
119+ $ table = $ xpath ->evaluate ('//h2[contains(@id,"リクエストパラ") or contains(@id,"query-para")]/following-sibling::table[1]/tbody ' );
120+ if ( sizeof ($ table ) > 0 ) {
121+ $ trs = $ xpath ->evaluate ('./tr ' , $ table [0 ]);
122+ $ args = array_map (function ( $ tr ) use ( $ xpath ) {
123+ $ tds = $ xpath ->evaluate ('./td ' , $ tr );
124+ $ tds = array_map (fn ( $ e ) => $ e ->textContent , iterator_to_array ($ tds ));
125+ return $ tds ;
126+ }, iterator_to_array ($ trs ));
127+ $ info ['request_args ' ] = $ args ;
128+ }
129+
116130 $ info ['doc_url ' ] = $ url ;
117131 $ info ['kebab ' ] = basename (parse_url ($ url )['path ' ]);
118132 $ info ['Camel ' ] = kababToCamel ($ info ['kebab ' ]);
@@ -145,7 +159,7 @@ protected function parse_response_json_sample($json,$api_name){
145159 return $ ret ;
146160
147161 }
148- protected function generate_class_def ( $ ret ){
162+ protected function generate_class_def ($ ret ){
149163 $ method_definitions = [];
150164 foreach ($ ret as $ item ) {
151165 $ method_definitions []=$ this ->doc_to_method ($ item );
@@ -163,13 +177,19 @@ trait BacklogAPIv2Methods {
163177 return $ class_def ;
164178 }
165179 protected function doc_to_method ($ info ){
166-
180+
167181 $ http_meth = $ info ->method ;
168182 $ http_path = $ info ->path ;
169183 $ doc_url = $ info ->doc_url ;
170184 dump ($ doc_url );
171185 $ name = $ info ->Camel ;
172-
186+
187+
188+ if ($ name == 'postAttachmentFile ' ){
189+ $ info ->request_args =[['multipart[] ' ,'' ,'' ]];
190+ }
191+
192+
173193 $ res_shape = $ this ->response_sample_to_array_shape ($ info ->response );
174194 $ path_params = [];
175195 if (str_contains ($ info ->path ,': ' )){
@@ -179,21 +199,58 @@ protected function doc_to_method($info){
179199 $ http_path = str_replace (': ' ,'$ ' ,$ http_path );
180200 }
181201
182-
183- $ func_args = implode (', ' ,array_filter ([...($ path_params ),!empty ($ info ->query_args ) ? '$query_options=[] ' :null ]));
202+ $ request_args_shape = '' ;
203+ $ func_args = $ path_params ;
204+ if (!empty ($ info ->request_args )){
205+
206+ $ assoc = $ info ->request_args ;
207+ $ assoc = array_map (fn ($ e )=>$ e [0 ],$ assoc );
208+ $ assoc = array_map (fn ($ e )=>preg_replace ('/[^A-z]/ ' ,'' ,$ e ),$ assoc );
209+ $ assoc = array_map (fn ($ e )=>"' $ {e}'=>null " ,$ assoc );
210+ $ assoc = array_map (fn ($ e )=>str_replace ("[]'=>null " ,"'=>[] " ,$ e ),$ assoc );
211+ if ($ name == 'postAttachmentFile ' ) {// マルチパート
212+ $ assoc = ["'multipart'=>['name'=>'','filename'=>'','contents'=>'',] " ];
213+ }
214+ $ func_args [] = str_replace ('{assoc} ' ,implode (', ' ,$ assoc ),'$params=[{assoc}] ' );
215+
216+
217+ $ array_shape = $ assoc ;
218+ $ array_shape = array_map (fn ($ e )=>str_replace ("' " ,'' ,$ e ),$ array_shape );
219+ $ array_shape = array_map (fn ($ e )=>preg_split ('/=>/ ' ,$ e ),$ array_shape );
220+ $ array_shape = array_map (function ($ e ){$ e [1 ]=$ e [1 ]=='null ' ?null :$ e [1 ];return $ e ;},$ array_shape );
221+ $ array_shape = array_map (function ($ e ){$ e [1 ]=$ e [1 ]=='[] ' ?[]:$ e [1 ];return $ e ;},$ array_shape );
222+ $ array_shape = array_combine (array_map (fn ($ e )=>$ e [0 ],$ array_shape ),array_map (fn ($ e )=>$ e [1 ],$ array_shape ));
223+ $ request_args_shape = $ this ->response_sample_to_array_shape ((object )$ array_shape );
224+ if ($ name == 'postAttachmentFile ' ){
225+ $ request_args_shape = "array{ multipart: array{name: string,filename:string,contents:string}} " ;
226+ }
227+ }else if (!empty ($ info ->query_args )){
228+ $ func_args [] ='$query_options=[] ' ;
229+ }
230+ $ func_args = implode (', ' , array_filter ($ func_args ));
231+ dump ($ func_args );
184232 $ func_def = [];
185233 $ func_def [] = "/** " ;
186234 $ func_def [] = "* {$ info ->title }" ;
187235 $ func_def [] = "* " ;
188236 foreach ($ path_params as $ path_param ){
189- $ func_def [] = !empty ($ path_param ) ? "* @params string|int $ path_param " :null ;
237+ $ func_def [] = !empty ($ path_param ) ? "* @param string|int $ path_param " :null ;
190238 }
191- $ func_def [] = isset ($ info ->query_args ) ? '* @params array $query_options ' :null ;
239+ $ func_def [] = !empty ($ info ->query_args ) ? '* @param array $query_options ' :null ;
240+ $ func_def [] = !empty ($ info ->request_args ) ? "* @param $ request_args_shape \$params " :null ;
192241 $ func_def [] = "* @return {$ res_shape }" ;
193242 $ func_def [] = "* @link {$ info ->doc_url }" ;
194243 $ func_def [] = "*/ " ;
195244 $ func_def [] = "public function $ name( $ func_args){ " ;
196- $ func_def [] = " return \$this->call_api(' {$ http_meth }', \"{$ http_path }\"" .(!empty ($ info ->query_args ) ? ',$query_options ' :'' )." ); " ;
245+ if (!empty ($ info ->query_args )){
246+ $ func_body = sprintf ( ' return $this->call_api("%s", "%s", $query_options ); ' , $ http_meth ,$ http_path , );
247+ }
248+ else if (!empty ($ info ->request_args )){
249+ $ func_body = sprintf ( ' return $this->call_api("%s", "%s",[], $params ); ' , $ http_meth ,$ http_path , );
250+ }else {
251+ $ func_body = sprintf ( ' return $this->call_api("%s", "%s" ); ' , $ http_meth ,$ http_path , );
252+ }
253+ $ func_def [] = $ func_body ;
197254 $ func_def [] = "} " ;
198255 $ func_def = array_filter ($ func_def );
199256 $ func_def = " " .implode (PHP_EOL ." " ,$ func_def );
@@ -217,10 +274,12 @@ protected function response_sample_to_array_shape($res){
217274 protected function convert_object_to_array_shape ($ obj ){
218275 $ shape = (array )$ obj ;
219276 $ shape = array_map ('gettype ' ,$ shape );
277+ $ shape = array_combine (array_keys ($ shape ),array_map (fn ($ e )=>$ e =='NULL ' ?'?string ' :$ e ,$ shape ));
220278 $ shape = json_encode ($ shape );
221279 $ shape = str_replace ('" ' ,'' ,$ shape );
222280 $ shape = str_replace (': ' ,': ' ,$ shape );
223- $ shape = 'array ' .$ shape ;
281+ $ shape = str_replace (', ' ,', ' ,$ shape );
282+ $ shape = is_array ($ obj )?'array ' :'object ' .$ shape ;
224283 return $ shape ;
225284 }
226285 protected function render_table ( $ docs ) {
0 commit comments