aviator/prefixed-number

A value object for numbers with prefixes.

1.1.0 2019-01-15 22:34 UTC

This package is auto-updated.

Last update: 2024-04-16 18:33:41 UTC


README

A simple class to manage prefixed numbers (eg X99) without relying on the intl extension.

Installation

Via Composer:

composer require aviator/prefixed-number

Testing

Via Composer:

composer test

Usage

Generally, you'll probably want to use PrefixedNumber::parse($string). This will return a new PrefixedNumber instance.

You can also instantiate it normally:

$object = new PrefixedNumber($number, $prefix, $padding);

From there you can get a string representation of the object.

$object->value(); // returns a string representation of the object. 

You can also easily increment or decrement it:

$object = new PrefixedNumber(99, 'X');

$incremented = $object->increment(); // Returns a new instance of PrefixedNumber
$decremented = $object->decrement(); // As does this

echo $object->value(); // 'X99'
echo $incremented->value(); // 'X100'
echo $decremented->value(); // 'X98'

You can also reset the number to 1:

$object = new PrefixedNumber(99, 'X');

$reset = $object->reset(); // Returns a new instance

echo $object->value(); // 'X99'
echo $reset->value(); // 'X1'

Other

The parser implements Aviator\Values\Interfaces\Parser. You can change how this class works by implementing that interface and injecting it as the second parameter of the static parse method:

$valueObject = PrefixedNumber::parse('X99', new MyLittleParser());

License

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