Вход Регистрация
Файл: vendor/symfony/uid/Ulid.php
Строк: 164
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentUid;

/**
 * A ULID is lexicographically sortable and contains a 48-bit timestamp and 80-bit of crypto-random entropy.
 *
 * @see https://github.com/ulid/spec
 *
 * @author Nicolas Grekas <p@tchwork.com>
 */
class Ulid extends AbstractUid implements TimeBasedUidInterface
{
    protected const 
NIL '00000000000000000000000000';
    protected const 
MAX '7ZZZZZZZZZZZZZZZZZZZZZZZZZ';

    private static 
string $time '';
    private static array 
$rand = [];

    public function 
__construct(?string $ulid null)
    {
        if (
null === $ulid) {
            
$this->uid = static::generate();
        } elseif (
self::NIL === $ulid) {
            
$this->uid $ulid;
        } elseif (
self::MAX === strtr($ulid'z''Z')) {
            
$this->uid $ulid;
        } else {
            if (!
self::isValid($ulid)) {
                throw new 
InvalidArgumentException(sprintf('Invalid ULID: "%s".'$ulid));
            }

            
$this->uid strtoupper($ulid);
        }
    }

    public static function 
isValid(string $ulid): bool
    
{
        if (
26 !== strlen($ulid)) {
            return 
false;
        }

        if (
26 !== strspn($ulid'0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz')) {
            return 
false;
        }

        return 
$ulid[0] <= '7';
    }

    public static function 
fromString(string $ulid): static
    {
        if (
36 === strlen($ulid) && preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di'$ulid)) {
            
$ulid uuid_parse($ulid);
        } elseif (
22 === strlen($ulid) && 22 === strspn($ulidBinaryUtil::BASE58[''])) {
            
$ulid str_pad(BinaryUtil::fromBase($ulidBinaryUtil::BASE58), 16""STR_PAD_LEFT);
        }

        if (
16 !== strlen($ulid)) {
            return 
match (strtr($ulid'z''Z')) {
                
self::NIL => new NilUlid(),
                
self::MAX => new MaxUlid(),
                default => new static(
$ulid),
            };
        }

        
$ulid bin2hex($ulid);
        
$ulid sprintf('%02s%04s%04s%04s%04s%04s%04s',
            
base_convert(substr($ulid02), 1632),
            
base_convert(substr($ulid25), 1632),
            
base_convert(substr($ulid75), 1632),
            
base_convert(substr($ulid125), 1632),
            
base_convert(substr($ulid175), 1632),
            
base_convert(substr($ulid225), 1632),
            
base_convert(substr($ulid275), 1632)
        );

        if (
self::NIL === $ulid) {
            return new 
NilUlid();
        }

        if (
self::MAX === $ulid strtr($ulid'abcdefghijklmnopqrstuv''ABCDEFGHJKMNPQRSTVWXYZ')) {
            return new 
MaxUlid();
        }

        
$u = new static(self::NIL);
        
$u->uid $ulid;

        return 
$u;
    }

    public function 
toBinary(): string
    
{
        
$ulid strtr($this->uid'ABCDEFGHJKMNPQRSTVWXYZ''abcdefghijklmnopqrstuv');

        
$ulid sprintf('%02s%05s%05s%05s%05s%05s%05s',
            
base_convert(substr($ulid02), 3216),
            
base_convert(substr($ulid24), 3216),
            
base_convert(substr($ulid64), 3216),
            
base_convert(substr($ulid104), 3216),
            
base_convert(substr($ulid144), 3216),
            
base_convert(substr($ulid184), 3216),
            
base_convert(substr($ulid224), 3216)
        );

        return 
hex2bin($ulid);
    }

    
/**
     * Returns the identifier as a base32 case insensitive string.
     *
     * @see https://tools.ietf.org/html/rfc4648#section-6
     *
     * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
     */
    
public function toBase32(): string
    
{
        return 
$this->uid;
    }

    public function 
getDateTime(): DateTimeImmutable
    
{
        
$time strtr(substr($this->uid010), 'ABCDEFGHJKMNPQRSTVWXYZ''abcdefghijklmnopqrstuv');

        if (
PHP_INT_SIZE >= 8) {
            
$time = (string) hexdec(base_convert($time3216));
        } else {
            
$time sprintf('%02s%05s%05s',
                
base_convert(substr($time02), 3216),
                
base_convert(substr($time24), 3216),
                
base_convert(substr($time64), 3216)
            );
            
$time BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
        }

        if (
strlen($time)) {
            
$time '000'.$time;
        }

        return 
DateTimeImmutable::createFromFormat('U.u'substr_replace($time'.', -30));
    }

    public static function 
generate(?DateTimeInterface $time null): string
    
{
        if (
null === $mtime $time) {
            
$time microtime(false);
            
$time substr($time11).substr($time23);
        } elseif (
$time $time->format('Uv')) {
            throw new 
InvalidArgumentException('The timestamp must be positive.');
        }

        if (
$time self::$time || (null !== $mtime && $time !== self::$time)) {
            
randomize:
            
$r unpack('n*'random_bytes(10));
            
$r[1] |= ($r[5] <<= 4) & 0xF0000;
            
$r[2] |= ($r[5] <<= 4) & 0xF0000;
            
$r[3] |= ($r[5] <<= 4) & 0xF0000;
            
$r[4] |= ($r[5] <<= 4) & 0xF0000;
            unset(
$r[5]);
            
self::$rand $r;
            
self::$time $time;
        } elseif ([
=> 0xFFFFF0xFFFFF0xFFFFF0xFFFFF] === self::$rand) {
            if (
PHP_INT_SIZE >= || 10 strlen($time self::$time)) {
                
$time = (string) ($time);
            } elseif (
'999999999' === $mtime substr($time, -9)) {
                
$time = (substr($time0, -9)).'000000000';
            } else {
                
$time substr_replace($timestr_pad(++$mtime9'0'STR_PAD_LEFT), -9);
            }

            goto 
randomize;
        } else {
            for (
$i 4$i && 0xFFFFF === self::$rand[$i]; --$i) {
                
self::$rand[$i] = 0;
            }

            ++
self::$rand[$i];
            
$time self::$time;
        }

        if (
PHP_INT_SIZE >= 8) {
            
$time base_convert($time1032);
        } else {
            
$time str_pad(bin2hex(BinaryUtil::fromBase($timeBinaryUtil::BASE10)), 12'0'STR_PAD_LEFT);
            
$time sprintf('%s%04s%04s',
                
base_convert(substr($time02), 1632),
                
base_convert(substr($time25), 1632),
                
base_convert(substr($time75), 1632)
            );
        }

        return 
strtr(sprintf('%010s%04s%04s%04s%04s',
            
$time,
            
base_convert(self::$rand[1], 1032),
            
base_convert(self::$rand[2], 1032),
            
base_convert(self::$rand[3], 1032),
            
base_convert(self::$rand[4], 1032)
        ), 
'abcdefghijklmnopqrstuv''ABCDEFGHJKMNPQRSTVWXYZ');
    }
}
Онлайн: 0
Реклама