phpgears/identity

PHP的标识对象

0.2.4 2020-11-20 11:56 UTC

This package is auto-updated.

Last update: 2024-09-20 19:45:30 UTC


README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

标识

PHP的标识对象

安装

Composer

composer require phpgears/identity

使用

需要composer自动加载文件

require './vendor/autoload.php';

通过扩展Gears\Identity\AbstractIdentity,您可以轻松地拥有一个标识类

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);
    }
}

实现

由于其流行,提供了基于UUID(《RFC 4122》)的标识实现

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

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

// From generator
$identity = (new UuidIdentityGenerator())->generate();
$identity = (new OrderedUuidIdentityGenerator())->generate();

// Get original UUID string
$originalUuid = $identity->getValue();

如果您想使用更简洁的基于UUID的标识,可以使用以下任何一种

CondensedUuidIdentity

不带短横线的UUID

use Gears\Identity\CondensedUuidIdentity;
use Gears\Identity\CondensedUuidIdentityGenerator;
use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4()->toString();
$identity = CondensedUuidIdentity::fromString(\str_replace('-', '', $uuid));

$identity = CondensedUuidIdentity::fromUuid($uuid); // From UUID string
$identity = (new CondensedUuidIdentityGenerator())->generate(); // From generator

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

需要https://github.com/ivanakimov/hashids.php

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

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

$identity = HashUuidIdentity::fromUuid($uuid); // From UUID string
$identity = (new HashUuidIdentityGenerator())->generate(); // From generator

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

需要https://github.com/pascaldevink/shortuuid

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

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

$identity = ShortUuidIdentity::fromUuid(Uuid::uuid4()->toString()); // From UUID string
$identity = (new ShortUuidIdentityGenerator())->generate(); // From generator

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

需要https://github.com/tuupola/base62

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

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

$identity = Base62UuidIdentity::fromUuid($uuid); // From UUID string
$identity = (new Base62UuidIdentityGenerator())->generate(); // From generator

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

非UUID标识

phpgears/identity-extra提供了非UUID标识实现,例如Mongo的ObjectId和其它一些,根据您的使用情况考虑查看它

合适的标识

创建非唯一的标识没有意义,始终使用一种经过验证的方法来确保标识值的唯一性。这基本上可以这样表述:永远不要实现创建唯一标识符的机制

强烈建议不要允许具有任意字符串值的标识,或者无法验证其正确性的值,这就是为什么这个包中永远不会提供通用开放值标识类,并且您也绝对不应该实现这样的事情。

如果您想与您的架构或他人的其他系统(如消息队列、webhooks、共享消息系统等)实现最大程度的互操作性,那么您可能应该选择普通的UUID标识,因为这种格式被广泛接受,并且所有主要语言都有支持

如果您完全控制您的架构以及与之共享数据的所有系统,您可以考虑使用更简洁的UUID标识符或非UUID标识符,这可能具有其他优点,例如更友好的用户/URL,可排序等

除了随机生成的唯一值(如UUID)之外,您可能可以使用可验证的真实生活唯一标识符,例如银行账户的IBAN或出版物的ISBN。在可能的情况下努力实现这种类型的标识符

use Biblys\Isbn\Isbn;
use Gears\Identity\AbstractIdentity;
use Gears\Identity\Exception\InvalidIdentityException;

class ISBNIdentity extends AbstractIdentity
{
    final public static function fromString(string $value)
    {
        $isbn = new Isbn($value);
        if (!$isbn->isValid()) {
            throw new InvalidIdentityException(\sprintf('"%s" is not a valid ISBN', $value));
        }

        return new static($isbn->format('ISBN-13'));
    }
}

贡献

发现了一个错误或有一个功能请求?请创建一个新的问题。在提交之前查看现有的问题。

查看文件CONTRIBUTING.md

许可证

有关许可证条款的副本,请参阅源代码中包含的文件LICENSE