Skip to content

Commit 1ef58b3

Browse files
committed
Using key instead of name for POLA
1 parent a5bc4fe commit 1ef58b3

18 files changed

+336
-342
lines changed

README.md

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

2525
## System Requirements

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(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
19+
$container[1]->parameterByKey(key: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
2020
```
2121

2222
## Motivation

docs/02-parsing-serializing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ The following field is an example from the Accept header which is already struct
128128
$fieldValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
129129
$field = DataType::List->parse($fieldValue);
130130
$field[2]->value()->toString(); // returns 'application/xml'
131-
$field[2]->parameterByName('q'); // returns (float) 0.9
131+
$field[2]->parameterByKey('q'); // returns (float) 0.9
132132
$field[0]->value()->toString(); // returns 'text/html'
133-
$field[0]->parameterByName('q'); // returns null
133+
$field[0]->parameterByKey('q'); // returns null
134134
```
135135

136136
- The Accept header is an `List` field made of `Item` only.

docs/03-field-values.md

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

183183
$item = Item::fromHttpValue('application/xml;q=0.9;foobar');
184184
$item->value()->toString(); // returns 'application/xhtml+xml'
185-
$item->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
185+
$item->parameterByKey(key: 'q', default: 1.0); // returns 1.0 if the parameter is not defined
186186
$item->parameterByIndex(index: 1, default: ['toto', false]); // returns ['foobar', true] because there's a parameter at index 1
187187
$item->parameters(); // returns a Parameters instance.
188188
```
189189

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

192192
- `Item::parameters` returns a `Parameters` instance;
193-
- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `name`;
193+
- `Item::parameterByKey` returns the value of the bare item instance attached to the supplied `name`;
194194
- `Item::parameterByIndex` returns the value of the bare item instance attached to the supplied `index`;
195195

196196
It is possible to alter and modify the `Parameters` attached to an `Item` but this will be explored in

docs/04-containers.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $permissions->isEmpty(); // returns false
4848

4949
> [!IMPORTANT]
5050
> For ordered maps, the `getByIndex` method returns a list containing exactly 2 entries.
51-
> The first entry is the member name, the second entry is the member value.
51+
> The first entry is the member key, the second entry is the member value.
5252
> For lists, the method directly returns the value.
5353
5454
To avoid invalid states, `ArrayAccess` modifying methods throw a `ForbiddenOperation`
@@ -68,39 +68,39 @@ unset($permissions['a']); // triggers a ForbiddenOperation exception
6868
The `Dictionary` and `Parameters` classes also allow accessing their members as value using their name:
6969

7070
```php
71-
$permissions->hasName('picture-in-picture'); // returns true
72-
$permissions->hasName('picture-in-picture', 'foobar'); // returns false
71+
$permissions->hasKey('picture-in-picture'); // returns true
72+
$permissions->hasKey('picture-in-picture', 'foobar'); // returns false
7373
// 'foobar' is not a valid name or at least it is not present
7474

75-
$permissions->getByName('camera'); // returns Item::fromToken('*');
75+
$permissions->getByKey('camera'); // returns Item::fromToken('*');
7676
$permissions->toAssociative(); // returns an iterator
7777
// the iterator key is the member name and the value is the member value
7878
// the offset is "lost"
79-
$permissions->nameByIndex(42); // returns null because there's no member with the offset 42
80-
$permissions->nameByIndex(2); // returns 'camera'
79+
$permissions->keyByIndex(42); // returns null because there's no member with the offset 42
80+
$permissions->keyByIndex(2); // returns 'camera'
8181

82-
$permissions->indexByName('foobar'): // returns null because there's no member with the name 'foobar'
83-
$permissions->indexByName('geolocation'): // returns 1
82+
$permissions->indexByKey('foobar'): // returns null because there's no member with the name 'foobar'
83+
$permissions->indexByKey('geolocation'): // returns 1
8484
```
8585

8686
> [!IMPORTANT]
87-
> The `getByName` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.
87+
> The `getByKey` method will throw an `InvalidOffset` exception if no member exists for the given `$offset`.
8888
8989
> [!TIP]
9090
> The `ArrayAccess` interface proxy the result from `getByIndex` and `hasIndices` with `OuterList` and `InnerList`.
91-
> The `ArrayAccess` interface proxy the result from `getByName` and `hasNames` with `Dictionary` and `Parameters`.
91+
> The `ArrayAccess` interface proxy the result from `getByNKey` and `hasKeys` with `Dictionary` and `Parameters`.
9292
9393
### Accessing the parameters values
9494

9595
As we have already seen, it is possible to access the `Parameters` values directly
9696
from the `Item` instance. The same public API is used for the `InnerList`.
9797

9898
On the other hand if you already have a `Parameters` instance you can use the
99-
`valueByName` and `valueByIndex` methods to directly access the value from a single
99+
`valueByKey` and `valueByIndex` methods to directly access the value from a single
100100
parameter.
101101

102102
> [!TIP]
103-
> The `parameterByName` proxy the result from `valueByName`.
103+
> The `parameterByKey` proxy the result from `valueByKey`.
104104
> The `parameterByIndex` proxy the result from `valueByIndex`.
105105
106106
## Building and Updating Containers
@@ -149,11 +149,11 @@ following steps. You, first, create a `Parameters` or a `Dictionary` instance us
149149
use any of the following modifying methods to populate it.
150150

151151
```php
152-
$map->add(string $name, $value): static;
153-
$map->append(string $name, $value): static;
154-
$map->prepend(string $name, $value): static;
152+
$map->add(string $key, $value): static;
153+
$map->append(string $key, $value): static;
154+
$map->prepend(string $key, $value): static;
155155
$map->mergeAssociative(...$others): static;
156-
$map->removeByNames(string ...$names): static;
156+
$map->removeByKeys(string ...$keys): static;
157157
```
158158

159159
As shown below:
@@ -183,14 +183,14 @@ using indices and pair as described in the RFC.
183183

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

186-
- the first array member is the parameter `$name`
186+
- the first array member is the parameter `$key`
187187
- the second array member is the parameter `$value`
188188

189189
```php
190190
$map->unshift(array ...$pairs): static;
191191
$map->push(array ...$pairs): static;
192-
$map->insert(int $name, array ...$pairs): static;
193-
$map->replace(int $name, array $pair): static;
192+
$map->insert(int $index, array ...$pairs): static;
193+
$map->replace(int $index, array $pair): static;
194194
$map->mergePairs(...$others): static;
195195
$map->removeByIndices(int ...$indices): static;
196196
```
@@ -223,15 +223,15 @@ echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
223223
> [!CAUTION]
224224
> on duplicate `names` pair values are merged as per RFC logic.
225225
226-
The following methods `removeByIndices` and/or `removeByNames` allow removing members
226+
The following methods `removeByIndices` and/or `removeByKeys` allow removing members
227227
per indices or per names.
228228

229229
```php
230230
use Bakame\Http\StructuredFields\Parameters;
231231

232232
$field = Parameters::fromHttpValue(';expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax');
233233
echo $field->removeByIndices(4, 2, 0)->toHttpValue(); // returns ;path="/";secure;samesite=lax
234-
echo $field->removeByNames('expire', 'httponly', 'max-age')->toHttpValue(); // returns ;path="/";secure;samesite=lax
234+
echo $field->removeByKeys('expire', 'httponly', 'max-age')->toHttpValue(); // returns ;path="/";secure;samesite=lax
235235
```
236236

237237
### Automatic conversion
@@ -396,21 +396,21 @@ You can attach and update the associated `Parameters` instance using the followi
396396

397397
```php
398398
$field->withParameters(Parameters $parameters): static;
399-
$field->addParameter(string $name, mixed $value): static;
400-
$field->appendParameter(string $name, mixed $value): static;
401-
$field->prependParameter(string $name, mixed $value): static;
399+
$field->addParameter(string $key, mixed $value): static;
400+
$field->appendParameter(string $key, mixed $value): static;
401+
$field->prependParameter(string $key, mixed $value): static;
402402
$field->pushParameters(array ...$pairs): static
403403
$field->unshiftParameters(array ...$pairs): static
404404
$field->insertParameters(int $index, array ...$pairs): static
405405
$field->replaceParameter(int $index, array $pair): static
406-
$field->withoutParametersByNames(string ...$names): static
406+
$field->withoutParametersByKeys(string ...$keys): static
407407
$field->withoutParametersByIndices(int ...$indices): static
408408
$field->withoutAnyParameter(): static;
409409
```
410410

411411
The `$pair` parameter is an array as list with exactly two members where:
412412

413-
- the first array member is the parameter `$name`
413+
- the first array member is the parameter `$key`
414414
- the second array member is the parameter `$value`
415415

416416
> [!WARNING]

docs/05-validation.md

Lines changed: 15 additions & 15 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-
- `{name}` the member name (only available with `Dictionary` and `Parameters`)
88+
- `{key}` 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.
@@ -150,33 +150,33 @@ to the item are: `location`, `longitude`, `latitude` and `date`
150150
```php
151151
use Bakame\Http\StructuredFields\Validation\Violation;
152152

153-
if (!$member->parameters()->allowedNames(['location', 'longitude', 'latitude', 'date'])) {
153+
if (!$member->parameters()->allowedKeys(['location', 'longitude', 'latitude', 'date'])) {
154154
throw new Violation('The parameters contains extra names that are not allowed.');
155155
}
156156
```
157157

158158
> [!TIP]
159-
> The `Dictionary` class also exposes an `allowedNames` method which behave the same way.
159+
> The `Dictionary` class also exposes an `allowedKeys` method which behave the same way.
160160
161161
> [!WARNING]
162162
> if the parameters container is empty no error will be triggered
163163
164164
### Validating single parameters
165165

166166
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.
167+
Since in our field there is no mention of offset, we will use the `::parameterByKey` method.
168168

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

171171
Because parameters are optional by default and the `longitude` parameter is required we must
172172
require its presence. So to fully validate the parameter we need to do the following
173173

174174
```php
175-
$member->parameterByName(
175+
$member->parameterByKey(
176176
name: 'longitude',
177177
validate: fn (mixed $value) => match (true) {
178178
Type::Decimal->supports($value) => true,
179-
default => "The `{name}` '{value}' failed the validation check."
179+
default => "The `{key}` '{value}' failed the validation check."
180180
},
181181
required: true,
182182
);
@@ -214,7 +214,7 @@ expects the `Parameters` container as its sole argument.
214214
```php
215215
$parametersValidator = ParametersValidator::new()
216216
->filterByCriteria(function (Parameters $parameters): bool|string {
217-
return $parameters->allowedNames(['location', 'longitude', 'latitude', 'date']);
217+
return $parameters->alloweKeys(['location', 'longitude', 'latitude', 'date']);
218218
});
219219
```
220220

@@ -228,11 +228,11 @@ we did earlier we end up with the following entries.
228228
```php
229229
use Bakame\Http\StructuredFields\Type;
230230

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

238238
return true;
@@ -257,16 +257,16 @@ use Bakame\Http\StructuredFields\Validation\ParametersValidator;
257257
$parametersValidator = ParametersValidator::new()
258258
->filterByCriteria(
259259
fn (Parameters $parameters): bool|string => $parameters
260-
->allowedNames(['location', 'longitude', 'latitude', 'date'])
260+
->allowedKeys(['location', 'longitude', 'latitude', 'date'])
261261
)
262-
->filterByNames([
262+
->filterByKeys([
263263
'location' => [
264264
'validate' => fn (mixed $value) => Type::fromVariable($value)->isOneOf(Type::String, Type::DisplayString),
265265
],
266266
'longitude' => [
267267
'validate' => function (mixed $value) {
268268
if (!Type::Decimal->supports($value)) {
269-
return "The `{name}` '{value}' failed the validation check.";
269+
return "The `{key}` '{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 `{name}` '{value}' failed the validation check.";
279+
return "The `{key}` '{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 `{name}` '{value}' is not a valid date";
289+
return "The `{key}` '{value}' is not a valid date";
290290
}
291291

292292
return true;
@@ -329,7 +329,7 @@ if ($validation->isSucces()) {
329329
> [!NOTE]
330330
> If we only use the `filterByCriteria` method the full parameter data is returned.
331331
332-
A `filterByIndices` method exists and behave exactly as the `filterByNames` method.
332+
A `filterByIndices` method exists and behave exactly as the `filterByKeys` method.
333333
There are two differences when it is used:
334334

335335
- The callback parameters are different (they match those of `parameterByIndex`)

docs/08-migration.md

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This will edit (or create) your `composer.json` file.
2727

2828
### Structured Field Interfaces
2929

30-
All the Interfaces around the structured field data types are remove. So if you typehinted your code using
30+
All the Interfaces around the structured field data types are removed. So if you type-hinted your code using
3131
the interfaces, you will need to replace them by their actual implementation.
3232

3333
- The `MemberOrderedMap` interface will need to be replaced either by `Dictionary` or `Parameters` classes
@@ -45,7 +45,7 @@ facing public API.
4545
+ public static function fromHttpValue(Stringable|string $httpValue, ?Ietf $rfc = null): self
4646
```
4747

48-
In v2, the parser instance is replaced by a enum that indicates which RFC should be used for parsing.
48+
In v2, the parser instance is replaced by an Enum that indicates which RFC should be used for parsing.
4949
If your code did not provide any second parameter to the method then the parsing will be done using `RFC9651`
5050
if you want to only consider the previous active RFC, then you will have to explicitly name it via the `Ietf` Enum.
5151

@@ -54,32 +54,26 @@ if you want to only consider the previous active RFC, then you will have to expl
5454
`Dictionary` and `Parameters` container members can be accessed vi their name or via their index.
5555
To normalize the accessor methods the following changes were introduced
5656

57-
| `1.x` method name | `2.x` method name |
58-
|-------------------------------------|--------------------------------------|
59-
| `Item::parameter` | `Item::parameterByName` |
60-
| `Item::withoutParamtersByKeys` | `Item::withoutParamtersByNames` |
61-
| `InnerList::parameter` | `InnerList::parameterByName` |
62-
| `InnerList::withoutParamtersByKeys` | `InnerList::withoutParamtersByNames` |
63-
| `InnerList::has` | `InnerList::hasIndices` |
64-
| `InnerList::get` | `InnerList::getIndex` |
65-
| `OuterList::get` | `OuterList::getIndex` |
66-
| `OuterList::has` | `OuterList::hasIndices` |
67-
| `Dictionary::has` | `Dictionary::hasNames` |
68-
| `Dictionary::keys` | `Dictionary::names` |
69-
| `Dictionary::get` | `Dictionary::getByName` |
70-
| `Dictionary::removeByKeys` | `Dictionary::removeByNames` |
71-
| `Dictionary::pair` | `Dictionary::getByIndex` |
72-
| `Parameters::has` | `Parameters::hasNames` |
73-
| `Parameters::keys` | `Parameters::names` |
74-
| `Parameters::get` | `Parameters::getByName` |
75-
| `Parameters::pair` | `Parameters::getByIndex` |
76-
| `Parameters::removeByKeys` | `Parameters::removeByNames` |
77-
| `Container::remove` | `Container::removeByIndices` |
78-
| `Container::hasNoMember` | `Container::isEmpty` |
79-
| `Container::hasMembers` | `Container::isNotEmpty` |
57+
| `1.x` method name | `2.x` method name |
58+
|-------------------------------------|------------------------------|
59+
| `Item::parameter` | `Item::parameterByKey` |
60+
| `InnerList::parameter` | `InnerList::parameterByKey` |
61+
| `InnerList::has` | `InnerList::hasIndices` |
62+
| `InnerList::get` | `InnerList::getIndex` |
63+
| `OuterList::get` | `OuterList::getIndex` |
64+
| `OuterList::has` | `OuterList::hasIndices` |
65+
| `Dictionary::has` | `Dictionary::hasKeys` |
66+
| `Dictionary::get` | `Dictionary::getByKey` |
67+
| `Dictionary::pair` | `Dictionary::getByIndex` |
68+
| `Parameters::has` | `Parameters::hasKeys` |
69+
| `Parameters::get` | `Parameters::getByKey` |
70+
| `Parameters::pair` | `Parameters::getByIndex` |
71+
| `Container::remove` | `Container::removeByIndices` |
72+
| `Container::hasNoMember` | `Container::isEmpty` |
73+
| `Container::hasMembers` | `Container::isNotEmpty` |
8074

8175
The `Parameters::remove` and `Dictionary::remove` methods are removed from the public API, they
82-
were accepting `indices` and `names` indiscriminately which may lead to subtle bugs in code.
76+
were accepting `indices` and `keys` indiscriminately which may lead to subtle bugs in code.
8377

8478
## Behaviour changes
8579

0 commit comments

Comments
 (0)