phpgears/identity

Identity object for PHP

0.1.3 2019-01-31 22:22 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:

ShortUuidIdentity

You need to require pascaldevink/shortuuid

use Gears\Identity\ShortUuidIdentity;
use PascalDeVink\ShortUuid\ShortUuid;

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

// Should you need to get original UUID
$originalUuid = $shortUuid->decode($identity->getValue())->toString();
HashUuidIdentity

You need to 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);

// Should you need to get original UUID, mind that UUID has no dash separators
$originalUuid = $hashIds->decodeHex($identity->getValue());

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.