tiny-blocks / encoder
Encodes and decodes arbitrary strings to and from Base62 for PHP.
Requires
- php: ^8.5
- ext-bcmath: *
Requires (Dev)
- ergebnis/composer-normalize: ^2.54
- infection/infection: ^0.35
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- slevomat/coding-standard: ^8.31
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Overview
Encodes and decodes arbitrary strings to and from Base62, the alphabet of digits and lower and upper case letters, which keeps the encoded value compact and safe in a URL. It backs the identifiers of ksuid.
Installation
composer require tiny-blocks/encoder
How to use
The library provides concrete implementations of the Encoder interface, enabling encoding and decoding of data into
specific formats like Base62.
Using Base62
To encode a value into Base62 format:
<?php declare(strict_types=1); use TinyBlocks\Encoder\Base62; $encoder = Base62::from(value: 'Hello world!'); $encoded = $encoder->encode(); # Output: T8dgcjRGuYUueWht
To decode a Base62-encoded value back to its original form:
<?php declare(strict_types=1); use TinyBlocks\Encoder\Base62; $encoder = Base62::from(value: 'T8dgcjRGuYUueWht'); $decoded = $encoder->decode(); # Output: Hello world!
If you attempt to decode an invalid Base62 value, an InvalidDecoding exception will be thrown:
<?php declare(strict_types=1); use TinyBlocks\Encoder\Base62; use TinyBlocks\Encoder\Exceptions\InvalidDecoding; try { $encoder = Base62::from(value: 'invalid_value'); $decoded = $encoder->decode(); } catch (InvalidDecoding $exception) { echo $exception->getMessage(); # Output: The value <invalid_value> could not be decoded. }
License
Encoder is licensed under MIT.
Contributing
Please follow the contributing guidelines to contribute to the project.