home/mip/mip/vendor/symfony/uid/Uuid.php 0000644 00000013276 15152054652 0014265 0 ustar 00 <?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 Symfony\Component\Uid;
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*
* @see https://tools.ietf.org/html/rfc4122#appendix-C for details about namespaces
*/
class Uuid extends AbstractUid
{
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
protected const TYPE = 0;
protected const NIL = '00000000-0000-0000-0000-000000000000';
protected const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
public function __construct(string $uuid, bool $checkVariant = false)
{
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
if (false === $type || (static::TYPE ?: $type) !== $type) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}
$this->uid = strtolower($uuid);
if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
}
}
public static function fromString(string $uuid): static
{
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58[''])) {
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
}
if (16 === \strlen($uuid)) {
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($uuid);
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
$uuid = substr_replace($uuid, '-', 23, 0);
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
$ulid = new NilUlid();
$ulid->uid = strtoupper($uuid);
$uuid = $ulid->toRfc4122();
}
if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
return new static($uuid);
}
if (self::NIL === $uuid) {
return new NilUuid();
}
if (self::MAX === $uuid = strtr($uuid, 'F', 'f')) {
return new MaxUuid();
}
if (!\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) {
return new self($uuid);
}
return match ((int) $uuid[14]) {
UuidV1::TYPE => new UuidV1($uuid),
UuidV3::TYPE => new UuidV3($uuid),
UuidV4::TYPE => new UuidV4($uuid),
UuidV5::TYPE => new UuidV5($uuid),
UuidV6::TYPE => new UuidV6($uuid),
UuidV7::TYPE => new UuidV7($uuid),
UuidV8::TYPE => new UuidV8($uuid),
default => new self($uuid),
};
}
final public static function v1(): UuidV1
{
return new UuidV1();
}
final public static function v3(self $namespace, string $name): UuidV3
{
// don't use uuid_generate_md5(), some versions are buggy
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
return new UuidV3(self::format($uuid, '-3'));
}
final public static function v4(): UuidV4
{
return new UuidV4();
}
final public static function v5(self $namespace, string $name): UuidV5
{
// don't use uuid_generate_sha1(), some versions are buggy
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
return new UuidV5(self::format($uuid, '-5'));
}
final public static function v6(): UuidV6
{
return new UuidV6();
}
final public static function v7(): UuidV7
{
return new UuidV7();
}
final public static function v8(string $uuid): UuidV8
{
return new UuidV8($uuid);
}
public static function isValid(string $uuid): bool
{
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
return true;
}
if (self::MAX === strtr($uuid, 'F', 'f') && \in_array(static::class, [__CLASS__, MaxUuid::class], true)) {
return true;
}
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
return false;
}
return __CLASS__ === static::class || static::TYPE === (int) $uuid[14];
}
public function toBinary(): string
{
return uuid_parse($this->uid);
}
/**
* Returns the identifier as a RFC4122 case insensitive string.
*
* @see https://tools.ietf.org/html/rfc4122#section-3
*
* @example 09748193-048a-4bfb-b825-8528cf74fdc1 (len=36)
*/
public function toRfc4122(): string
{
return $this->uid;
}
public function compare(AbstractUid $other): int
{
if (false !== $cmp = uuid_compare($this->uid, $other->uid)) {
return $cmp;
}
return parent::compare($other);
}
private static function format(string $uuid, string $version): string
{
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
$uuid = substr_replace($uuid, $version, 13, 1);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
}
home/mip/mip/vendor/ramsey/uuid/src/Uuid.php 0000644 00000062763 15152066062 0015040 0 ustar 00 <?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid;
use BadMethodCallException;
use DateTimeInterface;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
use Ramsey\Uuid\Fields\FieldsInterface;
use Ramsey\Uuid\Lazy\LazyUuidFromString;
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\Integer as IntegerObject;
use ValueError;
use function assert;
use function bin2hex;
use function method_exists;
use function preg_match;
use function sprintf;
use function str_replace;
use function strcmp;
use function strlen;
use function strtolower;
use function substr;
/**
* Uuid provides constants and static methods for working with and generating UUIDs
*
* @psalm-immutable
*/
class Uuid implements UuidInterface
{
use DeprecatedUuidMethodsTrait;
/**
* When this namespace is specified, the name string is a fully-qualified
* domain name
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C RFC 4122, Appendix C: Some Name Space IDs
*/
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is a URL
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C RFC 4122, Appendix C: Some Name Space IDs
*/
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an ISO OID
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C RFC 4122, Appendix C: Some Name Space IDs
*/
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an X.500 DN in DER
* or a text output format
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C RFC 4122, Appendix C: Some Name Space IDs
*/
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
/**
* The nil UUID is a special form of UUID that is specified to have all 128
* bits set to zero
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.7 RFC 4122, § 4.1.7: Nil UUID
*/
public const NIL = '00000000-0000-0000-0000-000000000000';
/**
* The max UUID is a special form of UUID that is specified to have all 128
* bits set to one
*
* @link https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.10 Max UUID
*/
public const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
/**
* Variant: reserved, NCS backward compatibility
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
*/
public const RESERVED_NCS = 0;
/**
* Variant: the UUID layout specified in RFC 4122
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
*/
public const RFC_4122 = 2;
/**
* Variant: reserved, Microsoft Corporation backward compatibility
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
*/
public const RESERVED_MICROSOFT = 6;
/**
* Variant: reserved for future definition
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
*/
public const RESERVED_FUTURE = 7;
/**
* @deprecated Use {@see ValidatorInterface::getPattern()} instead.
*/
public const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$';
/**
* Version 1 (Gregorian time) UUID
*
* @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
*/
public const UUID_TYPE_TIME = 1;
/**
* Version 2 (DCE Security) UUID
*
* @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
*/
public const UUID_TYPE_DCE_SECURITY = 2;
/**
* @deprecated Use {@see Uuid::UUID_TYPE_DCE_SECURITY} instead.
*/
public const UUID_TYPE_IDENTIFIER = 2;
/**
* Version 3 (name-based and hashed with MD5) UUID
*
* @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
*/
public const UUID_TYPE_HASH_MD5 = 3;
/**
* Version 4 (random) UUID
*
* @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
*/
public const UUID_TYPE_RANDOM = 4;
/**
* Version 5 (name-based and hashed with SHA1) UUID
*
* @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
*/
public const UUID_TYPE_HASH_SHA1 = 5;
/**
* @deprecated Use {@see Uuid::UUID_TYPE_REORDERED_TIME} instead.
*/
public const UUID_TYPE_PEABODY = 6;
/**
* Version 6 (reordered time) UUID
*
* @link https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.6 UUID Version 6
*/
public const UUID_TYPE_REORDERED_TIME = 6;
/**
* Version 7 (Unix Epoch time) UUID
*
* @link https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.7 UUID Version 7
*/
public const UUID_TYPE_UNIX_TIME = 7;
/**
* @link https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.8 UUID Version 8
*/
public const UUID_TYPE_CUSTOM = 8;
/**
* DCE Security principal domain
*
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1
*/
public const DCE_DOMAIN_PERSON = 0;
/**
* DCE Security group domain
*
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1
*/
public const DCE_DOMAIN_GROUP = 1;
/**
* DCE Security organization domain
*
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1
*/
public const DCE_DOMAIN_ORG = 2;
/**
* DCE Security domain string names
*
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1
*/
public const DCE_DOMAIN_NAMES = [
self::DCE_DOMAIN_PERSON => 'person',
self::DCE_DOMAIN_GROUP => 'group',
self::DCE_DOMAIN_ORG => 'org',
];
private static ?UuidFactoryInterface $factory = null;
/**
* @var bool flag to detect if the UUID factory was replaced internally,
* which disables all optimizations for the default/happy path internal
* scenarios
*/
private static bool $factoryReplaced = false;
protected CodecInterface $codec;
protected NumberConverterInterface $numberConverter;
protected Rfc4122FieldsInterface $fields;
protected TimeConverterInterface $timeConverter;
/**
* Creates a universally unique identifier (UUID) from an array of fields
*
* Unless you're making advanced use of this library to generate identifiers
* that deviate from RFC 4122, you probably do not want to instantiate a
* UUID directly. Use the static methods, instead:
*
* ```
* use Ramsey\Uuid\Uuid;
*
* $timeBasedUuid = Uuid::uuid1();
* $namespaceMd5Uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://php.net/');
* $randomUuid = Uuid::uuid4();
* $namespaceSha1Uuid = Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://php.net/');
* ```
*
* @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
* @param NumberConverterInterface $numberConverter The number converter to use
* for converting hex values to/from integers
* @param CodecInterface $codec The codec to use when encoding or decoding
* UUID strings
* @param TimeConverterInterface $timeConverter The time converter to use
* for converting timestamps extracted from a UUID to unix timestamps
*/
public function __construct(
Rfc4122FieldsInterface $fields,
NumberConverterInterface $numberConverter,
CodecInterface $codec,
TimeConverterInterface $timeConverter
) {
$this->fields = $fields;
$this->codec = $codec;
$this->numberConverter = $numberConverter;
$this->timeConverter = $timeConverter;
}
/**
* @psalm-return non-empty-string
*/
public function __toString(): string
{
return $this->toString();
}
/**
* Converts the UUID to a string for JSON serialization
*/
public function jsonSerialize(): string
{
return $this->toString();
}
/**
* Converts the UUID to a string for PHP serialization
*/
public function serialize(): string
{
return $this->codec->encode($this);
}
/**
* @return array{bytes: string}
*/
public function __serialize(): array
{
return ['bytes' => $this->serialize()];
}
/**
* Re-constructs the object from its serialized form
*
* @param string $data The serialized PHP string to unserialize into
* a UuidInterface instance
*/
public function unserialize(string $data): void
{
if (strlen($data) === 16) {
/** @var Uuid $uuid */
$uuid = self::getFactory()->fromBytes($data);
} else {
/** @var Uuid $uuid */
$uuid = self::getFactory()->fromString($data);
}
$this->codec = $uuid->codec;
$this->numberConverter = $uuid->numberConverter;
$this->fields = $uuid->fields;
$this->timeConverter = $uuid->timeConverter;
}
/**
* @param array{bytes?: string} $data
*/
public function __unserialize(array $data): void
{
// @codeCoverageIgnoreStart
if (!isset($data['bytes'])) {
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__));
}
// @codeCoverageIgnoreEnd
$this->unserialize($data['bytes']);
}
public function compareTo(UuidInterface $other): int
{
$compare = strcmp($this->toString(), $other->toString());
if ($compare < 0) {
return -1;
}
if ($compare > 0) {
return 1;
}
return 0;
}
public function equals(?object $other): bool
{
if (!$other instanceof UuidInterface) {
return false;
}
return $this->compareTo($other) === 0;
}
/**
* @psalm-return non-empty-string
*/
public function getBytes(): string
{
return $this->codec->encodeBinary($this);
}
public function getFields(): FieldsInterface
{
return $this->fields;
}
public function getHex(): Hexadecimal
{
return new Hexadecimal(str_replace('-', '', $this->toString()));
}
public function getInteger(): IntegerObject
{
return new IntegerObject($this->numberConverter->fromHex($this->getHex()->toString()));
}
public function getUrn(): string
{
return 'urn:uuid:' . $this->toString();
}
/**
* @psalm-return non-empty-string
*/
public function toString(): string
{
return $this->codec->encode($this);
}
/**
* Returns the factory used to create UUIDs
*/
public static function getFactory(): UuidFactoryInterface
{
if (self::$factory === null) {
self::$factory = new UuidFactory();
}
return self::$factory;
}
/**
* Sets the factory used to create UUIDs
*
* @param UuidFactoryInterface $factory A factory that will be used by this
* class to create UUIDs
*/
public static function setFactory(UuidFactoryInterface $factory): void
{
// Note: non-strict equality is intentional here. If the factory is configured differently, every assumption
// around purity is broken, and we have to internally decide everything differently.
// phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator
self::$factoryReplaced = ($factory != new UuidFactory());
self::$factory = $factory;
}
/**
* Creates a UUID from a byte string
*
* @param string $bytes A binary string
*
* @return UuidInterface A UuidInterface instance created from a binary
* string representation
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*
* @psalm-suppress ImpureStaticProperty we know that the factory being replaced can lead to massive
* havoc across all consumers: that should never happen, and
* is generally to be discouraged. Until the factory is kept
* un-replaced, this method is effectively pure.
*/
public static function fromBytes(string $bytes): UuidInterface
{
if (!self::$factoryReplaced && strlen($bytes) === 16) {
$base16Uuid = bin2hex($bytes);
// Note: we are calling `fromString` internally because we don't know if the given `$bytes` is a valid UUID
return self::fromString(
substr($base16Uuid, 0, 8)
. '-'
. substr($base16Uuid, 8, 4)
. '-'
. substr($base16Uuid, 12, 4)
. '-'
. substr($base16Uuid, 16, 4)
. '-'
. substr($base16Uuid, 20, 12)
);
}
return self::getFactory()->fromBytes($bytes);
}
/**
* Creates a UUID from the string standard representation
*
* @param string $uuid A hexadecimal string
*
* @return UuidInterface A UuidInterface instance created from a hexadecimal
* string representation
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*
* @psalm-suppress ImpureStaticProperty we know that the factory being replaced can lead to massive
* havoc across all consumers: that should never happen, and
* is generally to be discouraged. Until the factory is kept
* un-replaced, this method is effectively pure.
*/
public static function fromString(string $uuid): UuidInterface
{
$uuid = strtolower($uuid);
if (!self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) {
assert($uuid !== '');
return new LazyUuidFromString($uuid);
}
return self::getFactory()->fromString($uuid);
}
/**
* Creates a UUID from a DateTimeInterface instance
*
* @param DateTimeInterface $dateTime The date and time
* @param Hexadecimal|null $node A 48-bit number representing the hardware
* address
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes
*
* @return UuidInterface A UuidInterface instance that represents a
* version 1 UUID created from a DateTimeInterface instance
*/
public static function fromDateTime(
DateTimeInterface $dateTime,
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->fromDateTime($dateTime, $node, $clockSeq);
}
/**
* Creates a UUID from the Hexadecimal object
*
* @param Hexadecimal $hex Hexadecimal object representing a hexadecimal number
*
* @return UuidInterface A UuidInterface instance created from the Hexadecimal
* object representing a hexadecimal number
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public static function fromHexadecimal(Hexadecimal $hex): UuidInterface
{
$factory = self::getFactory();
if (method_exists($factory, 'fromHexadecimal')) {
/**
* @phpstan-ignore-next-line
* @psalm-suppress UndefinedInterfaceMethod
*/
return self::getFactory()->fromHexadecimal($hex);
}
throw new BadMethodCallException('The method fromHexadecimal() does not exist on the provided factory');
}
/**
* Creates a UUID from a 128-bit integer string
*
* @param string $integer String representation of 128-bit integer
*
* @return UuidInterface A UuidInterface instance created from the string
* representation of a 128-bit integer
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*/
public static function fromInteger(string $integer): UuidInterface
{
/** @psalm-suppress ImpureMethodCall */
return self::getFactory()->fromInteger($integer);
}
/**
* Returns true if the provided string is a valid UUID
*
* @param string $uuid A string to validate as a UUID
*
* @return bool True if the string is a valid UUID, false otherwise
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*
* @psalm-assert-if-true =non-empty-string $uuid
*/
public static function isValid(string $uuid): bool
{
/** @psalm-suppress ImpureMethodCall */
return self::getFactory()->getValidator()->validate($uuid);
}
/**
* Returns a version 1 (Gregorian time) UUID from a host ID, sequence number,
* and the current time
*
* @param Hexadecimal|int|string|null $node A 48-bit number representing the
* hardware address; this number may be represented as an integer or a
* hexadecimal string
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
* could arise when the clock is set backwards in time or if the node ID
* changes
*
* @return UuidInterface A UuidInterface instance that represents a
* version 1 UUID
*/
public static function uuid1($node = null, ?int $clockSeq = null): UuidInterface
{
return self::getFactory()->uuid1($node, $clockSeq);
}
/**
* Returns a version 2 (DCE Security) UUID from a local domain, local
* identifier, host ID, clock sequence, and the current time
*
* @param int $localDomain The local domain to use when generating bytes,
* according to DCE Security
* @param IntegerObject|null $localIdentifier The local identifier for the
* given domain; this may be a UID or GID on POSIX systems, if the local
* domain is person or group, or it may be a site-defined identifier
* if the local domain is org
* @param Hexadecimal|null $node A 48-bit number representing the hardware
* address
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes (in a version 2 UUID, the lower 8 bits of this number
* are replaced with the domain).
*
* @return UuidInterface A UuidInterface instance that represents a
* version 2 UUID
*/
public static function uuid2(
int $localDomain,
?IntegerObject $localIdentifier = null,
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->uuid2($localDomain, $localIdentifier, $node, $clockSeq);
}
/**
* Returns a version 3 (name-based) UUID based on the MD5 hash of a
* namespace ID and a name
*
* @param string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
* version 3 UUID
*
* @psalm-suppress ImpureMethodCall we know that the factory being replaced can lead to massive
* havoc across all consumers: that should never happen, and
* is generally to be discouraged. Until the factory is kept
* un-replaced, this method is effectively pure.
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*/
public static function uuid3($ns, string $name): UuidInterface
{
return self::getFactory()->uuid3($ns, $name);
}
/**
* Returns a version 4 (random) UUID
*
* @return UuidInterface A UuidInterface instance that represents a
* version 4 UUID
*/
public static function uuid4(): UuidInterface
{
return self::getFactory()->uuid4();
}
/**
* Returns a version 5 (name-based) UUID based on the SHA-1 hash of a
* namespace ID and a name
*
* @param string|UuidInterface $ns The namespace (must be a valid UUID)
* @param string $name The name to use for creating a UUID
*
* @return UuidInterface A UuidInterface instance that represents a
* version 5 UUID
*
* @psalm-pure note: changing the internal factory is an edge case not covered by purity invariants,
* but under constant factory setups, this method operates in functionally pure manners
*
* @psalm-suppress ImpureMethodCall we know that the factory being replaced can lead to massive
* havoc across all consumers: that should never happen, and
* is generally to be discouraged. Until the factory is kept
* un-replaced, this method is effectively pure.
*/
public static function uuid5($ns, string $name): UuidInterface
{
return self::getFactory()->uuid5($ns, $name);
}
/**
* Returns a version 6 (reordered time) UUID from a host ID, sequence number,
* and the current time
*
* @param Hexadecimal|null $node A 48-bit number representing the hardware
* address
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates that
* could arise when the clock is set backwards in time or if the node ID
* changes
*
* @return UuidInterface A UuidInterface instance that represents a
* version 6 UUID
*/
public static function uuid6(
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->uuid6($node, $clockSeq);
}
/**
* Returns a version 7 (Unix Epoch time) UUID
*
* @param DateTimeInterface|null $dateTime An optional date/time from which
* to create the version 7 UUID. If not provided, the UUID is generated
* using the current date/time.
*
* @return UuidInterface A UuidInterface instance that represents a
* version 7 UUID
*/
public static function uuid7(?DateTimeInterface $dateTime = null): UuidInterface
{
$factory = self::getFactory();
if (method_exists($factory, 'uuid7')) {
/** @var UuidInterface */
return $factory->uuid7($dateTime);
}
throw new UnsupportedOperationException(
'The provided factory does not support the uuid7() method',
);
}
/**
* Returns a version 8 (custom) UUID
*
* The bytes provided may contain any value according to your application's
* needs. Be aware, however, that other applications may not understand the
* semantics of the value.
*
* @param string $bytes A 16-byte octet string. This is an open blob
* of data that you may fill with 128 bits of information. Be aware,
* however, bits 48 through 51 will be replaced with the UUID version
* field, and bits 64 and 65 will be replaced with the UUID variant. You
* MUST NOT rely on these bits for your application needs.
*
* @return UuidInterface A UuidInterface instance that represents a
* version 8 UUID
*/
public static function uuid8(string $bytes): UuidInterface
{
$factory = self::getFactory();
if (method_exists($factory, 'uuid8')) {
/** @var UuidInterface */
return $factory->uuid8($bytes);
}
throw new UnsupportedOperationException(
'The provided factory does not support the uuid8() method',
);
}
}
home/mip/mip/vendor/symfony/polyfill-uuid/Uuid.php 0000644 00000041336 15152070122 0016266 0 ustar 00 <?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 Symfony\Polyfill\Uuid;
/**
* @internal
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
final class Uuid
{
public const UUID_VARIANT_NCS = 0;
public const UUID_VARIANT_DCE = 1;
public const UUID_VARIANT_MICROSOFT = 2;
public const UUID_VARIANT_OTHER = 3;
public const UUID_TYPE_DEFAULT = 0;
public const UUID_TYPE_TIME = 1;
public const UUID_TYPE_MD5 = 3;
public const UUID_TYPE_DCE = 4; // Deprecated alias
public const UUID_TYPE_NAME = 1; // Deprecated alias
public const UUID_TYPE_RANDOM = 4;
public const UUID_TYPE_SHA1 = 5;
public const UUID_TYPE_NULL = -1;
public const UUID_TYPE_INVALID = -42;
// https://tools.ietf.org/html/rfc4122#section-4.1.4
// 0x01b21dd213814000 is the number of 100-ns intervals between the
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
public const TIME_OFFSET_INT = 0x01B21DD213814000;
public const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
public const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
public static function uuid_create($uuid_type = \UUID_TYPE_DEFAULT)
{
if (!is_numeric($uuid_type) && null !== $uuid_type) {
trigger_error(sprintf('uuid_create() expects parameter 1 to be int, %s given', \gettype($uuid_type)), \E_USER_WARNING);
return null;
}
switch ((int) $uuid_type) {
case self::UUID_TYPE_NAME:
case self::UUID_TYPE_TIME:
return self::uuid_generate_time();
case self::UUID_TYPE_DCE:
case self::UUID_TYPE_RANDOM:
case self::UUID_TYPE_DEFAULT:
return self::uuid_generate_random();
default:
trigger_error(sprintf("Unknown/invalid UUID type '%d' requested, using default type instead", $uuid_type), \E_USER_WARNING);
return self::uuid_generate_random();
}
}
public static function uuid_generate_md5($uuid_ns, $name)
{
if (!\is_string($uuid_ns = self::toString($uuid_ns))) {
trigger_error(sprintf('uuid_generate_md5() expects parameter 1 to be string, %s given', \gettype($uuid_ns)), \E_USER_WARNING);
return null;
}
if (!\is_string($name = self::toString($name))) {
trigger_error(sprintf('uuid_generate_md5() expects parameter 2 to be string, %s given', \gettype($name)), \E_USER_WARNING);
return null;
}
if (!self::isValid($uuid_ns)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_generate_md5(): Argument #1 ($uuid_ns) UUID expected');
}
$hash = md5(hex2bin(str_replace('-', '', $uuid_ns)).$name);
return sprintf('%08s-%04s-3%03s-%04x-%012s',
// 32 bits for "time_low"
substr($hash, 0, 8),
// 16 bits for "time_mid"
substr($hash, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 3
substr($hash, 13, 3),
// 16 bits:
// * 8 bits for "clk_seq_hi_res",
// * 8 bits for "clk_seq_low",
hexdec(substr($hash, 16, 4)) & 0x3FFF | 0x8000,
// 48 bits for "node"
substr($hash, 20, 12)
);
}
public static function uuid_generate_sha1($uuid_ns, $name)
{
if (!\is_string($uuid_ns = self::toString($uuid_ns))) {
trigger_error(sprintf('uuid_generate_sha1() expects parameter 1 to be string, %s given', \gettype($uuid_ns)), \E_USER_WARNING);
return null;
}
if (!\is_string($name = self::toString($name))) {
trigger_error(sprintf('uuid_generate_sha1() expects parameter 2 to be string, %s given', \gettype($name)), \E_USER_WARNING);
return null;
}
if (!self::isValid($uuid_ns)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_generate_sha1(): Argument #1 ($uuid_ns) UUID expected');
}
$hash = sha1(hex2bin(str_replace('-', '', $uuid_ns)).$name);
return sprintf('%08s-%04s-5%03s-%04x-%012s',
// 32 bits for "time_low"
substr($hash, 0, 8),
// 16 bits for "time_mid"
substr($hash, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 5
substr($hash, 13, 3),
// 16 bits:
// * 8 bits for "clk_seq_hi_res",
// * 8 bits for "clk_seq_low",
// WARNING: On old libuuid version, there is a bug. 0x0fff is used instead of 0x3fff
// See https://github.com/karelzak/util-linux/commit/d6ddf07d31dfdc894eb8e7e6842aa856342c526e
hexdec(substr($hash, 16, 4)) & 0x3FFF | 0x8000,
// 48 bits for "node"
substr($hash, 20, 12)
);
}
public static function uuid_is_valid($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_is_valid() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
return self::isValid($uuid);
}
public static function uuid_compare($uuid1, $uuid2)
{
if (!\is_string($uuid1 = self::toString($uuid1))) {
trigger_error(sprintf('uuid_compare() expects parameter 1 to be string, %s given', \gettype($uuid1)), \E_USER_WARNING);
return null;
}
if (!\is_string($uuid2 = self::toString($uuid2))) {
trigger_error(sprintf('uuid_compare() expects parameter 2 to be string, %s given', \gettype($uuid2)), \E_USER_WARNING);
return null;
}
if (!self::isValid($uuid1)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_compare(): Argument #1 ($uuid1) UUID expected');
}
if (!self::isValid($uuid2)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_compare(): Argument #2 ($uuid2) UUID expected');
}
return strcasecmp($uuid1, $uuid2);
}
public static function uuid_is_null($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_is_null() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
if (80000 <= \PHP_VERSION_ID && !self::isValid($uuid)) {
throw new \ValueError('uuid_is_null(): Argument #1 ($uuid) UUID expected');
}
return '00000000-0000-0000-0000-000000000000' === $uuid;
}
public static function uuid_type($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_type() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
if ('00000000-0000-0000-0000-000000000000' === $uuid) {
return self::UUID_TYPE_NULL;
}
if (null === $parsed = self::parse($uuid)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_type(): Argument #1 ($uuid) UUID expected');
}
return $parsed['version'];
}
public static function uuid_variant($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_variant() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
if ('00000000-0000-0000-0000-000000000000' === $uuid) {
return self::UUID_TYPE_NULL;
}
if (null === $parsed = self::parse($uuid)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_variant(): Argument #1 ($uuid) UUID expected');
}
if (($parsed['clock_seq'] & 0x8000) === 0) {
return self::UUID_VARIANT_NCS;
}
if (($parsed['clock_seq'] & 0x4000) === 0) {
return self::UUID_VARIANT_DCE;
}
if (($parsed['clock_seq'] & 0x2000) === 0) {
return self::UUID_VARIANT_MICROSOFT;
}
return self::UUID_VARIANT_OTHER;
}
public static function uuid_time($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_time() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
$parsed = self::parse($uuid);
if (self::UUID_TYPE_TIME !== ($parsed['version'] ?? null)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_time(): Argument #1 ($uuid) UUID DCE TIME expected');
}
if (\PHP_INT_SIZE >= 8) {
return intdiv(hexdec($parsed['time']) - self::TIME_OFFSET_INT, 10000000);
}
$time = str_pad(hex2bin($parsed['time']), 8, "\0", \STR_PAD_LEFT);
$time = self::binaryAdd($time, self::TIME_OFFSET_COM);
$time[0] = $time[0] & "\x7F";
return (int) substr(self::toDecimal($time), 0, -7);
}
public static function uuid_mac($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_mac() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
$parsed = self::parse($uuid);
if (self::UUID_TYPE_TIME !== ($parsed['version'] ?? null)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_mac(): Argument #1 ($uuid) UUID DCE TIME expected');
}
return strtr($parsed['node'], 'ABCDEF', 'abcdef');
}
public static function uuid_parse($uuid)
{
if (!\is_string($uuid = self::toString($uuid))) {
trigger_error(sprintf('uuid_parse() expects parameter 1 to be string, %s given', \gettype($uuid)), \E_USER_WARNING);
return null;
}
if (!self::isValid($uuid)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_parse(): Argument #1 ($uuid) UUID expected');
}
return hex2bin(str_replace('-', '', $uuid));
}
public static function uuid_unparse($bytes)
{
if (!\is_string($bytes = self::toString($bytes))) {
trigger_error(sprintf('uuid_unparse() expects parameter 1 to be string, %s given', \gettype($bytes)), \E_USER_WARNING);
return null;
}
if (16 !== \strlen($bytes)) {
if (80000 > \PHP_VERSION_ID) {
return false;
}
throw new \ValueError('uuid_unparse(): Argument #1 ($uuid) UUID expected');
}
$uuid = bin2hex($bytes);
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
private static function uuid_generate_random()
{
$uuid = bin2hex(random_bytes(16));
return sprintf('%08s-%04s-4%03s-%04x-%012s',
// 32 bits for "time_low"
substr($uuid, 0, 8),
// 16 bits for "time_mid"
substr($uuid, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
substr($uuid, 13, 3),
// 16 bits:
// * 8 bits for "clk_seq_hi_res",
// * 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
hexdec(substr($uuid, 16, 4)) & 0x3FFF | 0x8000,
// 48 bits for "node"
substr($uuid, 20, 12)
);
}
/**
* @see http://tools.ietf.org/html/rfc4122#section-4.2.2
*/
private static function uuid_generate_time()
{
$time = microtime(false);
$time = substr($time, 11).substr($time, 2, 7);
if (\PHP_INT_SIZE >= 8) {
$time = str_pad(dechex($time + self::TIME_OFFSET_INT), 16, '0', \STR_PAD_LEFT);
} else {
$time = str_pad(self::toBinary($time), 8, "\0", \STR_PAD_LEFT);
$time = self::binaryAdd($time, self::TIME_OFFSET_BIN);
$time = bin2hex($time);
}
// https://tools.ietf.org/html/rfc4122#section-4.1.5
// We are using a random data for the sake of simplicity: since we are
// not able to get a super precise timeOfDay as a unique sequence
$clockSeq = random_int(0, 0x3FFF);
static $node;
if (null === $node) {
if (\function_exists('apcu_fetch')) {
$node = apcu_fetch('__symfony_uuid_node');
if (false === $node) {
$node = sprintf('%06x%06x',
random_int(0, 0xFFFFFF) | 0x010000,
random_int(0, 0xFFFFFF)
);
apcu_store('__symfony_uuid_node', $node);
}
} else {
$node = sprintf('%06x%06x',
random_int(0, 0xFFFFFF) | 0x010000,
random_int(0, 0xFFFFFF)
);
}
}
return sprintf('%08s-%04s-1%03s-%04x-%012s',
// 32 bits for "time_low"
substr($time, -8),
// 16 bits for "time_mid"
substr($time, -12, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 1
substr($time, -15, 3),
// 16 bits:
// * 8 bits for "clk_seq_hi_res",
// * 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
$clockSeq | 0x8000,
// 48 bits for "node"
$node
);
}
private static function isValid($uuid)
{
return (bool) preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid);
}
private static function parse($uuid)
{
if (!preg_match('{^(?<time_low>[0-9a-f]{8})-(?<time_mid>[0-9a-f]{4})-(?<version>[0-9a-f])(?<time_hi>[0-9a-f]{3})-(?<clock_seq>[0-9a-f]{4})-(?<node>[0-9a-f]{12})$}Di', $uuid, $matches)) {
return null;
}
return [
'time' => '0'.$matches['time_hi'].$matches['time_mid'].$matches['time_low'],
'version' => hexdec($matches['version']),
'clock_seq' => hexdec($matches['clock_seq']),
'node' => $matches['node'],
];
}
private static function toString($v)
{
if (\is_string($v) || null === $v || (\is_object($v) ? method_exists($v, '__toString') : \is_scalar($v))) {
return (string) $v;
}
return $v;
}
private static function toBinary($digits)
{
$bytes = '';
$count = \strlen($digits);
while ($count) {
$quotient = [];
$remainder = 0;
for ($i = 0; $i !== $count; ++$i) {
$carry = $digits[$i] + $remainder * 10;
$digit = $carry >> 8;
$remainder = $carry & 0xFF;
if ($digit || $quotient) {
$quotient[] = $digit;
}
}
$bytes = \chr($remainder).$bytes;
$count = \count($digits = $quotient);
}
return $bytes;
}
private static function toDecimal($bytes)
{
$digits = '';
$bytes = array_values(unpack('C*', $bytes));
while ($count = \count($bytes)) {
$quotient = [];
$remainder = 0;
for ($i = 0; $i !== $count; ++$i) {
$carry = $bytes[$i] + ($remainder << 8);
$digit = (int) ($carry / 10);
$remainder = $carry % 10;
if ($digit || $quotient) {
$quotient[] = $digit;
}
}
$digits = $remainder.$digits;
$bytes = $quotient;
}
return $digits;
}
private static function binaryAdd($a, $b)
{
$sum = 0;
for ($i = 7; 0 <= $i; --$i) {
$sum += \ord($a[$i]) + \ord($b[$i]);
$a[$i] = \chr($sum & 0xFF);
$sum >>= 8;
}
return $a;
}
}