give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Http
/
RequestBuilder.php
RequestBuilder.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at vendor/vendor-prefixed/stellarwp/licensing-api-client/src/Http/RequestBuilder.php
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 | |
| 5 | use JsonException; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Value\AuthToken; |
| 7 | use Give\Vendors\Psr\Http\Message\RequestFactoryInterface; |
| 8 | use Give\Vendors\Psr\Http\Message\RequestInterface; |
| 9 | use Give\Vendors\Psr\Http\Message\StreamFactoryInterface; |
| 10 | |
| 11 | /** |
| 12 | * Builds PSR-7 requests from SDK configuration and endpoint input. |
| 13 | * |
| 14 | * @phpstan-type QueryScalar string|int|float|bool|null |
| 15 | * @phpstan-type QueryList list<QueryScalar> |
| 16 | * @phpstan-type QueryValue QueryScalar|QueryList |
| 17 | * @phpstan-type HeaderValue string|int|float|bool |
| 18 | * @phpstan-type JsonScalar string|int|float|bool|null |
| 19 | * @phpstan-type JsonCollection array<array-key, JsonScalar|array<array-key, JsonScalar|array<array-key, JsonScalar|null>|null>|null> |
| 20 | * @phpstan-type JsonObject array<string, JsonScalar|JsonCollection|null> |
| 21 | */ |
| 22 | final class RequestBuilder |
| 23 | { |
| 24 | private RequestFactoryInterface $requestFactory; |
| 25 | |
| 26 | private StreamFactoryInterface $streamFactory; |
| 27 | |
| 28 | public function __construct( |
| 29 | RequestFactoryInterface $requestFactory, |
| 30 | StreamFactoryInterface $streamFactory |
| 31 | ) { |
| 32 | $this->requestFactory = $requestFactory; |
| 33 | $this->streamFactory = $streamFactory; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param array<string, QueryValue> $query |
| 38 | * @param JsonObject|null $body |
| 39 | * @param array<string, HeaderValue> $headers |
| 40 | * |
| 41 | * @throws JsonException |
| 42 | */ |
| 43 | public function build( |
| 44 | string $method, |
| 45 | ApiUri $uri, |
| 46 | array $query = [], |
| 47 | ?array $body = null, |
| 48 | ?AuthToken $token = null, |
| 49 | array $headers = [] |
| 50 | ): RequestInterface { |
| 51 | $request = $this->requestFactory->createRequest($method, $this->buildUri($uri, $query)); |
| 52 | |
| 53 | return $this->finalizeRequest($request, $body, $token, $headers); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param JsonObject|null $body |
| 58 | * @param array<string, HeaderValue> $headers |
| 59 | * |
| 60 | * @throws JsonException |
| 61 | */ |
| 62 | private function finalizeRequest( |
| 63 | RequestInterface $request, |
| 64 | ?array $body = null, |
| 65 | ?AuthToken $token = null, |
| 66 | array $headers = [] |
| 67 | ): RequestInterface { |
| 68 | |
| 69 | if ($token !== null) { |
| 70 | $request = $request->withHeader('X-LWS-Token', $token->get()); |
| 71 | } |
| 72 | |
| 73 | foreach ($headers as $name => $value) { |
| 74 | $request = $request->withHeader($name, (string) $value); |
| 75 | } |
| 76 | |
| 77 | if ($body !== null) { |
| 78 | $request = $request->withHeader('Content-Type', 'application/json'); |
| 79 | $request = $request->withBody( |
| 80 | $this->streamFactory->createStream(json_encode($body, JSON_THROW_ON_ERROR)) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return $request; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param array<string, QueryValue> $query |
| 89 | */ |
| 90 | private function buildUri(ApiUri $uri, array $query): string { |
| 91 | $uri = $uri->get(); |
| 92 | |
| 93 | $queryString = $this->buildQueryString($query); |
| 94 | |
| 95 | if ($queryString === '') { |
| 96 | return $uri; |
| 97 | } |
| 98 | |
| 99 | return $uri . (strpos($uri, '?') === false ? '?' : '&') . $queryString; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param array<string, QueryValue> $query |
| 104 | */ |
| 105 | private function buildQueryString(array $query): string { |
| 106 | $pairs = []; |
| 107 | |
| 108 | foreach ($query as $key => $value) { |
| 109 | if (is_array($value)) { |
| 110 | foreach ($value as $item) { |
| 111 | if ($item === null) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | $pairs[] = rawurlencode($key . '[]') . '=' . rawurlencode((string) $item); |
| 116 | } |
| 117 | |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | if ($value === null) { |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $pairs[] = rawurlencode($key) . '=' . rawurlencode((string) $value); |
| 126 | } |
| 127 | |
| 128 | return implode('&', $pairs); |
| 129 | } |
| 130 | } |
| 131 |