give
/
vendor
/
vendor-prefixed
/
stellarwp
/
licensing-api-client
/
src
/
Http
/
RequestHeaderCollection.php
Factories
3 months ago
ApiUri.php
3 months ago
ApiVersion.php
3 months ago
AuthContext.php
3 months ago
AuthState.php
3 months ago
JsonDecoder.php
3 months ago
RequestBuilder.php
3 months ago
RequestExecutor.php
3 months ago
RequestHeaderCollection.php
3 months ago
RetryPolicy.php
3 months ago
RequestHeaderCollection.php
138 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 | |
| 5 | use Give\Vendors\LiquidWeb\LicensingApiClient\Tracing\TraceContext; |
| 6 | use Give\Vendors\LiquidWeb\LicensingApiClient\Tracing\TraceParent; |
| 7 | |
| 8 | /** |
| 9 | * Immutable request-header state shared across resource views. |
| 10 | * |
| 11 | * @phpstan-import-type HeaderValue from RequestBuilder |
| 12 | */ |
| 13 | final class RequestHeaderCollection |
| 14 | { |
| 15 | /** |
| 16 | * @var array<string, array{name: string, value: HeaderValue}> |
| 17 | */ |
| 18 | private array $headers; |
| 19 | |
| 20 | /** |
| 21 | * @param array<string, HeaderValue> $headers |
| 22 | */ |
| 23 | public function __construct(array $headers = []) { |
| 24 | $this->headers = $this->normalizeHeaders($headers); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return array<string, HeaderValue> |
| 29 | */ |
| 30 | public function all(): array { |
| 31 | $headers = []; |
| 32 | |
| 33 | foreach ($this->headers as $entry) { |
| 34 | $headers[$entry['name']] = $entry['value']; |
| 35 | } |
| 36 | |
| 37 | return $headers; |
| 38 | } |
| 39 | |
| 40 | public function withoutHeaders(): self { |
| 41 | if ($this->headers === []) { |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | return new self(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param array<string, HeaderValue> $headers |
| 50 | */ |
| 51 | public function withHeaders(array $headers): self { |
| 52 | if ($headers === []) { |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | $merged = array_replace($this->headers, $this->normalizeHeaders($headers)); |
| 57 | |
| 58 | if ($merged === $this->headers) { |
| 59 | return $this; |
| 60 | } |
| 61 | |
| 62 | return self::fromNormalized($merged); |
| 63 | } |
| 64 | |
| 65 | public function withTraceParent(TraceParent $traceParent): self { |
| 66 | $normalized = $this->headers; |
| 67 | $normalized['traceparent'] = [ |
| 68 | 'name' => 'traceparent', |
| 69 | 'value' => $traceParent->header(), |
| 70 | ]; |
| 71 | |
| 72 | unset($normalized['tracestate']); |
| 73 | |
| 74 | if ($normalized === $this->headers) { |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | return self::fromNormalized($normalized); |
| 79 | } |
| 80 | |
| 81 | public function withTraceContext(TraceContext $traceContext): self { |
| 82 | $normalized = array_replace($this->headers, $this->normalizeHeaders($traceContext->headers())); |
| 83 | |
| 84 | if ($traceContext->traceState() === null) { |
| 85 | unset($normalized['tracestate']); |
| 86 | } |
| 87 | |
| 88 | if ($normalized === $this->headers) { |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | return self::fromNormalized($normalized); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param array<string, HeaderValue> $headers |
| 97 | * |
| 98 | * @return array<string, HeaderValue> |
| 99 | */ |
| 100 | public function merge(array $headers): array { |
| 101 | return $this->withHeaders($headers)->all(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param array<string, array{name: string, value: HeaderValue}> $headers |
| 106 | */ |
| 107 | private static function fromNormalized(array $headers): self { |
| 108 | $self = new self(); |
| 109 | $self->headers = $headers; |
| 110 | |
| 111 | return $self; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param array<string, HeaderValue> $headers |
| 116 | * |
| 117 | * @return array<string, array{name: string, value: HeaderValue}> |
| 118 | */ |
| 119 | private function normalizeHeaders(array $headers): array { |
| 120 | $normalized = []; |
| 121 | |
| 122 | foreach ($headers as $name => $value) { |
| 123 | $name = trim((string) $name); |
| 124 | |
| 125 | if ($name === '') { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | $normalized[strtolower($name)] = [ |
| 130 | 'name' => $name, |
| 131 | 'value' => $value, |
| 132 | ]; |
| 133 | } |
| 134 | |
| 135 | return $normalized; |
| 136 | } |
| 137 | } |
| 138 |