Skip to content

Commit

Permalink
Update documentation v2
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 19, 2024
1 parent 56da217 commit 79424e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions docs/05-containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ with the changes applied and leave the original instance unchanged.

### Ordered Maps

The `Dictionary` and `Parameters` are ordered map instances. They can be built using their keys with an
The `Dictionary` and `Parameters` are ordered map instances. They can be built using their names with an
associative iterable structure as shown below

```php
Expand Down Expand Up @@ -142,7 +142,7 @@ $map->add(string $name, $value): static;
$map->append(string $name, $value): static;
$map->prepend(string $name, $value): static;
$map->mergeAssociative(...$others): static;
$map->removeByKeys(string ...$names): static;
$map->removeByNames(string ...$names): static;
```

As shown below:
Expand Down Expand Up @@ -210,17 +210,17 @@ echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
```

> [!CAUTION]
> on duplicate `keys` pair values are merged as per RFC logic.
> on duplicate `names` pair values are merged as per RFC logic.
The following methods `removeByIndices` and/or `removeByKeys` allow removing members
per indices or per keys.
The following methods `removeByIndices` and/or `removeByNames` allow removing members
per indices or per names.

```php
use Bakame\Http\StructuredFields\Parameters;

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

### Automatic conversion
Expand Down Expand Up @@ -387,14 +387,14 @@ You can attach and update the associated `Parameters` instance using the followi
$field->addParameter(string $name, mixed $value): static;
$field->appendParameter(string $name, mixed $value): static;
$field->prependParameter(string $name, mixed $value): static;
$field->withoutParameters(string ...$keys): static; // this method is deprecated as of version 1.1 use withoutParametersByKeys instead
$field->withoutParameters(string ...$names): static; // this method is deprecated as of version 1.1 use withoutParametersByKeys instead
$field->withoutAnyParameter(): static;
$field->withParameters(Parameters $parameters): static;
$field->pushParameters(array ...$pairs): static
$field->unshiftParameters(array ...$pairs): static
$field->insertParameters(int $index, array ...$pairs): static
$field->replaceParameter(int $index, array $pair): static
$field->withoutParametersByKeys(string ...$keys): static
$field->withoutParametersByNames(string ...$names): static
$field->withoutParametersByIndices(int ...$indices): static
```

Expand Down
26 changes: 13 additions & 13 deletions docs/06-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,26 @@ $value = $member
### Validating the Item parameters.

### Checking for allowed keys
### Checking for allowed names

Before validating the content of the `Parameters` container we need to make
sure the container contains the proper data. That all the allowed keys are
present. To do so we can use the `Parameters::allowedKeys` method. This
method expects a list of keys. If other keys not present in the
sure the container contains the proper data. That all the allowed names are
present. To do so we can use the `Parameters::allowedNames` method. This
method expects a list of names. If other names not present in the
list are found in the container the method will return `false`. If we
go back to our definition. We know that the allowed parameters keys attached
go back to our definition. We know that the allowed parameters names attached
to the item are: `location`, `longitude`, `latitude` and `date`

```php
use Bakame\Http\StructuredFields\Validation\Violation;

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

> [!TIP]
> The `Dictionary` class also exposes an `allowedKeys` method which behave the same way.
> The `Dictionary` class also exposes an `allowedNames` method which behave the same way.
> [!WARNING]
> if the parameters container is empty no error will be triggered
Expand Down Expand Up @@ -208,13 +208,13 @@ all the criteria.
Going back to the HTTP field definitions we can translate the requirements and create the
following `ParametersValidator`.

We need to make sure about the allowed keys for that. the class has a `filterByCriteria` which
We need to make sure about the allowed names for that. the class has a `filterByCriteria` which
expects the `Parameters` container as its sole argument.

```php
$parametersValidator = ParametersValidator::new()
->filterByCriteria(function (Parameters $parameters): bool|string {
return $parameters->allowedKeys(['location', 'longitude', 'latitude', 'date']);
return $parameters->allowedNames(['location', 'longitude', 'latitude', 'date']);
});
```

Expand All @@ -228,7 +228,7 @@ we did earlier we end up with the following entries.
```php
use Bakame\Http\StructuredFields\Type;

$parametersValidator = ->filterByKeys([
$parametersValidator = ->filterByNames([
'longitude' => [
'validate' => function (mixed $value) {
if (!Type::Decimal->supports($value)) {
Expand All @@ -242,7 +242,7 @@ $parametersValidator = ->filterByKeys([
]);
```

We can do the same for all the other keys, the available parameters are:
We can do the same for all the other names, the available parameters are:
- `validate`: the callback used for validation; `null` by default
- `required`: a boolean telling whether the parameter presence is required; `false` by default
- `default`: the default value if the parameter is optional; `null` by default.
Expand Down Expand Up @@ -329,7 +329,7 @@ if ($validation->isSucces()) {
> [!NOTE]
> If we only use the `filterByCriteria` method the full parameter data is returned.
A `filterByIndices` method exists and behave exactly as the `filterByKeys` method.
A `filterByIndices` method exists and behave exactly as the `filterByNames` method.
There are two differences when it is used:

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

0 comments on commit 79424e4

Please sign in to comment.