PathPartMapper
Maps an input value to a URL-safe path part string (or null).
Accepts the following input types:
string.- Any
Stringableobject. null— left asnull, subject to validation (or used as the trigger for generation, see below).
Non-null values are normalized before validation: StringUtils::clean() trims and collapses whitespace, then mb_strtolower() lowercases the result.
Generation
When the input value is null and a $generationIfNullBaseName is provided, the mapper generates a path part from that base name instead of failing: special characters are stripped, the result is lowercased, padded with -<fillStr> (default path) up to $minlength, and truncated to $maxlength. If a $uniqueTester closure is also given, -N (from 2 up to 9999) is appended until the tester returns true. If no unique value is found, the value becomes null and a Mandatory error is raised.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $uniqueTester | Closure? | null | fn(string $v): bool returning true if $v is available. |
| $generationIfNullBaseName | string? | null | Base name to generate from when the input is null (requires $maxlength≥6). |
| $mandatory | bool | false | Rejects null input (unless generation produces a value). |
| $minlength | int? | 3 | Rejects strings shorter than this. |
| $maxlength | int? | 150 | Rejects strings longer than this. |
noSpecialChars validation is always applied, regardless of $mandatory.
Example: basic normalization
$result = Bind::values('Asdf', null)->map(Mappers::pathPart(null, null))->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
string(4) "asdf"
[1]=>
NULL
}
Example: generating from a base name
$result = Bind::values(null)->map(Mappers::pathPart(fn ($v) => true, 'Blubb', minlength: 8, maxlength: 12))
->toArray()->exec();
var_dump($result->get());
Output:
array(1) {
[0]=>
string(10) "blubb-path"
}
null triggers generation from Blubb; the result is lowercased and padded with -path to reach the minimum length of 8.