Skip to content

Commit f433ea1

Browse files
committed
Improve documentation and docblocks
1 parent 02d470d commit f433ea1

File tree

2 files changed

+88
-33
lines changed

2 files changed

+88
-33
lines changed

CHANGELOG.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9+
- **[BC Break]** `OrderedList` is renamed `OuterList`.
910
- **[BC Break]** `MemberContainer::remove` methods get added to the interface.
10-
- **[BC Break]** `MemberContainer::has` method accepts a variadic argument. All submitted indexes should be present for the method to return `true`
11-
- **[BC Break]** `MemberContainer::hasPair` method accepts a variadic argument. All submitted indexes should be present for the method to return `true`
11+
- **[BC Break]** `MemberContainer::keys` method added to the interface.
1212

1313
### Fixed
1414

1515
- Migrate to PHPUnit 10
16-
- **[BC Break]** `OrderedList` is renamed `OuterList`.
17-
- **[BC Break]** `ParameterAccess` interface signature updated to use the `Value` interface instead of the `Item` implementation.
1816
- Improve Collection immutability with method changes
17+
- **[BC Break]** `ParameterAccess` interface signature updated to use the `Value` interface instead of the `Item` implementation.
18+
- **[BC Break]** `MemberList::remove`, `MemberOrderedMap::remove` and `MemberOrderedMap::keys` methods are moved to their parent interface.
19+
- **[BC Break]** Renamed arguments for indexation for normalization
20+
- **[BC Break]** `MemberContainer::has` and `MemberOrderedMap::hasPair` methods accept a variadic argument. All submitted indexes/keys should be present for the method to return `true`
1921

2022
### Deprecated
2123

2224
- None
2325

2426
### Removed
2527

26-
- **[BC Break]** `OrderedList` is removed use `OuterList`.
27-
- **[BC Break]** `MemberList::remove` and `MemberOrderedMap::remove` methods are removed from the interface.
28+
- **[BC Break]** `OrderedList` is remove, use `OuterList` instead.
2829

2930
## [0.7.0] - 2023-02-06
3031

README.md

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,22 @@ echo $list[-1]->value(); // returns '/member/*/comments'
5252

5353
### Parsing and Serializing Structured Fields
5454

55-
To parse an HTTP field you may use the `fromHttpValue` named constructor provided by all the
56-
immutable value objects:
55+
To parse the string representation of an HTTP field you must use the `fromHttpValue`
56+
named constructor provided to all the immutable value objects:
5757

5858
```php
5959
use Bakame\Http\StructuredFields\Dictionary;
6060

6161
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
6262
```
6363

64-
The `fromHttpValue` named constructor returns an instance of the `Bakame\Http\StructuredFields\StructuredField` interface.
65-
The interface provides a way to serialize back the object into a normalized RFC compliant HTTP field using the `toHttpValue` method.
64+
`fromHttpValue` returns an instance of the `Bakame\Http\StructuredFields\StructuredField` interface.
65+
The interface provides a way to serialize the value object into a normalized RFC compliant HTTP field
66+
string value using the `toHttpValue` method.
6667

67-
To ease integration with current PHP frameworks and HTTP related packages working with headers and trailers, each value object
68-
also exposes the `Stringable` interface method `__toString`, an alias of the `toHttpValue` method.
68+
To ease integration with current PHP frameworks and packages working with HTTP headers and trailers,
69+
each value object also exposes the `Stringable` interface method `__toString`,
70+
an alias of the `toHttpValue` method.
6971

7072
````php
7173
use Bakame\Http\StructuredFields\OuterList;
@@ -75,18 +77,19 @@ echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
7577
echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
7678
````
7779

78-
The library provides currently five (5) immutable value objects define inside the `Bakame\Http\StructuredFields` namespace
79-
with expose the `StructuredField` interface and expose a `fromHttpValue` named constructor:
80+
The library provides currently five (5) immutable value objects define inside the `Bakame\Http\StructuredFields`
81+
namespace
8082

81-
- an `Item`;
82-
- a 2 Ordered Map Containers `Dictionary` and `Parameters`;
83-
- a 2 List Containers `OuterList` and `InnerList`;
83+
- `Item`;
84+
- `Dictionary` and `Parameters` as 2 Ordered Map Containers;
85+
- `OuterList` and `InnerList` as 2 List Containers ;
8486

85-
### Building and Updating Structured Fields
87+
they all implement the `StructuredField` interface and expose a `fromHttpValue` named constructor:
8688

87-
Creating or updating an HTTP field value can be achieved using any of our immutable value object as a starting point.
89+
### Building and Updating Structured Fields
8890

89-
For instance Ordered Map Containers can be build with an associative iterable as shown below
91+
Every value object can create or update the HTTP field value it represents.
92+
For instance Ordered Map containers can be build with an associative iterable as shown below
9093

9194
```php
9295
use Bakame\Http\StructuredFields\Dictionary;
@@ -138,7 +141,17 @@ echo $value; //"b=?0, a=bar;baz=42, c=@1671800423"
138141
Because we are using immutable value objects any change to the value object will return a new instance with
139142
the changes applied and leave the original instance unchanged.
140143

141-
The same changes can be applied to List Containers but with adapted methods around list handling:
144+
Ordered Map Containers exhibits the following modifying methods:
145+
146+
```php
147+
$map->add($key, $value): static;
148+
$map->append($key, $value): static;
149+
$map->prepend($key, $value): static;
150+
$map->mergeAssociative(...$others): static;
151+
$map->mergePairs(...$others): static;
152+
```
153+
154+
Conversely, changes can be applied to List containers but with adapted methods around list handling:
142155

143156
```php
144157
use Bakame\Http\StructuredFields\InnerList;
@@ -154,25 +167,46 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
154167
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
155168
```
156169

170+
List containers exhibit the following modifying methods:
171+
172+
```php
173+
$list->unshift(...$members): static;
174+
$list->push(...$members): static;
175+
$list->insert($key, ...$members): static;
176+
$list->replace($key, $member): static;
177+
```
178+
179+
Last but not least you can attach and update to `Item` and `InnerList` instances a `Parameters` instance using the
180+
following methods:
181+
182+
```php
183+
$field->withParameters(Parameters $parameters): static;
184+
$field->addParameter($key, $value): static;
185+
$field->appendParameter($key, $value): static;
186+
$field->prependParameter($key, $value): static;
187+
$field->withoutParameters(...$keys): static;
188+
$field->withoutAllParameters($): static;
189+
```
190+
157191
### Item and RFC Data Types
158192

159-
To handle an item, the package provide a specific `Item` value obhject with additional named constructors
193+
To handle an item, the package provide a specific `Item` value object with additional named constructors.
160194
Items can have different types that are translated to PHP using:
161195

162196
- native type where possible
163197
- specific classes defined in the package namespace to represent non-native type
164198

165199
The table below summarizes the item value type.
166200

167-
| HTTP DataType | Package Data Type | Package Enum Type |
168-
|---------------|---------------------------|----------------------|
169-
| Integer | `int` | `Type::Integer` |
170-
| Decimal | `float` | `Type::Decimal` |
171-
| String | `string` | `Tyoe::String` |
172-
| Boolean | `bool` | `Type::Boolean` |
173-
| Token | class `Token` | `Type::Token` |
174-
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` |
175-
| Date | class `DateTimeImmutable` | `Type::Date` |
201+
| HTTP DataType | Package Data Type | Package Enum Type |
202+
|---------------|--------------------------------------------------------|----------------------|
203+
| Integer | `int` | `Type::Integer` |
204+
| Decimal | `float` | `Type::Decimal` |
205+
| String | `string` or `Stringable` class | `Tyoe::String` |
206+
| Boolean | `bool` | `Type::Boolean` |
207+
| Token | class `Token` | `Type::Token` |
208+
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` |
209+
| Date | class `DateTimeImmutable` or `DateTimeInterface` class | `Type::Date` |
176210

177211
```php
178212
use Bakame\Http\StructuredFields\ByteSequence;
@@ -232,10 +266,22 @@ $integer->type(); //return Type::Integer
232266

233267
### Accessing members of Structured Fields Containers.
234268

269+
`Item` are accessible using three (3) methods:
270+
271+
```php
272+
use Bakame\Http\StructuredFields\Item;
273+
274+
$item = Item::from(CarbonImmutable::parse('today'));
275+
$item->type(); // return Type::Date;
276+
$item->value() // return CarbonImmutable::parse('today') (because it extends DateTimeImmutable)
277+
$item->parameters(); // returns a Parameters container
278+
```
279+
235280
All containers implement PHP `IteratorAggregate`, `Countable` and `ArrayAccess` interfaces for easy usage in your codebase.
236281
You also can access container members via the following shared methods
237282

238283
```php
284+
$container->keys(): array<string>;
239285
$container->has(string|int ...$offsets): bool;
240286
$container->get(string|int $offset): bool;
241287
$container->remove(string|int ...$offsets): bool;
@@ -252,11 +298,19 @@ use Bakame\Http\StructuredFields\Item;
252298
$value = Item::from("Hello world", [
253299
'a' => 'foobar',
254300
]);
255-
$value->parameters()->has('b'); // return false
256-
$value->parameters()->has('22'); // return false the index does not exists
301+
$value->parameters()->has('b'); // return false
302+
$value->parameters()['a']; // return Item::from('foobar')
303+
$value->parameters()['a'] = 23 // triggers a ForbiddenOperation exception
304+
unset($value->parameters()['a']); // triggers a ForbiddenOperation exception
257305
```
258306

307+
Apart from the PHP interfaces with the `Dictionary` and the `Parameters` classes you can access the members as pairs:
259308

309+
```php
310+
$container->hasPair(int ...$offsets): bool;
311+
$container->pair(int $offset): array{0:string, 1:StructuredField};
312+
$container->toPairs(): iterable<array{0:string, 1:StructuredField}>;
313+
```
260314

261315
## Contributing
262316

0 commit comments

Comments
 (0)