Skip to content

Commit a4c8190

Browse files
committed
Update documentation v2
1 parent 6a99128 commit a4c8190

File tree

10 files changed

+48
-42
lines changed

10 files changed

+48
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ composer require bakame/http-structured-fields
3636

3737
## Documentation
3838

39-
- The documentation for [version 2.x](/docs/00-intro.md) is a **work in progress**
39+
- The documentation for [version 2.x](/docs/00-intro.md) for **the upcoming v2 release**
4040
- The documentation for [version 1.x](https://github.com/bakame-php/http-structured-fields/tree/1.x) for the current stable version.
4141

4242
## Contributing

docs/00-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use Bakame\Http\StructuredFields\DataType;
1616
$fieldValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
1717
$container = DataType::List->parse($fieldValue);
1818
$container[1]->value()->toString(); // returns 'application/xhtml+xml'
19-
$container[1]->parameterByName(key: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
19+
$container[1]->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
2020
```
2121

2222
## Motivation

docs/04-item.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ They can be accessed by their indices **but also** by their required key attache
6464

6565
$item = Item::fromHttpValue('application/xml;q=0.9;foobar');
6666
$item->value()->toString(); // returns 'application/xhtml+xml'
67-
$item->parameterByName(key: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
67+
$item->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
6868
$item->parameterByIndex(index: 1, default: ['toto', false]); // returns ['foobar', true] because there's a parameter at index 1
6969
$item->parameters(); // returns a Parameters instance.
7070
```
7171

7272
By default, you can access the member `Item` of a parameters using the following methods:
7373

7474
- `Item::parameters` returns a `Parameters` instance;
75-
- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `key`;
75+
- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `name`;
7676
- `Item::parameterByIndex` returns the value of the bare item instance attached to the supplied `index`;
7777

7878
It is possible to alter and modify the `Parameters` attached to an `Item` but this section

docs/05-containers.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $permissions->isEmpty(); // returns false
3737
3838
> [!IMPORTANT]
3939
> For ordered maps, the `getByIndex` method returns a list containing exactly 2 entries.
40-
> The first entry is the member key, the second entry is the member value.
40+
> The first entry is the member name, the second entry is the member value.
4141
> For lists, the method directly returns the value.
4242
4343
To avoid invalid states, `ArrayAccess` modifying methods throw a `ForbiddenOperation`
@@ -51,24 +51,24 @@ unset($permissions['a']); // triggers a ForbiddenOperation exception
5151
```
5252

5353
> [!IMPORTANT]
54-
> For ordered map the ArrayAccess interface will use the member key
54+
> For ordered map the ArrayAccess interface will use the member name
5555
> whereas for lists the interface will use the member index.
5656
57-
The `Dictionary` and `Parameters` classes also allow accessing its members as value using their key:
57+
The `Dictionary` and `Parameters` classes also allow accessing its members as value using their name:
5858

5959
```php
6060
$permissions->hasName('picture-in-picture'); // returns true
6161
$permissions->hasName('picture-in-picture', 'foobar'); // returns false
62-
// 'foobar' is not a valid key or at least it is not present
62+
// 'foobar' is not a valid name or at least it is not present
6363

6464
$permissions->getByName('camera'); // returns Item::fromToken('*');
6565
$permissions->toAssociative(); // returns an iterator
66-
// the iterator key is the member key and the value is the member value
66+
// the iterator key is the member name and the value is the member value
6767
// the offset is "lost"
6868
$permissions->nameByIndex(42); // returns null because there's no member with the offset 42
6969
$permissions->nameByIndex(2); // returns 'camera'
7070

71-
$permissions->indexByName('foobar'): // returns null because there's no member with the key 'foobar'
71+
$permissions->indexByName('foobar'): // returns null because there's no member with the name 'foobar'
7272
$permissions->indexByName('geolocation'): // returns 1
7373
```
7474

@@ -89,7 +89,7 @@ On the other hand if you already have a `Parameters` instance you can use the
8989
parameter.
9090

9191
> [!TIP]
92-
> The `parameterByKey` proxy the result from `valueByName`.
92+
> The `parameterByName` proxy the result from `valueByName`.
9393
> The `parameterByIndex` proxy the result from `valueByIndex`.
9494
9595
## Building and Updating Structured Fields Values
@@ -172,14 +172,14 @@ using indices and pair as described in the RFC.
172172

173173
The `$pair` parameter is a tuple (ie: an array as list with exactly two members) where:
174174

175-
- the first array member is the parameter `$key`
175+
- the first array member is the parameter `$name`
176176
- the second array member is the parameter `$value`
177177

178178
```php
179179
$map->unshift(array ...$pairs): static;
180180
$map->push(array ...$pairs): static;
181-
$map->insert(int $key, array ...$pairs): static;
182-
$map->replace(int $key, array $pair): static;
181+
$map->insert(int $name, array ...$pairs): static;
182+
$map->replace(int $name, array $pair): static;
183183
$map->mergePairs(...$others): static;
184184
$map->removeByIndices(int ...$indices): static;
185185
```
@@ -292,9 +292,9 @@ named constructor and the use any of the following modifying methods.
292292
```php
293293
$list->unshift(...$members): static;
294294
$list->push(...$members): static;
295-
$list->insert(int $key, ...$members): static;
296-
$list->replace(int $key, $member): static;
297-
$list->removeByIndices(int ...$key): static;
295+
$list->insert(int $index, ...$members): static;
296+
$list->replace(int $index, $member): static;
297+
$list->removeByIndices(int ...$index): static;
298298
```
299299

300300
as shown below
@@ -387,7 +387,7 @@ You can attach and update the associated `Parameters` instance using the followi
387387
$field->addParameter(string $name, mixed $value): static;
388388
$field->appendParameter(string $name, mixed $value): static;
389389
$field->prependParameter(string $name, mixed $value): static;
390-
$field->withoutParameters(string ...$names): static; // this method is deprecated as of version 1.1 use withoutParametersByKeys instead
390+
$field->withoutParametersByNames(string ...$names): static;
391391
$field->withoutAnyParameter(): static;
392392
$field->withParameters(Parameters $parameters): static;
393393
$field->pushParameters(array ...$pairs): static
@@ -400,7 +400,7 @@ $field->withoutParametersByIndices(int ...$indices): static
400400

401401
The `$pair` parameter is a tuple (ie: an array as list with exactly two members) where:
402402

403-
- the first array member is the parameter `$key`
403+
- the first array member is the parameter `$name`
404404
- the second array member is the parameter `$value`
405405

406406
> [!WARNING]

docs/06-validation.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ following variables:
8585

8686
- `{index}` the member index
8787
- `{value}` the member value in its serialized version
88-
- `{key}` the member key (only available with `Dictionary` and `Parameters`)
88+
- `{name}` the member name (only available with `Dictionary` and `Parameters`)
8989

9090
Now that we know how to discriminate between an `InnerList` and a `Item` we want to validate
9191
the `Item` entry.
@@ -163,8 +163,8 @@ if (!$member->parameters()->allowedNames(['location', 'longitude', 'latitude', '
163163
164164
### Validating single parameters
165165

166-
The `parameterByKey` and `parameterByIndex` methods can be used to validate a parameter value.
167-
Since in our field there is no mention of offset, we will use the `::parameterByKey` method.
166+
The `parameterByName` and `parameterByIndex` methods can be used to validate a parameter value.
167+
Since in our field there is no mention of offset, we will use the `::parameterByName` method.
168168

169169
Let's try to validate the `longitude` parameter
170170

@@ -173,18 +173,18 @@ require its presence. So to fully validate the parameter we need to do the follo
173173

174174
```php
175175
$member->parameterByName(
176-
key: 'longitude',
176+
name: 'longitude',
177177
validate: fn (mixed $value) => match (true) {
178178
Type::Decimal->supports($value) => true,
179-
default => "The `{key}` '{value}' failed the validation check."
179+
default => "The `{name}` '{value}' failed the validation check."
180180
},
181181
required: true,
182182
);
183183
```
184184

185185
> [!NOTE]
186186
> `parameterByIndex` uses the same parameter only the callback parameter are
187-
> different as a second parameter the string key is added to the callback
187+
> different as a second parameter the string name is added to the callback
188188
> for validation purpose.
189189
190190
### Validating the complete Parameter container
@@ -220,9 +220,9 @@ $parametersValidator = ParametersValidator::new()
220220

221221
The `ParametersValidator` class is immutable so each added rules returns a new instance.
222222

223-
Then we can add all the key checks using an associative `array` where each entry index
224-
will be the parameter `key` and each entry value will also be an array which takes
225-
the parameters of the `parameterByKey` method. For instance for the `longitude` parameter
223+
Then we can add all the name checks using an associative `array` where each entry index
224+
will be the parameter `name` and each entry value will also be an array which takes
225+
the parameters of the `parameterByName` method. For instance for the `longitude` parameter
226226
we did earlier we end up with the following entries.
227227

228228
```php
@@ -232,7 +232,7 @@ $parametersValidator = ->filterByNames([
232232
'longitude' => [
233233
'validate' => function (mixed $value) {
234234
if (!Type::Decimal->supports($value)) {
235-
return "The `{key}` '{value}' failed the validation check.";
235+
return "The `{name}` '{value}' failed the validation check.";
236236
}
237237

238238
return true;
@@ -266,7 +266,7 @@ $parametersValidator = ParametersValidator::new()
266266
'longitude' => [
267267
'validate' => function (mixed $value) {
268268
if (!Type::Decimal->supports($value)) {
269-
return "The `{key}` '{value}' failed the validation check.";
269+
return "The `{name}` '{value}' failed the validation check.";
270270
}
271271

272272
return true;
@@ -276,7 +276,7 @@ $parametersValidator = ParametersValidator::new()
276276
'latitude' => [
277277
'validate' => function (mixed $value) {
278278
if (!Type::Decimal->supports($value)) {
279-
return "The `{key}` '{value}' failed the validation check.";
279+
return "The `{name}` '{value}' failed the validation check.";
280280
}
281281

282282
return true;
@@ -286,7 +286,7 @@ $parametersValidator = ParametersValidator::new()
286286
'date' => [
287287
'validate' => function (mixed $value) {
288288
if (!Type::Date->supports($value)) {
289-
return "The `{key}` '{value}' is not a valid date";
289+
return "The `{name}` '{value}' is not a valid date";
290290
}
291291

292292
return true;

docs/07-extensions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ On the other hand to allow composition the package does expose the `StructuredFi
3434
interface StructuredFieldProvider
3535
{
3636
/**
37-
* Returns ane of the StructuredField Data Type class.
37+
* Returns one of the StructuredField Data Type class.
3838
*/
39-
public function toStructuredField(): OuterList|Dictionary|Item|InnerList|Parameters;
39+
public function toStructuredField(): Dictionary|InnerList|Item|OuterList|Parameters;
4040
}
4141
```
4242

src/Dictionary.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ private static function filterMember(mixed $member): InnerList|Item
5959
}
6060

6161
return match (true) {
62-
$member instanceof InnerList || $member instanceof Item => $member,
63-
$member instanceof OuterList || $member instanceof Dictionary || $member instanceof Parameters => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
62+
$member instanceof InnerList,
63+
$member instanceof Item => $member,
64+
$member instanceof OuterList,
65+
$member instanceof Dictionary,
66+
$member instanceof Parameters => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
6467
is_iterable($member) => InnerList::new(...$member),
6568
default => Item::new($member),
6669
};

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function removeByIndices(int ...$indices): self
351351
count($indices) === $max => self::new(),
352352
default => new self(array_filter(
353353
$this->members,
354-
fn (int $key): bool => !in_array($key, $indices, true),
354+
fn (int $offset): bool => !in_array($offset, $indices, true),
355355
ARRAY_FILTER_USE_KEY
356356
), $this->parameters),
357357
};

src/OuterList.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ private function filterMember(mixed $member): InnerList|Item
6060
}
6161

6262
return match (true) {
63-
$member instanceof InnerList || $member instanceof Item => $member,
64-
$member instanceof OuterList || $member instanceof Parameters || $member instanceof Dictionary => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
63+
$member instanceof InnerList,
64+
$member instanceof Item => $member,
65+
$member instanceof OuterList,
66+
$member instanceof Parameters,
67+
$member instanceof Dictionary => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
6568
is_iterable($member) => InnerList::new(...$member),
6669
default => Item::new($member),
6770
};

src/StructuredFieldProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use DateTimeInterface;
99

1010
/**
11-
* @phpstan-type StructuredField OuterList|Dictionary|InnerList|Parameters|Item
11+
* @phpstan-type StructuredField Dictionary|InnerList|Item|OuterList|Parameters
1212
* @phpstan-type SfType ByteSequence|Token|DisplayString|DateTimeImmutable|string|int|float|bool
1313
* @phpstan-type SfTypeInput SfType|DateTimeInterface
1414
* @phpstan-type SfItemInput Item|SfTypeInput|StructuredFieldProvider|StructuredField
@@ -20,7 +20,7 @@
2020
interface StructuredFieldProvider
2121
{
2222
/**
23-
* Returns ane of the StructuredField Data Type class.
23+
* Returns one of the StructuredField Data Type class.
2424
*/
25-
public function toStructuredField(): OuterList|Dictionary|Item|InnerList|Parameters;
25+
public function toStructuredField(): Dictionary|InnerList|Item|OuterList|Parameters;
2626
}

0 commit comments

Comments
 (0)