Skip to content

Commit f489594

Browse files
committed
:octocat:
1 parent 24da22e commit f489594

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

README.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# chillerlan/php-http-message-utils
22

3+
A collection of utilities for use with [PSR-7 Message implementations](https://www.php-fig.org/psr/psr-7/).
4+
35
[![PHP Version Support][php-badge]][php]
46
[![version][packagist-badge]][packagist]
57
[![license][license-badge]][license]
68
[![Coverage][coverage-badge]][coverage]
79
[![Scrunitizer][scrutinizer-badge]][scrutinizer]
8-
[![Packagist downloads][downloads-badge]][downloads]<br/>
10+
[![Packagist downloads][downloads-badge]][downloads]
911
[![Continuous Integration][gh-action-badge]][gh-action]
1012

1113
[php-badge]: https://img.shields.io/packagist/php-v/chillerlan/php-http-message-utils?logo=php&color=8892BF
@@ -22,3 +24,71 @@
2224
[downloads]: https://packagist.org/packages/chillerlan/php-http-message-utils/stats
2325
[gh-action-badge]: https://github.com/chillerlan/php-http-message-utils/workflows/CI/badge.svg
2426
[gh-action]: https://github.com/chillerlan/php-http-message-utils/actions/workflows/ci.yml
27+
28+
# Documentation
29+
30+
## Installation
31+
**requires [composer](https://getcomposer.org)**
32+
33+
`composer.json` (note: replace `dev-main` with a [version boundary](https://getcomposer.org/doc/articles/versions.md))
34+
```json
35+
{
36+
"require": {
37+
"php": "^7.4 || ^8.0",
38+
"chillerlan/php-http-message-utils": "dev-main"
39+
}
40+
}
41+
```
42+
Profit!
43+
44+
## API
45+
46+
### `Header`
47+
The `Header` class contains static methods for use with http request and response headers.
48+
49+
method | return | info
50+
------ | ------ | ----
51+
`normalize(array $headers)` | `array` | Normalizes an array of header lines to format `["Name" => "Value (, Value2, Value3, ...)", ...]` An exception is being made for `Set-Cookie`, which holds an array of values for each cookie. For multiple cookies with the same name, only the last value will be kept.
52+
53+
### `Query`
54+
The `Query` class contains static methods for use with http query strings.
55+
56+
method | return | info
57+
------ | ------ | ----
58+
`cleanParams(iterable $params, int $bool_cast = null, bool $remove_empty = null)` | `array` | Cleans/normalizes an array of query parameters, booleans will be converted according to the given `$bool_cast` constant. By default, booleans will be left as-is (`Query::BOOLEANS_AS_BOOL`) and may result in empty values. If `$remove_empty` is set to true, empty non-boolean and null values will be removed from the array. The `Query` class provides the following constants for `$bool_cast`:<br>`BOOLEANS_AS_BOOL`: unchanged boolean value (default)<br>`BOOLEANS_AS_INT`: integer values 0 or 1<br>`BOOLEANS_AS_STRING`: "true"/"false" strings<br>`BOOLEANS_AS_INT_STRING`: "0"/"1" strings
59+
`build(array $params, int $encoding = null, string $delimiter = null, string $enclosure = null)` | `string` | Builds a query string from an array of key value pairs, similar to [`http_build_query`](https://www.php.net/manual/en/function.http-build-query). Valid values for `$encoding` are `PHP_QUERY_RFC3986` (default) and `PHP_QUERY_RFC1738`, any other integer value will be interpreted as "no encoding" (`Query::NO_ENCODING`).
60+
`merge(string $uri, array $query)` | `string` | Merges additional query parameters into an existing query string.
61+
`parse(string $querystring, int $urlEncoding = null)` | `array` | Parses a query string into an associative array, similar to [`parse_str`](https://www.php.net/manual/en/function.parse-str) (without the inconvenient usage of a by-reference result variable).
62+
63+
### `Server`
64+
The `Server` object requires a set of [PSR-17 factories](https://www.php-fig.org/psr/psr-17/) on invocation, namely `ServerRequestFactoryInterface`, `UriFactoryInterface`, `UploadedFileFactoryInterface` and `StreamFactoryInterface`.
65+
It provides convenience methods to create server requests, URIs and uploaded files from the [superglobals](https://www.php.net/manual/en/language.variables.superglobals.php).
66+
67+
method | return | info
68+
------ | ------ | ----
69+
`createServerRequestFromGlobals()` | `ServerRequestInterface` | Returns a ServerRequest object populated from the superglobals `$_GET`, `$_POST`, `$_COOKIE`, `$_FILES` and `$_SERVER`.
70+
`createUriFromGlobals()` | `UriInterface` | Creates an Uri populated with values from [`$_SERVER`](https://www.php.net/manual/en/reserved.variables.server) (`HTTP_HOST`, `SERVER_NAME`, `SERVER_ADDR`, `SERVER_PORT`, `REQUEST_URI`, `QUERY_STRING`).
71+
`normalizeFiles(array $files)` | `UploadedFileInterface[]` | Returns an `UploadedFile` instance array.
72+
`createUploadedFileFromSpec(array $value)` | `UploadedFileInterface` or `UploadedFileInterface[]` | Creates an UploadedFile instance from a $_FILES specification. If the specification represents an array of values, this method will delegate to `normalizeNestedFileSpec()` and return that return value.
73+
`normalizeNestedFileSpec(array $files):array` | `array` | Normalizes an array of file specifications. Loops through all nested files and returns a normalized array of `UploadedFileInterface` instances.
74+
75+
### Functions
76+
The namespace `chillerlan\HTTP\Utils` contains several functions for various operations with message objects.
77+
78+
function | return | info
79+
------ | ------ | ----
80+
`getMimetypeFromExtension(string $extension)` | `?string` | Get the mime type for the given file extension (checks against the constant `chillerlan\HTTP\Utils\MIMETYPES`, a list of mime types from the [apache default config](http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types))
81+
`getMimetypeFromFilename(string $filename)` | `?string` | Get the mime type from a file name
82+
`r_rawurlencode($data)` | mixed | Recursive [`rawurlencode`](https://www.php.net/manual/en/function.rawurlencode)
83+
`get_json(MessageInterface $message, bool $assoc = null)` | mixed | fetches the body of a `MessageInterface` and converts it to a JSON object (`stdClass`) or an associative array if `$assoc` is set to `true` and returns the result.
84+
`get_xml(MessageInterface $message, bool $assoc = null)` | mixed | fetches the body of a `MessageInterface` and converts it to a `SimpleXMLElement` or an associative array if `$assoc` is set to `true` and returns the result.
85+
`message_to_string(MessageInterface $message)` | `string` | Returns the string representation of an HTTP message.
86+
`decompress_content(MessageInterface $message)` | `string` | Decompresses the message content according to the `Content-Encoding` header (`compress`, `deflate`, `gzip`, `br`) and returns the decompressed data. For a [`br` (Brotli)](https://en.wikipedia.org/wiki/Brotli) extension see: https://github.com/kjdev/php-ext-brotli
87+
`uriIsDefaultPort(UriInterface $uri)` | `bool` | Checks whether the `UriInterface` has a port set and if that port is one of the default ports for the given scheme.
88+
`uriIsAbsolute(UriInterface $uri)` | `bool` | Checks Whether the URI is absolute, i.e. it has a scheme.
89+
`uriIsNetworkPathReference(UriInterface $uri)` | `bool` | Checks Whether the URI is a network-path reference.
90+
`uriIsAbsolutePathReference(UriInterface $uri)` | `bool` | Checks Whether the URI is a absolute-path reference.
91+
`uriIsRelativePathReference(UriInterface $uri)` | `bool` | Checks Whether the URI is a relative-path reference.
92+
`uriWithoutQueryValue(UriInterface $uri, string $key)` | `UriInterface` | Removes a specific query string value. Any existing query string values that exactly match the provided `$key` are removed.
93+
`uriWithQueryValue(UriInterface $uri, string $key, string $value = null)` | `UriInterface` | Adds a specific query string value. Any existing query string values that exactly match the provided `$key` are removed and replaced with the given `$key`-`$value` pair. A value of null will set the query string key without a value, e.g. "key" instead of "key=value".
94+
`parseUrl(string $url)` | `?array` | UTF-8 aware \parse_url() replacement.

0 commit comments

Comments
 (0)