Skip to content

Commit 814c134

Browse files
committed
Update documentation
1 parent f10f3c8 commit 814c134

File tree

3 files changed

+96
-39
lines changed

3 files changed

+96
-39
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ The package uses value objects to parse, serialize and build [HTTP Structured Fi
1111

1212
HTTP Structured fields are intended for use by specifications of new HTTP fields that wish to
1313
use a common syntax that is more restrictive than traditional HTTP field values or could
14-
be used to [retrofit current headers][2] to have them compliant with the new syntax.
14+
be used to [retrofit current fields][2] to have them compliant with the new syntax.
1515

16-
The package can be used to:
17-
18-
- parse and serialize HTTP Structured Fields
19-
- build or update HTTP Structured Fields in a predicable way;
16+
The package can be used to **parse, build, update and serialize** HTTP Structured Fields in a predicable way;
2017

2118
```php
2219
use Bakame\Http\StructuredFields\Item;
2320

2421
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
25-
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
26-
echo $field->value(); // display "/terms"
22+
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
23+
echo $field->value(); // display "/terms"
2724
echo $field->parameters()['rel']->value(); // display "copyright"
2825
```
2926

docs/basic-usage.md

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
Parsing and Serializing Structured Fields
22
------------
33

4-
an HTTP field value can be:
5-
6-
- an [Item](item.md);
7-
- a [Dictionary](ordered-maps.md);
8-
- a [List](lists.md);
9-
10-
For each of these top-level types, the package provides a dedicated object to parse the textual
11-
representation of the field and to serialize the value object back to the textual representation.
12-
13-
- Parsing is done via a common named constructor `fromHttpValue` which expects the Header or Trailer string value.
14-
- Serializing is done via a common `toHttpValue` public method or using the `__toString` method. The method returns the **normalized string** representation suited for HTTP textual representation.
4+
To parse an HTTP field you may use the `fromHttpValue` named constructor provided by all the
5+
immutable value objects:
156

167
```php
178
use Bakame\Http\StructuredFields\Dictionary;
189
use Bakame\Http\StructuredFields\Item;
10+
use Bakame\Http\StructuredFields\InnerList;
11+
use Bakame\Http\StructuredFields\Parameters;
1912
use Bakame\Http\StructuredFields\OuterList;
2013

14+
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
15+
$item = Item::fromHttpValue('"foo";a=1;b=2');
16+
$innerList = InnerList::fromHttpValue('("hello)world" 42 42.0;john=doe);foo="bar("');
17+
$list = OuterList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
18+
$parameters = Parameters::fromHttpValue(';foo=bar');
19+
```
20+
21+
The `fromHttpValue` named constructor returns an instance of the `Bakame\Http\StructuredFields\StructuredField` interface
22+
which provides a way to serialize back the object into a normalized RFC compliant HTTP field using the `toHttpValue` method.
23+
24+
To ease integration with current PHP frameworks and packages working with HTTP headers and trailers, each value object
25+
also exposes the `Stringable` interface method `__toString` which is an alias of the `toHttpValue` method.
26+
27+
````php
28+
use Bakame\Http\StructuredFields\Dictionary;
29+
use Bakame\Http\StructuredFields\InnerList;
30+
use Bakame\Http\StructuredFields\Value;
31+
2132
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
2233
echo $dictionary->toHttpValue(); // 'a=?0, b, c;foo=bar'
2334
echo $dictionary; // 'a=?0, b, c;foo=bar'
@@ -29,13 +40,73 @@ echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
2940
$item = Item::fromHttpValue('"foo";a=1;b=2');
3041
echo $item->toHttpValue(); // "foo";a=1;b=2
3142
echo $item; // "foo";a=1;b=2
32-
```
43+
44+
$parameters = Parameters::fromHttpValue(' ;foo=bar');
45+
echo $parameters->toHttpValue(); // ;foo=bar
46+
echo $parameters; // ;foo=bar
47+
````
3348

3449
Building and Updating Structured Fields
3550
------------
3651

3752
Updating or creating an HTTP field value can be achieved using any of our immutable value object as a starting point:
3853

54+
The `Dictionary` and the `Parameters` objects can be instantiated using the following named constructors:
55+
56+
```php
57+
use Bakame\Http\StructuredFields\Dictionary;
58+
use Bakame\Http\StructuredFields\Parameters;
59+
60+
Dictionary::fromAssociative($associativeEnumerable): self; // from associative iterable
61+
Parameters::fromAssociative($associativeEnumerable): self; // from associative iterable
62+
63+
Dictionary::fromPairs($enumerableTuple): self; // from pairs as iterable tuple
64+
Parameters::fromPairs($enumerableTuple): self; // from pairs as iterable tuple
65+
66+
Dictionary::create(): self; // an empty container
67+
Parameters::create(): self; // an empty container
68+
```
69+
70+
The `OuterList` and the `InnerList` objects can be instantiated using the following named constructors:
71+
72+
```php
73+
use Bakame\Http\StructuredFields\OuterList;
74+
use Bakame\Http\StructuredFields\InnerList;
75+
use Bakame\Http\StructuredFields\StructuredFields;
76+
77+
OuterList::from(...$members): self; // from a list of structured fields
78+
InnerList::from(...$members): self; // from a list of structured fields
79+
80+
OuterList::fromList($members): self; // from an iterable list of structured fields
81+
InnerList::fromList($members, $parameters): self; // / from an iterable list of structured fields and of parameters
82+
```
83+
84+
The `Item` value object can be instantiated using the following named constructors:
85+
86+
```php
87+
use Bakame\Http\StructuredFields\Item;
88+
89+
Item::from($value, $parameters): self; // from a value and an associate iterable of parameters
90+
Item::fromToken($value, $parameters): self; // a string to convert to a Token and an associate iterable of parameters
91+
Item::fromDecodedByteSequence($value, $parameters): self; // a string to convert to a Byte Sequence and an associate iterable of parameters
92+
Item::fromEncodedByteSequence($value, $parameters): self; // a raw string of encoded Byte Sequence and an associate iterable of parameters
93+
Item::fromPair([$value, $parameters]): self // from a tuple of a value and a pair iterable of parameters
94+
```
95+
96+
The RFC define two (2) specific data types that can not be represented by PHP default type system, for them, we define
97+
two classes `Token` and `ByteSequence` to help representing them in our code.
98+
99+
```php
100+
use Bakame\Http\StructuredFields\Token;
101+
use Bakame\Http\StructuredFields\ByteSequence;
102+
103+
Token::fromString(string|Stringable $value): self; // from a value and an associate iterable of parameters
104+
ByteSequence::fromDecoded($value): self; // a string to convert to a Token and an associate iterable of parameters
105+
ByteSequence::fromEncoded($value): self; // a string to convert to a Byte Sequence and an associate iterable of parameters
106+
```
107+
108+
With this API in mind, it then becomes possible to do the following:
109+
39110
```php
40111
use Bakame\Http\StructuredFields\Dictionary;
41112

@@ -87,4 +158,10 @@ echo $dictBuilder; //"b=?0, a=bar;baz=42;c=@1671800423"
87158
```
88159

89160
In every scenario if the Data Type can be inferred from the PHP type it will get converted into it's
90-
correct data type behind the scene. It is possible to mix the different style if it suits the usage.
161+
correct data type behind the scene. It is possible to mix the different style if it suits the usage.
162+
163+
It is possible to get more information on each value object using the following links:
164+
165+
- an [Item](item.md);
166+
- a [Dictionary](ordered-maps.md);
167+
- a [List](lists.md);

docs/index.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,9 @@ HTTP Structured Fields For PHP
88
[![Total Downloads](https://img.shields.io/packagist/dt/bakame/http-structured-fields.svg?style=flat-square)](https://packagist.org/packages/bakame/http-structured-fields)
99
[![Sponsor development of this project](https://img.shields.io/badge/sponsor%20this%20package-%E2%9D%A4-ff69b4.svg?style=flat-square)](https://github.com/sponsors/nyamsprod)
1010

11-
The package uses value objects to parse, serialize, build and update [HTTP Structured Fields][1] in PHP.
11+
The package provides an expressive, minimal API around the [HTTP Structured Fields RFC][1] in PHP.
12+
It allows the user to quickly parse, serialize, build and update HTTP fields in a predicable way.
1213

13-
HTTP Structured fields are intended for use by specifications of new HTTP fields that wish to
14-
use a common syntax that is more restrictive than traditional HTTP field values or could
15-
be used to [retrofit current headers][2] to have them compliant with the new syntax.
16-
17-
The package can be used to:
18-
19-
- parse and serialize HTTP Structured Fields
20-
- build or update HTTP Structured Fields in a predicable way;
21-
22-
```php
23-
use Bakame\Http\StructuredFields\Item;
24-
25-
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
26-
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
27-
echo $field; // display "/terms";rel="copyright";anchor="#foo"
28-
echo $field->value(); // display "/terms"
29-
echo $field->parameters()['rel']->value(); // display "copyright"
30-
```
3114

3215
System Requirements
3316
-------

0 commit comments

Comments
 (0)