Skip to content

Commit 6b201b1

Browse files
committed
Update documentation
1 parent 701fcae commit 6b201b1

File tree

6 files changed

+78
-45
lines changed

6 files changed

+78
-45
lines changed

docs/basic-usage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ For each of these top-level types, the package provides a dedicated object to pa
1111
representation of the field and to serialize the value object back to the textual representation.
1212

1313
- 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. The method returns the **normalized string** representation suited for HTTP textual representation.
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.
1515

1616
```php
1717
use Bakame\Http\StructuredFields\Dictionary;
@@ -20,14 +20,14 @@ use Bakame\Http\StructuredFields\OrderedList;
2020

2121
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
2222
echo $dictionary->toHttpValue(); // 'a=?0, b, c;foo=bar'
23-
echo $dictionary; // 'a=?0, b, c;foo=bar'
23+
echo $dictionary; // 'a=?0, b, c;foo=bar'
2424

2525
$list = OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
2626
echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
27-
echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
27+
echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
2828

2929
$item = Item::fromHttpValue('"foo";a=1;b=2');
3030
echo $item->toHttpValue(); // "foo";a=1;b=2
31-
echo $item; // "foo";a=1;b=2
31+
echo $item; // "foo";a=1;b=2
3232
```
3333

docs/containers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use Bakame\Http\StructuredFields\Parameters;
55

66
$parameters = Parameters::fromAssociative(['a' => 1, 'b' => 2, 'c' => "hello world"]);
7-
count($parameters); // returns 3
8-
$parameters->hasMembers(); // returns true
9-
$parameters->toHttpValue(); // returns ';a=1;b=2;c="hello world"'
10-
$parameters->clear()->hasMembers(); // returns false
7+
count($parameters); // returns 3
8+
$parameters->hasMembers(); // returns true
9+
$parameters->toHttpValue(); // returns ';a=1;b=2;c="hello world"'
10+
Paramaters::create()->hasMembers(); // returns false
1111
```
1212

1313
The package exposes [ordered maps](ordered-maps.md)
@@ -27,7 +27,7 @@ At any given time it is possible with each of these objects to:
2727
- iterate over its members using the `IteratorAggregate` interface;
2828
- know the number of members it contains via the `Countable` interface;
2929
- tell whether the container contains or not members with the `hasMembers` methods from the `Container` interface;
30-
- to access the members using the methods exposed by the `ArrayAccess`. **WARNING: Updating using `ArrayAccess` method is forbidden and will result in a `LogicException` being emitted.**;
30+
- access the members using the methods exposed by the `ArrayAccess`. **WARNING: Updating using `ArrayAccess` method is forbidden and will result in a `ForbiddenOperation` being emitted.**;
3131

3232
getter methods:
3333

@@ -36,5 +36,5 @@ getter methods:
3636

3737
**Of note:**
3838

39-
- All setter methods are returns a new instance with the applied changes.
39+
- All setter methods returns a new instance with the applied changes.
4040
- For setter methods, Item types are inferred using `Item::from` if a `Item` object is not provided.

docs/index.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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 and build [HTTP Structured Fields][1] in PHP.
11+
The package uses value objects to parse, serialize, build and update [HTTP Structured Fields][1] in PHP.
1212

1313
HTTP Structured fields are intended for use by specifications of new HTTP fields that wish to
1414
use a common syntax that is more restrictive than traditional HTTP field values or could
@@ -23,9 +23,10 @@ The package can be used to:
2323
use Bakame\Http\StructuredFields\Item;
2424

2525
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
26-
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
27-
echo $field->value(); // display "/terms"
28-
echo $field->parameters->get('rel')->value(); // display "copyright"
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"
2930
```
3031

3132
System Requirements
@@ -47,15 +48,6 @@ or download the library and:
4748
- use any other [PSR-4](https://www.php-fig.org/psr/psr-4/) compatible autoloader.
4849
- use the bundle autoloader script as shown below:
4950

50-
~~~php
51-
require 'path/to/http-structured-fields/repo/autoload.php';
52-
53-
use Bakame\Http\StructuredFields\OrderedList;
54-
55-
$list = OrderedList::fromHttpValue('"/member/*/author", "/member/*/comments"');
56-
echo $list->get(-1)->value(); // returns '/member/*/comments'
57-
~~~
58-
5951
Documentation
6052
------------
6153

docs/item.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,30 @@ To instantiate the class you are required to use the `ByteSequence::fromDecoded`
5555
named constructors. The class also exposes the complementary public methods `ByteSequence::decoded`,
5656
`ByteSequence::encoded` to enable the correct textual representation.
5757

58+
### Date
59+
60+
The Date data type is a special integer as defined in the RFC to represent date as timestamp.
61+
They are represented in the package using PHP's `DateTimeImmutable` object. But the package
62+
supports any `DateTimeInterface` object as long as they are conforming to the RFC supported date range.
63+
64+
```php
65+
use Bakame\Http\StructuredFields\Item;
66+
67+
$date1 = Item::from(new DateTime('today'));
68+
$date2 = Item::from(new DateTimeImmutable('today'));
69+
70+
$date1->value() == $date2->value();
71+
//in both cases the `value` method returns a DateTimeImmutable object.
72+
73+
Item::from(new DateTimeImmutable('@'.PHP_INT_MAX)); // will throw
74+
```
75+
5876
## Usages
5977

6078
Items can be associated with parameters that are an ordered maps of key-value pairs where the
6179
keys are strings and the value are bare items. Their public API is covered in the [ordered maps section](ordered-maps.md].
6280

6381
**An item without any parameter associated to it is said to be a bare item.**
64-
**Exception will be thrown when trying to access or serialize a parameter object containing non-bare items.**
6582

6683
```php
6784
use Bakame\Http\StructuredFields\Item;
@@ -71,10 +88,9 @@ $item->value(); // returns "hello world"
7188
$item->type(); // returns Type::String
7289
$item->parameters()['a']->value(); //returns true
7390
```
74-
7591
Instantiation via type recognition is done using the `Item::from` named constructor.
7692

77-
- The first argument represents one of the six (6) item type value;
93+
- The first argument represents one of the supported type value;
7894
- The second argument, which is optional, MUST be an iterable construct
7995
where its index represents the parameter key and its value an item or an item type value;
8096

@@ -96,8 +112,8 @@ $item = Item::fromPair([
96112
["a", ByteSequence::fromDecoded("Hello World")],
97113
]
98114
]);
99-
$item->value(); // returns "hello world"
100-
$item->type(); // returns Type::String
115+
$item->value(); // returns "hello world"
116+
$item->type(); // returns Type::String
101117
$item->parameters()["a"]->type(); // returns Type::ByteSequence
102118
$item->parameters()["a"]->value(); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
103119
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
@@ -106,13 +122,13 @@ echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
106122
`Item::fromPair` is an alternative to the `Item::from` named constructor, it expects
107123
a tuple composed by an array as a list where:
108124

109-
- The first member on index `0` represents one of the six (6) item type value;
110-
- The second optional member, on index `1`, **MUST** be an iterable construct containing
111-
tuples of key-value pairs;
125+
- The first member on index `0` represents one of the supported item type value;
126+
- The second optional member, on index `1`, **MUST** be an iterable construct containing tuples of key-value pairs;
112127

113128
Once instantiated, accessing `Item` properties is done via:
114129

115-
- the method `Item::value` which returns the instance underlying value fully decoded;
130+
- the method `Item::type` which returns the instance type via the `Type` enum;
131+
- the method `Item::value` which returns the instance underlying value;
116132
- the method `Item::parameters` which returns the parameters associated to the `Item` as a distinct `Parameters` object
117133

118134
```php

docs/lists.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Both classes expose the following:
1717
named constructors:
1818

1919
- `fromList` instantiates the container with a list of members in an iterable construct;
20-
- `new` instantiates the container with a list of members as variadic;
20+
- `from` instantiates the container with a list of members as variadic;
2121

2222
setter methods
2323

@@ -27,6 +27,8 @@ setter methods
2727
- `replace` to replace an element at a given position in the list;
2828
- `remove` to remove elements based on their position;
2929

30+
**All container classes are immutable so change will only be applied on the new instance.**
31+
3032
In addition to `StructuredField` specific interfaces, both classes implements:
3133

3234
- PHP `Countable` interface.
@@ -42,15 +44,19 @@ use Bakame\Http\StructuredFields\Token;
4244
$innerList = InnerList::fromList([42, 42.0, "42"], ["a" => true]);
4345
$innerList->has(2); //return true
4446
$innerList->has(42); //return false
45-
$innerList->push(Token::fromString('forty-two'));
46-
$innerList->remove(0, 2);
47-
echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
4847

49-
$orderedList = OrderedList::from(
48+
$newList = $innerList
49+
->push(Token::fromString('forty-two'));
50+
->remove(0, 2);
51+
52+
echo $innerList; //returns '(42 42.0 "42");a'
53+
echo $newList->toHttpValue(); //returns '(42.0 forty-two);a'
54+
55+
$list = OrderedList::from(
5056
Item::from("42", ["foo" => "bar"]),
51-
$innerList
57+
$newList
5258
);
53-
echo $orderedList->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
59+
echo $list->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
5460
```
5561

5662
**if you try to set a key which does not exist an exception will be
@@ -62,7 +68,15 @@ your logic**
6268
use Bakame\Http\StructuredFields\OrderedList;
6369
use Bakame\Http\StructuredFields\Token;
6470

65-
$innerList = OrderedList::fromList([42, 42.0]);
66-
$innerList->insert(2, Token::fromString('forty-two')); // will throw
67-
echo $innerList->toHttpValue(), PHP_EOL;
71+
$list = OrderedList::fromList([42, 42.0]);
72+
$list->insert(2, Token::fromString('forty-two')); // will throw
73+
```
74+
75+
**Updating using `ArrayAccess` methods is forbidden and will result in a `ForbiddenOperation` being emitted.**
76+
77+
```php
78+
use Bakame\Http\StructuredFields\OrderedList;
79+
80+
$list = OrderedList::fromList([42, 42.0]);
81+
$list[0] = false; // will throw a ForbiddenOperation exception
6882
```

docs/ordered-maps.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ getter methods:
2626

2727
setter methods:
2828

29+
**All container classes are immutable so change will only be applied on the new instance.**
30+
2931
- `add` add an element at the end of the container if the key is new otherwise only the value is updated;
3032
- `append` always add an element at the end of the container, if already present the previous value is removed;
3133
- `prepend` always add an element at the beginning of the container, if already present the previous value is removed;
@@ -66,9 +68,9 @@ In addition to `StructuredField` specific interfaces, both classes implements:
6668
use Bakame\Http\StructuredFields\Parameters;
6769

6870
$parameters = Parameters::fromAssociative(['b' => true, 'foo' => 'bar']);
69-
$parameters->keys(); // returns ['b', 'foo']
70-
$parameters->get('b'); // returns Item::from(true)
71-
$parameters->get('b')->value(); // returns true
71+
$parameters->keys(); // returns ['b', 'foo']
72+
$parameters->get('b'); // returns Item::from(true)
73+
$parameters['b']->value(); // returns true
7274
iterator_to_array($parameters->toPairs(), true); // returns [['b', Item::from(true)], ['foo', Item::from('bar')]]
7375
iterator_to_array($parameters, true); // returns ['b' => Item::from(true), 'foo' => Item::from('bar')]
7476
$parameters->mergeAssociative(
@@ -79,3 +81,12 @@ $parameters->toHttpValue(); // returns ;b="false";foo="foo"
7981
```
8082

8183
**Both classes support negative indexes.**
84+
85+
**Updating using `ArrayAccess` methods is forbidden and will result in a `ForbiddenOperation` being emitted.**
86+
87+
```php
88+
use Bakame\Http\StructuredFields\Dictionary;
89+
90+
$container = Dictionary::create();
91+
$container['zero'] = false; // will throw a ForbiddenOperation exception
92+
```

0 commit comments

Comments
 (0)