as3/short-mongo-id

Generates a short string identifier from a Mongo object id value.

v1.0 2016-07-13 16:33 UTC

This package is not auto-updated.

Last update: 2024-12-25 20:23:58 UTC


README

Generates a short string identifier from a MongoId value.

This repository is a PHP port of the npm/javascript library created by treygriffith. The usage and preamble of the following documentation is taken from that repository and modified for use with PHP. The original repo can be found here: https://github.com/treygriffith/short-mongo-id

========

Id's are generated from the timestamp and counter of the MongoDB Id, with some slight variation. They should be reasonably unique.

This is, unfortunately, a one-way function. It will reliably produce the same short id for the same MongoDB Id, but the operation can't be reversed (it is missing information about the machine id, process id, and most of the counter).

Install

Use Composer:

$ composer require as3/short-mongo-id

Usage

Pass a PHP MongoId object (or a string that can be converted to one) and it will return a reasonably unique short id made of [-_a-zA-Z0-9].

use As3\ShortMongoId\Shortener;

$shortener = new Shortener();
$shortId   = $shortener->shorten('507f191e810c19729de860ea'); // returns "iTxuMF"

License

MIT (see License)