CleanStringMapper
Maps an input value to a cleaned string (or null).
Accepts the following input types:
string.- Any
Stringableobject. null— left asnull, subject to validation.
Non-null values are cleaned before validation: StringUtils::clean() trims, collapses repeated whitespace and strips non-printable control characters. With $simpleWhitespacesOnly = true (the default, used by Mappers::cleanString()) all whitespace is normalized to single spaces. Mappers::cleanMultilineString() passes false, which preserves line breaks so multi-line text keeps its structure.
Validation possibilities
| Parameter | Type | Default | Description |
|---|---|---|---|
| $mandatory | bool | false | Rejects null input. |
| $minlength | int? | 1 | Rejects strings shorter than this. |
| $maxlength | int? | 255 | Rejects strings longer than this. |
| $simpleWhitespacesOnly | bool | true | Whitespace normalization mode (see above). |
Example: basic cleaning
$result = Bind::values(' Hello World ', null)->map(Mappers::cleanString())->toArray()->exec();
var_dump($result->get());
Output:
array(2) {
[0]=>
string(11) "Hello World"
[1]=>
NULL
}
Example: mandatory and length validation
$result = Bind::attrs(['title' => str_repeat('A', 256), 'name' => 'a', 'missing' => null])
->props(['title', 'name', 'missing'], Mappers::cleanString(true, 11, 255))
->toArray()
->exec();
// result will be invalid with error messages provided for all properties.
var_dump($result->isValid()); // false
var_dump($result->getErrorMap()->getChild('title')->isEmpty()); // false due to maxlength
var_dump($result->getErrorMap()->getChild('name')->isEmpty()); // false due to minlength
var_dump($result->getErrorMap()->getChild('missing')->isEmpty()); // false because mandatory