aviator/length-aware-string

A string class with length handling strategies

0.1.2 2017-12-19 16:54 UTC

This package is auto-updated.

Last update: 2024-04-21 19:18:04 UTC


README

Build Status Latest Stable Version License

Overview

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.

Installation

Via Composer:

composer require aviator/length-aware-string

Testing

Via Composer:

composer test

Usage

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():

$las->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

Other

License

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