1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Http;
4:
5: use Salient\Contract\Collection\CollectionInterface;
6: use Salient\Contract\Core\Arrayable;
7: use Salient\Contract\Core\Immutable;
8: use Stringable;
9:
10: /**
11: * @api
12: *
13: * @extends CollectionInterface<string,string[]>
14: */
15: interface HttpHeadersInterface extends
16: CollectionInterface,
17: Immutable,
18: Stringable
19: {
20: /**
21: * @param Arrayable<string,string[]|string>|iterable<string,string[]|string> $items
22: */
23: public function __construct($items = []);
24:
25: /**
26: * Parse and apply an HTTP header field or continuation thereof
27: *
28: * This method should be called once per HTTP header line. Each line must
29: * have a trailing CRLF. If an empty line (`"\r\n"`) is given,
30: * {@see HttpHeadersInterface::hasLastLine()} returns `true` and subsequent
31: * headers applied via {@see HttpHeadersInterface::addLine()} are flagged as
32: * trailers. Methods other than {@see HttpHeadersInterface::trailers()} and
33: * {@see HttpHeadersInterface::withoutTrailers()} make no distinction
34: * between trailers and other headers.
35: *
36: * @param bool $strict If `true`, throw an exception if `$line` is not
37: * \[RFC7230]-compliant.
38: * @return static
39: */
40: public function addLine(string $line, bool $strict = false);
41:
42: /**
43: * Check if addLine() has received an empty line
44: */
45: public function hasLastLine(): bool;
46:
47: /**
48: * Apply a value to a header, preserving any existing values
49: *
50: * @param string $key
51: * @param string[]|string $value
52: * @return static
53: */
54: public function add($key, $value);
55:
56: /**
57: * Apply a value to a header, replacing any existing values
58: *
59: * @param string[]|string $value
60: */
61: public function set($key, $value);
62:
63: /**
64: * Remove a header
65: */
66: public function unset($key);
67:
68: /**
69: * Merge the collection with the given headers, optionally preserving
70: * existing values
71: *
72: * @param Arrayable<string,string[]|string>|iterable<string,string[]|string> $items
73: */
74: public function merge($items, bool $addToExisting = false);
75:
76: /**
77: * Apply an access token to the collection
78: *
79: * @return static
80: */
81: public function authorize(
82: AccessTokenInterface $token,
83: string $headerName = HttpHeader::AUTHORIZATION
84: );
85:
86: /**
87: * Sort headers in the collection if necessary for compliance with [RFC7230]
88: *
89: * @return static
90: */
91: public function canonicalize();
92:
93: /**
94: * Reduce the collection to headers received after the HTTP message body
95: *
96: * @return static
97: */
98: public function trailers();
99:
100: /**
101: * Reduce the collection to headers received before the HTTP message body
102: *
103: * @return static
104: */
105: public function withoutTrailers();
106:
107: /**
108: * Get header names and values in their original order as a list of HTTP
109: * header fields, preserving the original case of each header
110: *
111: * If `$emptyFormat` is given, it is used for headers with an empty value.
112: *
113: * @return string[]
114: */
115: public function getLines(
116: string $format = '%s: %s',
117: ?string $emptyFormat = null
118: ): array;
119:
120: /**
121: * Get an array that maps header names to values, preserving the original
122: * case of the first appearance of each header
123: *
124: * @return array<string,string[]>
125: */
126: public function getHeaders(): array;
127:
128: /**
129: * Get an array that maps lowercase header names to comma-separated values
130: *
131: * @return array<string,string>
132: */
133: public function getHeaderLines(): array;
134:
135: /**
136: * True if a header is found in the collection
137: */
138: public function hasHeader(string $name): bool;
139:
140: /**
141: * Get the value of a header as a list of values
142: *
143: * @return string[]
144: */
145: public function getHeader(string $name): array;
146:
147: /**
148: * Get the value of a header as a list of values, splitting any
149: * comma-separated values
150: *
151: * @return string[]
152: */
153: public function getHeaderValues(string $name): array;
154:
155: /**
156: * Get the comma-separated values of a header
157: */
158: public function getHeaderLine(string $name): string;
159:
160: /**
161: * Get the first value of a header after splitting any comma-separated
162: * values
163: */
164: public function getFirstHeaderLine(string $name): string;
165:
166: /**
167: * Get the last value of a header after splitting any comma-separated values
168: */
169: public function getLastHeaderLine(string $name): string;
170:
171: /**
172: * Get the only value of a header after splitting any comma-separated values
173: *
174: * An exception is thrown if the header has more than one value.
175: */
176: public function getOneHeaderLine(string $name, bool $orSame = false): string;
177:
178: /**
179: * Get header names and values in their original order as a list of HTTP
180: * Archive (HAR) header objects, preserving the original case of each header
181: *
182: * @return array<array{name:string,value:string}>
183: */
184: public function jsonSerialize(): array;
185: }
186: