phpgears/identity

Identity object for PHP

0.1.4 2019-06-06 00:27 UTC

README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Identity

Identity objects for PHP

Installation

Composer

composer require phpgears/identity

Usage

Require composer autoload file

require './vendor/autoload.php';

By extending Gears\Identity\AbstractIdentity you can easily have an Identity class

use Gears\Identity\AbstractIdentity;

class CustomIdentity extends AbstractIdentity
{
    final public static function fromString(string $value)
    {
        // Check $value validity as an identity

        return new static($value);
    }
}

Available implementations

Due to its popularity UUID based identity implementations are provided

UuidIdentity
use Gears\Identity\UuidIdentity;
use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4()->toString();
$identity = UuidIdentity::fromString($uuid);

If you want a more concise UUID based identities you can use any of the following:

HashUuidIdentity

You need to require https://github.com/ivanakimov/hashids.php

composer require hashids/hashids
use Gears\Identity\HashUuidIdentity;
use Hashids\Hashids;
use Ramsey\Uuid\Uuid;

$hashIds = new Hashids();
$uuid = Uuid::uuid4()->toString();
$hashedUuid = $hashIds->encodeHex(\str_replace('-', '', $uuid));
$identity = HashUuidIdentity::fromString($hashedUuid);

// Or from UUID string
$identity = HashUuidIdentity::fromUuid(Uuid::uuid4()->toString());

// Get original UUID
$originalUuid = \sprintf('%s%s-%s-%s-%s-%s%s%s', ...str_split($hashIds->decodeHex($identity->getValue()), 4));
ShortUuidIdentity

You need to require https://github.com/pascaldevink/shortuuid

composer require pascaldevink/shortuuid
use Gears\Identity\ShortUuidIdentity;
use PascalDeVink\ShortUuid\ShortUuid;
use Ramsey\Uuid\Uuid;

$shortUuid = new ShortUuid();
$identity = ShortUuidIdentity::fromString($shortUuid->uuid4());

// Or from UUID string
$identity = ShortUuidIdentity::fromUuid(Uuid::uuid4()->toString());

// Get original UUID
$originalUuid = $shortUuid->decode($identity->getValue())->toString();
Base62UuidIdentity

You need to require https://github.com/tuupola/base62

composer require tuupola/base62
use Gears\Identity\Base62UuidIdentity;
use Ramsey\Uuid\Uuid;
use Tuupola\Base62;

$bas62 = new Base62();
$uuid = Uuid::uuid4()->toString();
$base62Uuid = $bas62->encode(\hex2bin(\str_replace('-', '', $uuid)));
$identity = Base62UuidIdentity::fromString($base62Uuid);

// Or from UUID string
$identity = Base62UuidIdentity::fromUuid(Uuid::uuid4()->toString());

// Get original UUID
$originalUuid = \sprintf('%s%s-%s-%s-%s-%s%s%s', ...str_split(\bin2hex($bas62->decode($identity->getValue())), 4));

Non UUID based identities

phpgears/identity-extra hosts non UUID based identity implementations, such as Mongo ObjectId and others

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.