vinkla/base62

A reversible base62 ID obfuscator

Installs: 762

Dependents: 0

Stars: 18

Watches: 3

Open Issues: 1

Language: PHP

2.1.0 2015-05-08 14:58 UTC

README

base62-package

A reversible base62 ID obfuscater. Works well with Laravel.

// The base string.
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Setup the Base62 instance.
$base = new Base62($string);

// Encode an integer.
$base->encode(4815162342); // returns '5fRVGK'

// Decode a string.
$base->decode('5fRVGK'); // returns 4815162342

Build Status StyleCI Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

composer require vinkla/base62

Laravel

If using Laravel (not required), add the service provider to config/app.php in the providers array.

'Vinkla\Base62\Base62ServiceProvider'

If you want you can use the facade. Add the reference in config/app.php to your aliases array.

'Base62' => 'Vinkla\Base62\Facades\Base62'

To add the configuration file to your config directory, run the command below.

php artisan vendor:publish
Looking for a Laravel 4 compatable version?

Please use 1.0 branch instead. Installable by requiring:

composer require vinkla/vimeo:~1.0

Usage

Here you can see an example of just how simple this package is to use.

use Vinkla\Base62\Base62;

// The base string.
$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Setup the Base62 instance.
$base = new Base62($string);

// Encode an integer.
$base->encode(4815162342); // returns '5fRVGK'

// Decode a string.
$base->decode('5fRVGK'); // returns 4815162342

Laravel Usage

If you implement this package in a Laravel application, which isn't required, we've provided real world examples below.

// You can alias this in config/app.php.
use Vinkla\Base62\Facades\Base62;

Base62::encode(4815162342);
// We're done here - how easy was that, it just works!

Base62::decode('5fRVGK');
// This example is simple and there are far more methods available.

If you prefer to use dependency injection over facades like me, then you can inject the manager:

use Vinkla\Base62\Base62;

class Foo
{
    protected $base62;

    public function __construct(Base62 $base62)
    {
        $this->base62 = $base62;
    }

    public function bar()
    {
        $this->base62->decode('5fRVGK');
    }
}

App::make('Foo')->bar();

License

Base62 package is licensed under The MIT License (MIT).