A string class with length handling strategies

0.1.2 2017-12-19 16:54 UTC

This package is auto-updated.

Last update: 2021-10-21 14:01:37 UTC


Build Status Latest Stable Version License


LengthAwareString is a string wrapper concerned with string length. By default it truncates strings that fail validation, but can be (optionally) given a strategy object to provide different functionality.


Via Composer:

composer require aviator/length-aware-string


Via Composer:

composer test


LengthAwareString requires at least two parameters, a string and an integer length:

$las = LengthAwareString::make('string', 5);

The string length is validated and handled on instantiation. If no validation strategy is provided the class will use Truncates, which does what it says on the tin.

You can provide an alternative strategy implementing the StringLengthValidator contract:

$las = LengthAwareString::make('string', 5, new YourStrategy);

This package also provides a Throws strategy which throws an exception if the string is too long.

To get the result you can call get():


// 'string'

The class implements __toString() so you can treat it like a string:

echo $las;

// 'string'

The class implements Countable so you can count() it:

echo count($las);

// 6



This package is licensed with the MIT License (MIT).