Skip to content

Commit 0da683b

Browse files
committed
配列[]をつけたリクエストに対応。
1 parent 268fc72 commit 0da683b

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/lib/BacklogAPIClient.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,33 @@ public function into_class( string $class, string $method, $query = [], $parent
114114
}
115115

116116
public function call_api( $method, $path, $query = null, $params=null) {
117+
$method = strtoupper($method);
117118
$key = ['apiKey' => $this->key];
118-
$query = array_merge_recursive($key, $query??[]);
119-
$options = ['query' => $query];
120-
//
121-
if(!empty($params['form_params'])||!empty($params['multipart'])){
122-
$options = array_merge_recursive($options,$params);
123-
}else if(!empty($params)){
124-
$options = ['query' => $query,'form_params'=>$params];
119+
$options = ['query' => $key ];
120+
121+
if ('GET'==$method && !empty($query)){
122+
$query = array_merge_recursive($key, $query??[]);
123+
$options = ['query' => $this->http_build_query($query)];
124+
}else if (in_array($method,['POST','PUT','PATCH','DELETE'] )&& !empty($params)){
125+
if (!empty($params['multipart'])){
126+
$options = array_merge_recursive($options,$params);
127+
}else{
128+
$options['body'] = $this->http_build_query($params['form_params']??$params);
129+
}
125130
}
131+
//
126132
$res = $this->send_request($method, $path,$options );
127133
if( str_contains($res->getHeader("Content-Type")[0], 'json') ) {
128134
return json_decode($res->getBody()->getContents());
129135
}
130136

131137
return $res->getBody()->getContents();
132138
}
139+
protected function http_build_query($data,$numeric_prefix=false,$arg_separator=null,$encoding_type=PHP_QUERY_RFC3986){
140+
$str = http_build_query($data,$numeric_prefix,$arg_separator=null,$encoding_type);
141+
$str = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '%5B%5D=', $str);
142+
return $str;
143+
}
133144

134145
protected function send_request( $method, $path, $opts ) {
135146
try {

tests/Unit/Api/BacklogApiCallTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,21 @@ public function test_issue_search_by_issue_id(){
104104
$this->assertEquals($issue_b->id,$issue_a->id);
105105
$this->assertEquals($issue_b,$issue_a);
106106
}
107-
107+
108+
public function test_query_parameter_has_array(){
109+
$api = $this->api_client();
110+
$projects = $this->api_client()->getProjectList();
111+
shuffle($projects);
112+
foreach ( $projects as $project ) {
113+
$list = $api->getIssueList(['projectId'=>[$project->id],'count'=>20]);
114+
if (sizeof($list)>0){
115+
break;
116+
}
117+
}
118+
$this->assertEquals($project->id, $list[0]->projectId);
119+
$source_ids = array_slice(array_map(fn($e)=>$e->id,$list),0,2);
120+
$result = $api->getIssueList(['id'=>$source_ids]);
121+
$result_ids = array_slice(array_map(fn($e)=>$e->id,$result),0,2);
122+
$this->assertEquals($source_ids,$result_ids);
123+
}
108124
}

0 commit comments

Comments
 (0)