annotate vendor/guzzlehttp/psr7/README.md @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 # PSR-7 Message Implementation
Chris@0 2
Chris@0 3 This repository contains a full [PSR-7](http://www.php-fig.org/psr/psr-7/)
Chris@0 4 message implementation, several stream decorators, and some helpful
Chris@0 5 functionality like query string parsing.
Chris@0 6
Chris@0 7
Chris@0 8 [![Build Status](https://travis-ci.org/guzzle/psr7.svg?branch=master)](https://travis-ci.org/guzzle/psr7)
Chris@0 9
Chris@0 10
Chris@0 11 # Stream implementation
Chris@0 12
Chris@0 13 This package comes with a number of stream implementations and stream
Chris@0 14 decorators.
Chris@0 15
Chris@0 16
Chris@0 17 ## AppendStream
Chris@0 18
Chris@0 19 `GuzzleHttp\Psr7\AppendStream`
Chris@0 20
Chris@0 21 Reads from multiple streams, one after the other.
Chris@0 22
Chris@0 23 ```php
Chris@0 24 use GuzzleHttp\Psr7;
Chris@0 25
Chris@0 26 $a = Psr7\stream_for('abc, ');
Chris@0 27 $b = Psr7\stream_for('123.');
Chris@0 28 $composed = new Psr7\AppendStream([$a, $b]);
Chris@0 29
Chris@0 30 $composed->addStream(Psr7\stream_for(' Above all listen to me'));
Chris@0 31
Chris@0 32 echo $composed; // abc, 123. Above all listen to me.
Chris@0 33 ```
Chris@0 34
Chris@0 35
Chris@0 36 ## BufferStream
Chris@0 37
Chris@0 38 `GuzzleHttp\Psr7\BufferStream`
Chris@0 39
Chris@0 40 Provides a buffer stream that can be written to fill a buffer, and read
Chris@0 41 from to remove bytes from the buffer.
Chris@0 42
Chris@0 43 This stream returns a "hwm" metadata value that tells upstream consumers
Chris@0 44 what the configured high water mark of the stream is, or the maximum
Chris@0 45 preferred size of the buffer.
Chris@0 46
Chris@0 47 ```php
Chris@0 48 use GuzzleHttp\Psr7;
Chris@0 49
Chris@0 50 // When more than 1024 bytes are in the buffer, it will begin returning
Chris@0 51 // false to writes. This is an indication that writers should slow down.
Chris@0 52 $buffer = new Psr7\BufferStream(1024);
Chris@0 53 ```
Chris@0 54
Chris@0 55
Chris@0 56 ## CachingStream
Chris@0 57
Chris@0 58 The CachingStream is used to allow seeking over previously read bytes on
Chris@0 59 non-seekable streams. This can be useful when transferring a non-seekable
Chris@0 60 entity body fails due to needing to rewind the stream (for example, resulting
Chris@0 61 from a redirect). Data that is read from the remote stream will be buffered in
Chris@0 62 a PHP temp stream so that previously read bytes are cached first in memory,
Chris@0 63 then on disk.
Chris@0 64
Chris@0 65 ```php
Chris@0 66 use GuzzleHttp\Psr7;
Chris@0 67
Chris@0 68 $original = Psr7\stream_for(fopen('http://www.google.com', 'r'));
Chris@0 69 $stream = new Psr7\CachingStream($original);
Chris@0 70
Chris@0 71 $stream->read(1024);
Chris@0 72 echo $stream->tell();
Chris@0 73 // 1024
Chris@0 74
Chris@0 75 $stream->seek(0);
Chris@0 76 echo $stream->tell();
Chris@0 77 // 0
Chris@0 78 ```
Chris@0 79
Chris@0 80
Chris@0 81 ## DroppingStream
Chris@0 82
Chris@0 83 `GuzzleHttp\Psr7\DroppingStream`
Chris@0 84
Chris@0 85 Stream decorator that begins dropping data once the size of the underlying
Chris@0 86 stream becomes too full.
Chris@0 87
Chris@0 88 ```php
Chris@0 89 use GuzzleHttp\Psr7;
Chris@0 90
Chris@0 91 // Create an empty stream
Chris@0 92 $stream = Psr7\stream_for();
Chris@0 93
Chris@0 94 // Start dropping data when the stream has more than 10 bytes
Chris@0 95 $dropping = new Psr7\DroppingStream($stream, 10);
Chris@0 96
Chris@0 97 $dropping->write('01234567890123456789');
Chris@0 98 echo $stream; // 0123456789
Chris@0 99 ```
Chris@0 100
Chris@0 101
Chris@0 102 ## FnStream
Chris@0 103
Chris@0 104 `GuzzleHttp\Psr7\FnStream`
Chris@0 105
Chris@0 106 Compose stream implementations based on a hash of functions.
Chris@0 107
Chris@0 108 Allows for easy testing and extension of a provided stream without needing
Chris@0 109 to create a concrete class for a simple extension point.
Chris@0 110
Chris@0 111 ```php
Chris@0 112
Chris@0 113 use GuzzleHttp\Psr7;
Chris@0 114
Chris@0 115 $stream = Psr7\stream_for('hi');
Chris@0 116 $fnStream = Psr7\FnStream::decorate($stream, [
Chris@0 117 'rewind' => function () use ($stream) {
Chris@0 118 echo 'About to rewind - ';
Chris@0 119 $stream->rewind();
Chris@0 120 echo 'rewound!';
Chris@0 121 }
Chris@0 122 ]);
Chris@0 123
Chris@0 124 $fnStream->rewind();
Chris@0 125 // Outputs: About to rewind - rewound!
Chris@0 126 ```
Chris@0 127
Chris@0 128
Chris@0 129 ## InflateStream
Chris@0 130
Chris@0 131 `GuzzleHttp\Psr7\InflateStream`
Chris@0 132
Chris@0 133 Uses PHP's zlib.inflate filter to inflate deflate or gzipped content.
Chris@0 134
Chris@0 135 This stream decorator skips the first 10 bytes of the given stream to remove
Chris@0 136 the gzip header, converts the provided stream to a PHP stream resource,
Chris@0 137 then appends the zlib.inflate filter. The stream is then converted back
Chris@0 138 to a Guzzle stream resource to be used as a Guzzle stream.
Chris@0 139
Chris@0 140
Chris@0 141 ## LazyOpenStream
Chris@0 142
Chris@0 143 `GuzzleHttp\Psr7\LazyOpenStream`
Chris@0 144
Chris@0 145 Lazily reads or writes to a file that is opened only after an IO operation
Chris@0 146 take place on the stream.
Chris@0 147
Chris@0 148 ```php
Chris@0 149 use GuzzleHttp\Psr7;
Chris@0 150
Chris@0 151 $stream = new Psr7\LazyOpenStream('/path/to/file', 'r');
Chris@0 152 // The file has not yet been opened...
Chris@0 153
Chris@0 154 echo $stream->read(10);
Chris@0 155 // The file is opened and read from only when needed.
Chris@0 156 ```
Chris@0 157
Chris@0 158
Chris@0 159 ## LimitStream
Chris@0 160
Chris@0 161 `GuzzleHttp\Psr7\LimitStream`
Chris@0 162
Chris@0 163 LimitStream can be used to read a subset or slice of an existing stream object.
Chris@0 164 This can be useful for breaking a large file into smaller pieces to be sent in
Chris@0 165 chunks (e.g. Amazon S3's multipart upload API).
Chris@0 166
Chris@0 167 ```php
Chris@0 168 use GuzzleHttp\Psr7;
Chris@0 169
Chris@0 170 $original = Psr7\stream_for(fopen('/tmp/test.txt', 'r+'));
Chris@0 171 echo $original->getSize();
Chris@0 172 // >>> 1048576
Chris@0 173
Chris@0 174 // Limit the size of the body to 1024 bytes and start reading from byte 2048
Chris@0 175 $stream = new Psr7\LimitStream($original, 1024, 2048);
Chris@0 176 echo $stream->getSize();
Chris@0 177 // >>> 1024
Chris@0 178 echo $stream->tell();
Chris@0 179 // >>> 0
Chris@0 180 ```
Chris@0 181
Chris@0 182
Chris@0 183 ## MultipartStream
Chris@0 184
Chris@0 185 `GuzzleHttp\Psr7\MultipartStream`
Chris@0 186
Chris@0 187 Stream that when read returns bytes for a streaming multipart or
Chris@0 188 multipart/form-data stream.
Chris@0 189
Chris@0 190
Chris@0 191 ## NoSeekStream
Chris@0 192
Chris@0 193 `GuzzleHttp\Psr7\NoSeekStream`
Chris@0 194
Chris@0 195 NoSeekStream wraps a stream and does not allow seeking.
Chris@0 196
Chris@0 197 ```php
Chris@0 198 use GuzzleHttp\Psr7;
Chris@0 199
Chris@0 200 $original = Psr7\stream_for('foo');
Chris@0 201 $noSeek = new Psr7\NoSeekStream($original);
Chris@0 202
Chris@0 203 echo $noSeek->read(3);
Chris@0 204 // foo
Chris@0 205 var_export($noSeek->isSeekable());
Chris@0 206 // false
Chris@0 207 $noSeek->seek(0);
Chris@0 208 var_export($noSeek->read(3));
Chris@0 209 // NULL
Chris@0 210 ```
Chris@0 211
Chris@0 212
Chris@0 213 ## PumpStream
Chris@0 214
Chris@0 215 `GuzzleHttp\Psr7\PumpStream`
Chris@0 216
Chris@0 217 Provides a read only stream that pumps data from a PHP callable.
Chris@0 218
Chris@0 219 When invoking the provided callable, the PumpStream will pass the amount of
Chris@0 220 data requested to read to the callable. The callable can choose to ignore
Chris@0 221 this value and return fewer or more bytes than requested. Any extra data
Chris@0 222 returned by the provided callable is buffered internally until drained using
Chris@0 223 the read() function of the PumpStream. The provided callable MUST return
Chris@0 224 false when there is no more data to read.
Chris@0 225
Chris@0 226
Chris@0 227 ## Implementing stream decorators
Chris@0 228
Chris@0 229 Creating a stream decorator is very easy thanks to the
Chris@0 230 `GuzzleHttp\Psr7\StreamDecoratorTrait`. This trait provides methods that
Chris@0 231 implement `Psr\Http\Message\StreamInterface` by proxying to an underlying
Chris@0 232 stream. Just `use` the `StreamDecoratorTrait` and implement your custom
Chris@0 233 methods.
Chris@0 234
Chris@0 235 For example, let's say we wanted to call a specific function each time the last
Chris@0 236 byte is read from a stream. This could be implemented by overriding the
Chris@0 237 `read()` method.
Chris@0 238
Chris@0 239 ```php
Chris@0 240 use Psr\Http\Message\StreamInterface;
Chris@0 241 use GuzzleHttp\Psr7\StreamDecoratorTrait;
Chris@0 242
Chris@0 243 class EofCallbackStream implements StreamInterface
Chris@0 244 {
Chris@0 245 use StreamDecoratorTrait;
Chris@0 246
Chris@0 247 private $callback;
Chris@0 248
Chris@0 249 public function __construct(StreamInterface $stream, callable $cb)
Chris@0 250 {
Chris@0 251 $this->stream = $stream;
Chris@0 252 $this->callback = $cb;
Chris@0 253 }
Chris@0 254
Chris@0 255 public function read($length)
Chris@0 256 {
Chris@0 257 $result = $this->stream->read($length);
Chris@0 258
Chris@0 259 // Invoke the callback when EOF is hit.
Chris@0 260 if ($this->eof()) {
Chris@0 261 call_user_func($this->callback);
Chris@0 262 }
Chris@0 263
Chris@0 264 return $result;
Chris@0 265 }
Chris@0 266 }
Chris@0 267 ```
Chris@0 268
Chris@0 269 This decorator could be added to any existing stream and used like so:
Chris@0 270
Chris@0 271 ```php
Chris@0 272 use GuzzleHttp\Psr7;
Chris@0 273
Chris@0 274 $original = Psr7\stream_for('foo');
Chris@0 275
Chris@0 276 $eofStream = new EofCallbackStream($original, function () {
Chris@0 277 echo 'EOF!';
Chris@0 278 });
Chris@0 279
Chris@0 280 $eofStream->read(2);
Chris@0 281 $eofStream->read(1);
Chris@0 282 // echoes "EOF!"
Chris@0 283 $eofStream->seek(0);
Chris@0 284 $eofStream->read(3);
Chris@0 285 // echoes "EOF!"
Chris@0 286 ```
Chris@0 287
Chris@0 288
Chris@0 289 ## PHP StreamWrapper
Chris@0 290
Chris@0 291 You can use the `GuzzleHttp\Psr7\StreamWrapper` class if you need to use a
Chris@0 292 PSR-7 stream as a PHP stream resource.
Chris@0 293
Chris@0 294 Use the `GuzzleHttp\Psr7\StreamWrapper::getResource()` method to create a PHP
Chris@0 295 stream from a PSR-7 stream.
Chris@0 296
Chris@0 297 ```php
Chris@0 298 use GuzzleHttp\Psr7\StreamWrapper;
Chris@0 299
Chris@0 300 $stream = GuzzleHttp\Psr7\stream_for('hello!');
Chris@0 301 $resource = StreamWrapper::getResource($stream);
Chris@0 302 echo fread($resource, 6); // outputs hello!
Chris@0 303 ```
Chris@0 304
Chris@0 305
Chris@0 306 # Function API
Chris@0 307
Chris@0 308 There are various functions available under the `GuzzleHttp\Psr7` namespace.
Chris@0 309
Chris@0 310
Chris@0 311 ## `function str`
Chris@0 312
Chris@0 313 `function str(MessageInterface $message)`
Chris@0 314
Chris@0 315 Returns the string representation of an HTTP message.
Chris@0 316
Chris@0 317 ```php
Chris@0 318 $request = new GuzzleHttp\Psr7\Request('GET', 'http://example.com');
Chris@0 319 echo GuzzleHttp\Psr7\str($request);
Chris@0 320 ```
Chris@0 321
Chris@0 322
Chris@0 323 ## `function uri_for`
Chris@0 324
Chris@0 325 `function uri_for($uri)`
Chris@0 326
Chris@0 327 This function accepts a string or `Psr\Http\Message\UriInterface` and returns a
Chris@0 328 UriInterface for the given value. If the value is already a `UriInterface`, it
Chris@0 329 is returned as-is.
Chris@0 330
Chris@0 331 ```php
Chris@0 332 $uri = GuzzleHttp\Psr7\uri_for('http://example.com');
Chris@0 333 assert($uri === GuzzleHttp\Psr7\uri_for($uri));
Chris@0 334 ```
Chris@0 335
Chris@0 336
Chris@0 337 ## `function stream_for`
Chris@0 338
Chris@0 339 `function stream_for($resource = '', array $options = [])`
Chris@0 340
Chris@0 341 Create a new stream based on the input type.
Chris@0 342
Chris@0 343 Options is an associative array that can contain the following keys:
Chris@0 344
Chris@0 345 * - metadata: Array of custom metadata.
Chris@0 346 * - size: Size of the stream.
Chris@0 347
Chris@0 348 This method accepts the following `$resource` types:
Chris@0 349
Chris@0 350 - `Psr\Http\Message\StreamInterface`: Returns the value as-is.
Chris@0 351 - `string`: Creates a stream object that uses the given string as the contents.
Chris@0 352 - `resource`: Creates a stream object that wraps the given PHP stream resource.
Chris@0 353 - `Iterator`: If the provided value implements `Iterator`, then a read-only
Chris@0 354 stream object will be created that wraps the given iterable. Each time the
Chris@0 355 stream is read from, data from the iterator will fill a buffer and will be
Chris@0 356 continuously called until the buffer is equal to the requested read size.
Chris@0 357 Subsequent read calls will first read from the buffer and then call `next`
Chris@0 358 on the underlying iterator until it is exhausted.
Chris@0 359 - `object` with `__toString()`: If the object has the `__toString()` method,
Chris@0 360 the object will be cast to a string and then a stream will be returned that
Chris@0 361 uses the string value.
Chris@0 362 - `NULL`: When `null` is passed, an empty stream object is returned.
Chris@0 363 - `callable` When a callable is passed, a read-only stream object will be
Chris@0 364 created that invokes the given callable. The callable is invoked with the
Chris@0 365 number of suggested bytes to read. The callable can return any number of
Chris@0 366 bytes, but MUST return `false` when there is no more data to return. The
Chris@0 367 stream object that wraps the callable will invoke the callable until the
Chris@0 368 number of requested bytes are available. Any additional bytes will be
Chris@0 369 buffered and used in subsequent reads.
Chris@0 370
Chris@0 371 ```php
Chris@0 372 $stream = GuzzleHttp\Psr7\stream_for('foo');
Chris@0 373 $stream = GuzzleHttp\Psr7\stream_for(fopen('/path/to/file', 'r'));
Chris@0 374
Chris@17 375 $generator = function ($bytes) {
Chris@0 376 for ($i = 0; $i < $bytes; $i++) {
Chris@0 377 yield ' ';
Chris@0 378 }
Chris@0 379 }
Chris@0 380
Chris@0 381 $stream = GuzzleHttp\Psr7\stream_for($generator(100));
Chris@0 382 ```
Chris@0 383
Chris@0 384
Chris@0 385 ## `function parse_header`
Chris@0 386
Chris@0 387 `function parse_header($header)`
Chris@0 388
Chris@0 389 Parse an array of header values containing ";" separated data into an array of
Chris@0 390 associative arrays representing the header key value pair data of the header.
Chris@0 391 When a parameter does not contain a value, but just contains a key, this
Chris@0 392 function will inject a key with a '' string value.
Chris@0 393
Chris@0 394
Chris@0 395 ## `function normalize_header`
Chris@0 396
Chris@0 397 `function normalize_header($header)`
Chris@0 398
Chris@0 399 Converts an array of header values that may contain comma separated headers
Chris@0 400 into an array of headers with no comma separated values.
Chris@0 401
Chris@0 402
Chris@0 403 ## `function modify_request`
Chris@0 404
Chris@0 405 `function modify_request(RequestInterface $request, array $changes)`
Chris@0 406
Chris@0 407 Clone and modify a request with the given changes. This method is useful for
Chris@0 408 reducing the number of clones needed to mutate a message.
Chris@0 409
Chris@0 410 The changes can be one of:
Chris@0 411
Chris@0 412 - method: (string) Changes the HTTP method.
Chris@0 413 - set_headers: (array) Sets the given headers.
Chris@0 414 - remove_headers: (array) Remove the given headers.
Chris@0 415 - body: (mixed) Sets the given body.
Chris@0 416 - uri: (UriInterface) Set the URI.
Chris@0 417 - query: (string) Set the query string value of the URI.
Chris@0 418 - version: (string) Set the protocol version.
Chris@0 419
Chris@0 420
Chris@0 421 ## `function rewind_body`
Chris@0 422
Chris@0 423 `function rewind_body(MessageInterface $message)`
Chris@0 424
Chris@0 425 Attempts to rewind a message body and throws an exception on failure. The body
Chris@0 426 of the message will only be rewound if a call to `tell()` returns a value other
Chris@0 427 than `0`.
Chris@0 428
Chris@0 429
Chris@0 430 ## `function try_fopen`
Chris@0 431
Chris@0 432 `function try_fopen($filename, $mode)`
Chris@0 433
Chris@0 434 Safely opens a PHP stream resource using a filename.
Chris@0 435
Chris@0 436 When fopen fails, PHP normally raises a warning. This function adds an error
Chris@0 437 handler that checks for errors and throws an exception instead.
Chris@0 438
Chris@0 439
Chris@0 440 ## `function copy_to_string`
Chris@0 441
Chris@0 442 `function copy_to_string(StreamInterface $stream, $maxLen = -1)`
Chris@0 443
Chris@0 444 Copy the contents of a stream into a string until the given number of bytes
Chris@0 445 have been read.
Chris@0 446
Chris@0 447
Chris@0 448 ## `function copy_to_stream`
Chris@0 449
Chris@0 450 `function copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)`
Chris@0 451
Chris@0 452 Copy the contents of a stream into another stream until the given number of
Chris@0 453 bytes have been read.
Chris@0 454
Chris@0 455
Chris@0 456 ## `function hash`
Chris@0 457
Chris@0 458 `function hash(StreamInterface $stream, $algo, $rawOutput = false)`
Chris@0 459
Chris@0 460 Calculate a hash of a Stream. This method reads the entire stream to calculate
Chris@0 461 a rolling hash (based on PHP's hash_init functions).
Chris@0 462
Chris@0 463
Chris@0 464 ## `function readline`
Chris@0 465
Chris@0 466 `function readline(StreamInterface $stream, $maxLength = null)`
Chris@0 467
Chris@0 468 Read a line from the stream up to the maximum allowed buffer length.
Chris@0 469
Chris@0 470
Chris@0 471 ## `function parse_request`
Chris@0 472
Chris@0 473 `function parse_request($message)`
Chris@0 474
Chris@0 475 Parses a request message string into a request object.
Chris@0 476
Chris@0 477
Chris@0 478 ## `function parse_response`
Chris@0 479
Chris@0 480 `function parse_response($message)`
Chris@0 481
Chris@0 482 Parses a response message string into a response object.
Chris@0 483
Chris@0 484
Chris@0 485 ## `function parse_query`
Chris@0 486
Chris@0 487 `function parse_query($str, $urlEncoding = true)`
Chris@0 488
Chris@0 489 Parse a query string into an associative array.
Chris@0 490
Chris@0 491 If multiple values are found for the same key, the value of that key value pair
Chris@0 492 will become an array. This function does not parse nested PHP style arrays into
Chris@0 493 an associative array (e.g., `foo[a]=1&foo[b]=2` will be parsed into
Chris@0 494 `['foo[a]' => '1', 'foo[b]' => '2']`).
Chris@0 495
Chris@0 496
Chris@0 497 ## `function build_query`
Chris@0 498
Chris@0 499 `function build_query(array $params, $encoding = PHP_QUERY_RFC3986)`
Chris@0 500
Chris@0 501 Build a query string from an array of key value pairs.
Chris@0 502
Chris@0 503 This function can use the return value of parse_query() to build a query string.
Chris@0 504 This function does not modify the provided keys when an array is encountered
Chris@0 505 (like http_build_query would).
Chris@0 506
Chris@0 507
Chris@0 508 ## `function mimetype_from_filename`
Chris@0 509
Chris@0 510 `function mimetype_from_filename($filename)`
Chris@0 511
Chris@0 512 Determines the mimetype of a file by looking at its extension.
Chris@0 513
Chris@0 514
Chris@0 515 ## `function mimetype_from_extension`
Chris@0 516
Chris@0 517 `function mimetype_from_extension($extension)`
Chris@0 518
Chris@0 519 Maps a file extensions to a mimetype.
Chris@0 520
Chris@0 521
Chris@0 522 # Additional URI Methods
Chris@0 523
Chris@0 524 Aside from the standard `Psr\Http\Message\UriInterface` implementation in form of the `GuzzleHttp\Psr7\Uri` class,
Chris@0 525 this library also provides additional functionality when working with URIs as static methods.
Chris@0 526
Chris@0 527 ## URI Types
Chris@0 528
Chris@0 529 An instance of `Psr\Http\Message\UriInterface` can either be an absolute URI or a relative reference.
Chris@0 530 An absolute URI has a scheme. A relative reference is used to express a URI relative to another URI,
Chris@0 531 the base URI. Relative references can be divided into several forms according to
Chris@0 532 [RFC 3986 Section 4.2](https://tools.ietf.org/html/rfc3986#section-4.2):
Chris@0 533
Chris@0 534 - network-path references, e.g. `//example.com/path`
Chris@0 535 - absolute-path references, e.g. `/path`
Chris@0 536 - relative-path references, e.g. `subpath`
Chris@0 537
Chris@0 538 The following methods can be used to identify the type of the URI.
Chris@0 539
Chris@0 540 ### `GuzzleHttp\Psr7\Uri::isAbsolute`
Chris@0 541
Chris@0 542 `public static function isAbsolute(UriInterface $uri): bool`
Chris@0 543
Chris@0 544 Whether the URI is absolute, i.e. it has a scheme.
Chris@0 545
Chris@0 546 ### `GuzzleHttp\Psr7\Uri::isNetworkPathReference`
Chris@0 547
Chris@0 548 `public static function isNetworkPathReference(UriInterface $uri): bool`
Chris@0 549
Chris@0 550 Whether the URI is a network-path reference. A relative reference that begins with two slash characters is
Chris@0 551 termed an network-path reference.
Chris@0 552
Chris@0 553 ### `GuzzleHttp\Psr7\Uri::isAbsolutePathReference`
Chris@0 554
Chris@0 555 `public static function isAbsolutePathReference(UriInterface $uri): bool`
Chris@0 556
Chris@0 557 Whether the URI is a absolute-path reference. A relative reference that begins with a single slash character is
Chris@0 558 termed an absolute-path reference.
Chris@0 559
Chris@0 560 ### `GuzzleHttp\Psr7\Uri::isRelativePathReference`
Chris@0 561
Chris@0 562 `public static function isRelativePathReference(UriInterface $uri): bool`
Chris@0 563
Chris@0 564 Whether the URI is a relative-path reference. A relative reference that does not begin with a slash character is
Chris@0 565 termed a relative-path reference.
Chris@0 566
Chris@0 567 ### `GuzzleHttp\Psr7\Uri::isSameDocumentReference`
Chris@0 568
Chris@0 569 `public static function isSameDocumentReference(UriInterface $uri, UriInterface $base = null): bool`
Chris@0 570
Chris@0 571 Whether the URI is a same-document reference. A same-document reference refers to a URI that is, aside from its
Chris@0 572 fragment component, identical to the base URI. When no base URI is given, only an empty URI reference
Chris@0 573 (apart from its fragment) is considered a same-document reference.
Chris@0 574
Chris@0 575 ## URI Components
Chris@0 576
Chris@0 577 Additional methods to work with URI components.
Chris@0 578
Chris@0 579 ### `GuzzleHttp\Psr7\Uri::isDefaultPort`
Chris@0 580
Chris@0 581 `public static function isDefaultPort(UriInterface $uri): bool`
Chris@0 582
Chris@0 583 Whether the URI has the default port of the current scheme. `Psr\Http\Message\UriInterface::getPort` may return null
Chris@0 584 or the standard port. This method can be used independently of the implementation.
Chris@0 585
Chris@0 586 ### `GuzzleHttp\Psr7\Uri::composeComponents`
Chris@0 587
Chris@0 588 `public static function composeComponents($scheme, $authority, $path, $query, $fragment): string`
Chris@0 589
Chris@0 590 Composes a URI reference string from its various components according to
Chris@0 591 [RFC 3986 Section 5.3](https://tools.ietf.org/html/rfc3986#section-5.3). Usually this method does not need to be called
Chris@0 592 manually but instead is used indirectly via `Psr\Http\Message\UriInterface::__toString`.
Chris@0 593
Chris@0 594 ### `GuzzleHttp\Psr7\Uri::fromParts`
Chris@0 595
Chris@0 596 `public static function fromParts(array $parts): UriInterface`
Chris@0 597
Chris@0 598 Creates a URI from a hash of [`parse_url`](http://php.net/manual/en/function.parse-url.php) components.
Chris@0 599
Chris@0 600
Chris@0 601 ### `GuzzleHttp\Psr7\Uri::withQueryValue`
Chris@0 602
Chris@0 603 `public static function withQueryValue(UriInterface $uri, $key, $value): UriInterface`
Chris@0 604
Chris@0 605 Creates a new URI with a specific query string value. Any existing query string values that exactly match the
Chris@0 606 provided key are removed and replaced with the given key value pair. A value of null will set the query string
Chris@0 607 key without a value, e.g. "key" instead of "key=value".
Chris@0 608
Chris@17 609 ### `GuzzleHttp\Psr7\Uri::withQueryValues`
Chris@17 610
Chris@17 611 `public static function withQueryValues(UriInterface $uri, array $keyValueArray): UriInterface`
Chris@17 612
Chris@17 613 Creates a new URI with multiple query string values. It has the same behavior as `withQueryValue()` but for an
Chris@17 614 associative array of key => value.
Chris@0 615
Chris@0 616 ### `GuzzleHttp\Psr7\Uri::withoutQueryValue`
Chris@0 617
Chris@0 618 `public static function withoutQueryValue(UriInterface $uri, $key): UriInterface`
Chris@0 619
Chris@0 620 Creates a new URI with a specific query string value removed. Any existing query string values that exactly match the
Chris@0 621 provided key are removed.
Chris@0 622
Chris@0 623 ## Reference Resolution
Chris@0 624
Chris@0 625 `GuzzleHttp\Psr7\UriResolver` provides methods to resolve a URI reference in the context of a base URI according
Chris@0 626 to [RFC 3986 Section 5](https://tools.ietf.org/html/rfc3986#section-5). This is for example also what web browsers
Chris@0 627 do when resolving a link in a website based on the current request URI.
Chris@0 628
Chris@0 629 ### `GuzzleHttp\Psr7\UriResolver::resolve`
Chris@0 630
Chris@0 631 `public static function resolve(UriInterface $base, UriInterface $rel): UriInterface`
Chris@0 632
Chris@0 633 Converts the relative URI into a new URI that is resolved against the base URI.
Chris@0 634
Chris@0 635 ### `GuzzleHttp\Psr7\UriResolver::removeDotSegments`
Chris@0 636
Chris@0 637 `public static function removeDotSegments(string $path): string`
Chris@0 638
Chris@0 639 Removes dot segments from a path and returns the new path according to
Chris@0 640 [RFC 3986 Section 5.2.4](https://tools.ietf.org/html/rfc3986#section-5.2.4).
Chris@0 641
Chris@0 642 ### `GuzzleHttp\Psr7\UriResolver::relativize`
Chris@0 643
Chris@0 644 `public static function relativize(UriInterface $base, UriInterface $target): UriInterface`
Chris@0 645
Chris@0 646 Returns the target URI as a relative reference from the base URI. This method is the counterpart to resolve():
Chris@0 647
Chris@0 648 ```php
Chris@0 649 (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
Chris@0 650 ```
Chris@0 651
Chris@0 652 One use-case is to use the current request URI as base URI and then generate relative links in your documents
Chris@0 653 to reduce the document size or offer self-contained downloadable document archives.
Chris@0 654
Chris@0 655 ```php
Chris@0 656 $base = new Uri('http://example.com/a/b/');
Chris@0 657 echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
Chris@0 658 echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
Chris@0 659 echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
Chris@0 660 echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
Chris@0 661 ```
Chris@0 662
Chris@0 663 ## Normalization and Comparison
Chris@0 664
Chris@0 665 `GuzzleHttp\Psr7\UriNormalizer` provides methods to normalize and compare URIs according to
Chris@0 666 [RFC 3986 Section 6](https://tools.ietf.org/html/rfc3986#section-6).
Chris@0 667
Chris@0 668 ### `GuzzleHttp\Psr7\UriNormalizer::normalize`
Chris@0 669
Chris@0 670 `public static function normalize(UriInterface $uri, $flags = self::PRESERVING_NORMALIZATIONS): UriInterface`
Chris@0 671
Chris@0 672 Returns a normalized URI. The scheme and host component are already normalized to lowercase per PSR-7 UriInterface.
Chris@0 673 This methods adds additional normalizations that can be configured with the `$flags` parameter which is a bitmask
Chris@0 674 of normalizations to apply. The following normalizations are available:
Chris@0 675
Chris@0 676 - `UriNormalizer::PRESERVING_NORMALIZATIONS`
Chris@0 677
Chris@0 678 Default normalizations which only include the ones that preserve semantics.
Chris@0 679
Chris@0 680 - `UriNormalizer::CAPITALIZE_PERCENT_ENCODING`
Chris@0 681
Chris@0 682 All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized.
Chris@0 683
Chris@0 684 Example: `http://example.org/a%c2%b1b` → `http://example.org/a%C2%B1b`
Chris@0 685
Chris@0 686 - `UriNormalizer::DECODE_UNRESERVED_CHARACTERS`
Chris@0 687
Chris@0 688 Decodes percent-encoded octets of unreserved characters. For consistency, percent-encoded octets in the ranges of
Chris@0 689 ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should
Chris@0 690 not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved
Chris@0 691 characters by URI normalizers.
Chris@0 692
Chris@0 693 Example: `http://example.org/%7Eusern%61me/` → `http://example.org/~username/`
Chris@0 694
Chris@0 695 - `UriNormalizer::CONVERT_EMPTY_PATH`
Chris@0 696
Chris@0 697 Converts the empty path to "/" for http and https URIs.
Chris@0 698
Chris@0 699 Example: `http://example.org` → `http://example.org/`
Chris@0 700
Chris@0 701 - `UriNormalizer::REMOVE_DEFAULT_HOST`
Chris@0 702
Chris@0 703 Removes the default host of the given URI scheme from the URI. Only the "file" scheme defines the default host
Chris@0 704 "localhost". All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` are equivalent according to
Chris@0 705 RFC 3986.
Chris@0 706
Chris@0 707 Example: `file://localhost/myfile` → `file:///myfile`
Chris@0 708
Chris@0 709 - `UriNormalizer::REMOVE_DEFAULT_PORT`
Chris@0 710
Chris@0 711 Removes the default port of the given URI scheme from the URI.
Chris@0 712
Chris@0 713 Example: `http://example.org:80/` → `http://example.org/`
Chris@0 714
Chris@0 715 - `UriNormalizer::REMOVE_DOT_SEGMENTS`
Chris@0 716
Chris@0 717 Removes unnecessary dot-segments. Dot-segments in relative-path references are not removed as it would
Chris@0 718 change the semantics of the URI reference.
Chris@0 719
Chris@0 720 Example: `http://example.org/../a/b/../c/./d.html` → `http://example.org/a/c/d.html`
Chris@0 721
Chris@0 722 - `UriNormalizer::REMOVE_DUPLICATE_SLASHES`
Chris@0 723
Chris@0 724 Paths which include two or more adjacent slashes are converted to one. Webservers usually ignore duplicate slashes
Chris@0 725 and treat those URIs equivalent. But in theory those URIs do not need to be equivalent. So this normalization
Chris@0 726 may change the semantics. Encoded slashes (%2F) are not removed.
Chris@0 727
Chris@0 728 Example: `http://example.org//foo///bar.html` → `http://example.org/foo/bar.html`
Chris@0 729
Chris@0 730 - `UriNormalizer::SORT_QUERY_PARAMETERS`
Chris@0 731
Chris@0 732 Sort query parameters with their values in alphabetical order. However, the order of parameters in a URI may be
Chris@0 733 significant (this is not defined by the standard). So this normalization is not safe and may change the semantics
Chris@0 734 of the URI.
Chris@0 735
Chris@0 736 Example: `?lang=en&article=fred` → `?article=fred&lang=en`
Chris@0 737
Chris@0 738 ### `GuzzleHttp\Psr7\UriNormalizer::isEquivalent`
Chris@0 739
Chris@0 740 `public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, $normalizations = self::PRESERVING_NORMALIZATIONS): bool`
Chris@0 741
Chris@0 742 Whether two URIs can be considered equivalent. Both URIs are normalized automatically before comparison with the given
Chris@0 743 `$normalizations` bitmask. The method also accepts relative URI references and returns true when they are equivalent.
Chris@0 744 This of course assumes they will be resolved against the same base URI. If this is not the case, determination of
Chris@0 745 equivalence or difference of relative references does not mean anything.