Skip to content

Commit f57b6b5

Browse files
committed
Prepare first stable release
1 parent b6a2543 commit f57b6b5

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All Notable changes to `bakame/http-strucured-fields` will be documented in this file.
44

5-
## [Next] - TBD
5+
## [1.0.0] - 2023-04-16
66

77
### Added
88

@@ -267,7 +267,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
267267

268268
**Initial release!**
269269

270-
[Next]: https://github.com/bakame-php/http-structured-fields/compare/0.8.0...master
270+
[Next]: https://github.com/bakame-php/http-structured-fields/compare/1.0.0...master
271+
[1.0.0]: https://github.com/bakame-php/http-structured-fields/compare/0.8.0...1.0.0
271272
[0.8.0]: https://github.com/bakame-php/http-structured-fields/compare/0.7.0...0.8.0
272273
[0.7.0]: https://github.com/bakame-php/http-structured-fields/compare/0.6.0...0.7.0
273274
[0.6.0]: https://github.com/bakame-php/http-structured-fields/compare/0.5.0...0.6.0

README.md

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use Bakame\Http\StructuredFields\OuterList;
1919
use Bakame\Http\StructuredFields\Token;
2020

2121
//1 - parsing an Accept Header
22-
$acceptHeaderValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
23-
$field = OuterList::fromHttpValue($acceptHeaderValue);
22+
$headerValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
23+
$field = OuterList::fromHttpValue($headerValue);
2424
$field[2]->value()->toString(); // returns 'application/xml'
2525
$field[2]->parameter('q'); // returns (float) 0.9
2626
$field[0]->value()->toString(); // returns 'text/html'
@@ -38,7 +38,7 @@ echo OuterList::new(
3838
])
3939
)
4040
->toHttpValue();
41-
// return ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
41+
// returns ("foo" "bar");expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax
4242
```
4343

4444
## System Requirements
@@ -57,15 +57,15 @@ composer require bakame/http-structured-fields
5757

5858
### Foreword
5959

60-
**While this package parses, builds, updates and serializes the header value,
61-
it does not in any shape or form validate its content. It is still required
62-
to validate the parse data against the RFC rules of the corresponding header.
63-
Content validation is out of scope for this library.**
60+
**While this package parses and serializes the header value, it does not validate its content.
61+
It is still required to validate the parsed data against the constraints of the corresponding
62+
header. Content validation is out of scope for this library.**
6463

6564
### Parsing and Serializing Structured Fields
6665

67-
Parsing the header value is done via the `fromHttpValue` named constructor
68-
attached to each library's structured fields representation as shown below:
66+
Parsing the header value is done via the `fromHttpValue` named constructor.
67+
The method is attached to each library's structured fields representation
68+
as shown below:
6969

7070
```php
7171
declare(strict_types=1);
@@ -75,35 +75,32 @@ require 'vendor/autoload.php';
7575
use Bakame\Http\StructuredFields\Item;
7676

7777
// the raw HTTP field value is given by your application
78-
// via any given framework or package or super global.
79-
// We are using a PSR-7 Request object in this example
78+
// via any given framework, package or super global.
8079

81-
$headerLine = $request->getHeaderLine('foo'); // 'foo: bar;baz=42' the raw header line is a structured field item
80+
$headerLine = 'bar;baz=42'; //the raw header line is a structured field item
8281
$field = Item::fromHttpValue($headerLine);
8382
$field->value(); // returns Token::fromString('bar); the found token value
8483
$field->parameter('baz'); // returns 42; the value of the parameter or null if the parameter is not defined.
8584
```
8685

87-
The `fromHttpValue` method returns an instance which implements
88-
the `StructuredField` interface. The interface provides the
89-
`toHttpValue` method that serializes it into a normalized
90-
RFC compliant HTTP field string value. To ease integration,
91-
the `__toString` method is implemented as an alias to the
92-
`toHttpValue` method.
86+
The `fromHttpValue` method returns an instance which implements the `StructuredField` interface.
87+
The interface provides the `toHttpValue` method that serializes it into a normalized RFC
88+
compliant HTTP field string value. To ease integration, the `__toString` method is
89+
implemented as an alias to the `toHttpValue` method.
9390

9491
````php
9592
use Bakame\Http\StructuredFields\Item;
9693

97-
$bar = Item::fromHttpValue('bar; baz=42; secure=?1');
98-
echo $bar->toHttpValue(); // return 'bar;baz=42;secure' on serialization the field has been normalized
94+
$field = Item::fromHttpValue('bar; baz=42; secure=?1');
95+
echo $field->toHttpValue(); // return 'bar;baz=42;secure'
96+
// on serialization the field has been normalized
9997

100-
// the HTTP response object is build by your application
101-
// via your framework, a package or a native PHP function.
102-
// We are using Symfony Response object in this example
98+
// the HTTP response is build by your application
99+
// via any given framework, package or PHP native function.
103100

104-
$newResponse = $response->headers->set('foo', $bar->toHttpValue());
101+
header('foo: '. $field->toHttpValue());
105102
//or
106-
$newResponse = $response->headers->set('foo', $bar);
103+
header('foo: '. $field);
107104
````
108105

109106
All five (5) structured data type as defined in the RFC are provided inside the
@@ -176,16 +173,16 @@ from a string or a string like object**
176173

177174
#### Item
178175

179-
The defined types are all attached to the `Item` object where their values and
180-
types are accessible using the following methods:
176+
The defined types are all attached to an `Item` object where their value and
177+
type are accessible using the following methods:
181178

182179
```php
183180
use Bakame\Http\StructuredFields\Item;
184181
use Bakame\Http\StructuredFields\Type;
185182

186183
$item = Item::fromHttpValue('@1234567890');
187184
$item->type(); // return Type::Date;
188-
$item->value() // return the equivalent to DateTimeImmutable('2009-02-13T23:31:30.000+00:00');
185+
$item->value() // return the equivalent to DateTimeImmutable('@1234567890');
189186
// you can also do
190187
Type::Date->equals($item); // returns true
191188
```
@@ -196,13 +193,15 @@ All containers objects implement PHP `IteratorAggregate`, `Countable` and `Array
196193
interfaces. Their members can be accessed using the following shared methods
197194

198195
```php
199-
$container->keys(): array<string>;
196+
$container->keys(): array<string|int>;
200197
$container->has(string|int ...$offsets): bool;
201198
$container->get(string|int $offset): StrucuredField;
202199
$container->hasMembers(): bool;
203200
$container->hasNoMembers(): bool;
204201
```
205202

203+
**The `get` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.**
204+
206205
To avoid invalid states, `ArrayAccess` modifying methods throw a `ForbiddenOperation`
207206
if you try to use them on any container object:
208207

@@ -225,6 +224,8 @@ $container->pair(int $offset): array{0:string, 1:StructuredField};
225224
$container->toPairs(): iterable<array{0:string, 1:StructuredField}>;
226225
```
227226

227+
**The `pair` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.**
228+
228229
#### Accessing the parameters values
229230

230231
Accessing the associated `Parameters` instance attached to an `InnerList` or a `Item` instances
@@ -241,7 +242,7 @@ InnerList::toPair(): array{0:list<Item>, 1:Parameters}>};
241242
Item::toPair(): array{0:ByteSequence|Token|DateTimeImmutable|Stringable|string|int|float|bool, 1:Parameters}>};
242243
```
243244

244-
**Of note: the `parameter` method will return `null` if no value is found for the given index.**
245+
**The `parameter` method will return `null` if no value is found for the given index.**
245246

246247
### Building and Updating Structured Fields Values
247248

@@ -315,10 +316,12 @@ echo $value->toHttpValue(); //;b=?0;a=bar;c=@1671800423
315316
echo $value; //;b=?0;a=bar;c=@1671800423
316317
```
317318

318-
If the preference is to use the builder pattern, the same result can be achieved with the following steps.
319-
First create a `Parameters` or a `Dictionary` instance using the `new` named constructor which
320-
returns a new instance with no members. And then, use any of the following modifying methods
321-
to populate it.
319+
If the preference is to use the builder pattern, the same result can be achieved with the
320+
following steps:
321+
322+
- First create a `Parameters` or a `Dictionary` instance using the `new` named constructor which
323+
returns a new instance with no members.
324+
- And then, use any of the following modifying methods to populate it.
322325

323326
```php
324327
$map->add(string $key, $value): static;
@@ -350,14 +353,18 @@ echo $value->toHttpValue(); //b=?0, a=(bar "42" 42 42.0), c=@1671800423
350353
echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
351354
```
352355

353-
#### Automatic conversion.
356+
#### Automatic conversion
354357

355358
For all containers, to ease instantiation the following automatic conversion are applied on
356-
the member argument of each modifying methods, if the submitted type is:
359+
the member argument of each modifying methods.
360+
361+
If the submitted type is:
357362

358363
- a `StructuredField` implementing object, it will be passed as is
359364
- an iterable structure, it will be converted to an `InnerList` instance using `InnerList::new`
360-
- otherwise, the method will try to convert it into an `Item` using `Item::new` following the conversion rule in the table above.
365+
- otherwise, it is converted into an `Item` using `Item::new` following the conversion rules explained in the table above.
366+
367+
If no conversion is possible an `InvalidArgument` exception will be thrown.
361368

362369
This means that the previous example can be rewritten like this:
363370

@@ -376,10 +383,12 @@ echo $value->toHttpValue(); //b=?0, a=(bar "42" 42 42.0), c=@1671800423
376383
echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
377384
```
378385

386+
Of course, it is possible to mix both notation as shown in the example.
387+
379388
#### Lists
380389

381-
To Create `OuterList` and `InnerList` instances you can use the `new` named constructor
382-
which takes fron 0 to n members:
390+
To create `OuterList` and `InnerList` instances you can use the `new` named constructor
391+
which takes a single variadic parameter `$members`:
383392

384393
```php
385394
use Bakame\Http\StructuredFields\InnerList;
@@ -395,9 +404,8 @@ echo $list->toHttpValue(); //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
395404
echo $list; //'(:SGVsbG8gV29ybGQ=: 42.0 42)'
396405
```
397406

398-
Once again, the builder pattern can be achieved with a combinason of
399-
using the `new` named constructor and the using any of the following
400-
modifying methods.
407+
Once again, the builder pattern can be used via a combination of the `new`
408+
named constructor and the use any of the following modifying methods.
401409

402410
```php
403411
$list->unshift(...$members): static;

0 commit comments

Comments
 (0)