Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
11 / 12 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
FormUrlEncodedContent | |
91.67% |
11 / 12 |
|
50.00% |
1 / 2 |
4.01 | |
0.00% |
0 / 1 |
__construct | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
toHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Http\Client; |
6 | |
7 | use PeServer\Core\Binary; |
8 | use PeServer\Core\Collection\Dictionary; |
9 | use PeServer\Core\Encoding; |
10 | use PeServer\Core\Http\HttpHeader; |
11 | use PeServer\Core\Text; |
12 | use PeServer\Core\TypeUtility; |
13 | use PeServer\Core\Web\UrlEncodeKind; |
14 | use PeServer\Core\Web\UrlEncoding; |
15 | |
16 | class FormUrlEncodedContent extends StaticContentBase |
17 | { |
18 | /** |
19 | * 生成。 |
20 | * |
21 | * @param Dictionary<string|null> $map |
22 | * @param UrlEncoding|null $urlEncoding |
23 | */ |
24 | public function __construct(Dictionary $map, ?UrlEncoding $urlEncoding = null) |
25 | { |
26 | $urlEncoding ??= new UrlEncoding(UrlEncodeKind::Rfc1738, Encoding::getDefaultEncoding()); |
27 | |
28 | $kvItems = []; |
29 | foreach ($map as $key => $value) { |
30 | $encKey = $urlEncoding->encode($key); |
31 | if ($value === null) { |
32 | $kvItems[] = $encKey; |
33 | } else { |
34 | $encValue = $urlEncoding->encode($value); |
35 | $kvItems[] = "$encKey=$encValue"; |
36 | } |
37 | } |
38 | |
39 | $rawBody = Text::join('&', $kvItems); |
40 | $body = new Binary($rawBody); |
41 | |
42 | parent::__construct($body); |
43 | } |
44 | |
45 | #region StaticContentBase |
46 | |
47 | public function toHeader(): HttpHeader |
48 | { |
49 | return $this->createContentTypeHeader(" application/x-www-form-urlencoded"); |
50 | } |
51 | |
52 | #endregion |
53 | } |