Skip to content

Commit ec92f43

Browse files
committed
カスタムフィールド(複数選択)はリクエストが厄介なので対応。
1 parent 72b5e24 commit ec92f43

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/lib/BacklogAPIClient.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function Takuya\Utils\assert_str_is_domain;
1212
use League\Flysystem\WebDAV\WebDAVAdapter;
1313
use League\Flysystem\Filesystem;
14+
use function Takuya\Utils\array_map_with_key;
1415

1516
class BacklogAPIClient {
1617

@@ -167,10 +168,29 @@ public function call_api( $method, $path, $query = null, $params=null) {
167168

168169
return $res->getBody()->getContents();
169170
}
170-
protected function http_build_query($data,$numeric_prefix=false,$arg_separator=null,$encoding_type=PHP_QUERY_RFC3986){
171-
$str = http_build_query($data,$numeric_prefix,$arg_separator=null,$encoding_type);
172-
$str = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '%5B%5D=', $str);
173-
return $str;
171+
protected function http_build_query($data,$numeric_prefix=false,$arg_separator=null,$encoding_type=PHP_QUERY_RFC3986) {
172+
// custom field は複数キーが許可される。
173+
$cust_array = array_filter( $data, fn( $v, $k ) => preg_match( '/custom/', $k ) && is_array( $v ),
174+
ARRAY_FILTER_USE_BOTH );
175+
if ( sizeof( $cust_array ) > 0 ) {
176+
$cf_list = '&'.join( '&',
177+
array_map_with_key( $cust_array, function( $k, $values ) {
178+
return join( '&', array_map( fn( $v ) => sprintf( "%s=%s", urldecode( $k ), urlencode( $v ) ), $values ) );
179+
} ) );
180+
}else{
181+
$cf_list = '';
182+
}
183+
184+
// bool値が name=1 になるのを避けて、 name=true にする。
185+
array_walk_recursive( $data, function( &$v ) {
186+
if ( is_bool( $v ) ) {
187+
$v = json_encode( $v );
188+
}
189+
} );
190+
// 配列が name[0]=value になるのを避けて、name[]=value にする
191+
$str = http_build_query( $data, $numeric_prefix, $arg_separator = null, $encoding_type );
192+
$str = preg_replace( '/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '%5B%5D=', $str );
193+
return $str.$cf_list;
174194
}
175195

176196
protected function send_request( $method, $path, $opts ) {

tests/Unit/HTTP/HttpBuildQueryTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use tests\TestCase;
66
use GuzzleHttp\Client;
7+
use function Takuya\Utils\array_map_with_key;
78

89
class HttpBuildQueryTest extends TestCase {
910

@@ -16,5 +17,18 @@ public function test_http_build_query(){
1617
$this->assertStringContainsString('names[]=', urldecode($ret));
1718
$this->assertStringContainsString('あ[0]=', urldecode($ret));
1819
}
19-
20+
public function test_custom_field_for_http_build_query(){
21+
$data = ['customField_1234'=>1,'customField_1235'=>['a','b','c'],'customField_1236'=>['','','']];
22+
$cust_array = array_filter($data,fn($v,$k)=>preg_match('/custom/',$k)&&is_array($v),ARRAY_FILTER_USE_BOTH);
23+
// 処理
24+
$query = join('&',
25+
array_map_with_key($cust_array,function($k,$values){
26+
return join('&',array_map(fn($v)=> sprintf("%s=%s",urldecode($k),urlencode($v)),$values));
27+
}));
28+
29+
$this->assertEquals("customField_1235=a&customField_1235=b&customField_1235=c".
30+
"&customField_1236=%E3%81%82&customField_1236=%E3%81%84&customField_1236=%E3%81%86",$query);
31+
32+
33+
}
2034
}

0 commit comments

Comments
 (0)