Version 3 to 4

I’ve made great efforts to ensure that the upgrade experience for most will be seamless and uneventful. However, no matter the degree to which you use ramsey/uuid (customized or unchanged), there are a number of things to be aware of as you upgrade your code to use version 4.

Tip

These are the changes that are most likely to affect you. For a full list of changes, take a look at the 4.0.0 changelog.

What’s New?

There are a lot of new features in ramsey/uuid! Here are a few of them:

What’s Changed?

Attention

ramsey/uuid version 4 requires PHP 7.2 or later.

Quite a bit has changed, but much remains familiar. Unless you’ve changed the behavior of ramsey/uuid through custom codecs, providers, generators, etc., the standard functionality and API found in version 3 will not differ much.

Here are the highlights:

  • ramsey/uuid now works on 32-bit and 64-bit systems, with no degradation in functionality! All Degraded* classes are deprecated and no longer used; they’ll go away in ramsey/uuid version 5.

  • Pay attention to the return types for the static methods on the Uuid class. They’ve changed slightly, but this won’t affect you if your type hints use UuidInterface.

  • The return types for three methods defined on UuidInterface have changed, breaking backwards compatibility. Take note and update your code.

  • There are a number of deprecations. These shouldn’t affect you now, but please take a look at the recommendations and update your code soon. These will go away in ramsey/uuid version 5.

  • ramsey/uuid now throws custom exceptions for everything. The exception UnsatisfiedDependencyException no longer exists.

  • If you customize ramsey/uuid at all by implementing the interfaces, take a look at the interface and constructor changes and update your code.

Tip

If you maintain a public project that uses ramsey/uuid version 3 and you find that your code does not require any changes to upgrade to version 4, consider using the following version constraint in your project’s composer.json file:

composer require ramsey/uuid:"^3 || ^4"

This will allow any downstream users of your project who aren’t ready to upgrade to version 4 the ability to continue using your project while deciding on an appropriate upgrade schedule.

If your downstream users do not specify ramsey/uuid as a dependency, and they use functionality specific to version 3, they may need to update their own Composer dependencies to use ramsey/uuid ^3 to avoid using version 4.

Uuid Static Methods

All the static methods on the Uuid class continue to work as they did in version 3, with this slight change: they now return more-specific types, all of which implement the new interface Rfc4122\UuidInterface, which implements the familiar interface UuidInterface.

If your type hints are for UuidInterface, then you should not require any changes.

Return types for Uuid static methods

Method

3.x Returned

4.x Returns

Uuid::uuid1()

Uuid

Rfc4122\UuidV1

Uuid::uuid3()

Uuid

Rfc4122\UuidV3

Uuid::uuid4()

Uuid

Rfc4122\UuidV4

Uuid::uuid5()

Uuid

Rfc4122\UuidV5

Uuid::fromString(), Uuid::fromBytes(), and Uuid::fromInteger() all return an appropriate more-specific type, based on the input value. If the input value is a version 1 UUID, for example, the return type will be an Rfc4122\UuidV1. If the input looks like a UUID or is a 128-bit number, but it doesn’t validate as an RFC 4122 UUID, the return type will be a Nonstandard\Uuid. These return types implement UuidInterface. If using this as a type hint, you shouldn’t need to make any changes.

Changed Return Types

The following UuidInterface method return types have changed in version 4 and you will need to update your code, if you use these methods.

Changed UuidInterface method return types

Method

3.x Returned

4.x Returns

UuidInterface::getFields()

array

Rfc4122\FieldsInterface

UuidInterface::getHex()

string

Type\Hexadecimal

UuidInterface::getInteger()

mixed 1

Type\Integer

In version 3, the following Uuid methods return int, string, or Moontoast\Math\BigNumber, depending on the environment. In version 4, they all return numeric string values for the sake of consistency. These methods are also deprecated and will be removed in version 5.

  • getClockSeqHiAndReserved()

  • getClockSeqLow()

  • getClockSequence()

  • getLeastSignificantBits()

  • getMostSignificantBits()

  • getNode()

  • getTimeHiAndVersion()

  • getTimeLow()

  • getTimeMid()

  • getTimestamp()

Deprecations

UuidInterface

The following UuidInterface methods are deprecated, but upgrading to version 4 should not cause any problems if using these methods. You are encouraged to update your code according to the recommendations, though, since these methods will go away in version 5.

Deprecated UuidInterface methods

Deprecated Method

Update To

getDateTime()

Use getDateTime() on UuidV1, UuidV2, or UuidV6

getClockSeqHiAndReservedHex()

getFields()->getClockSeqHiAndReserved()->toString()

getClockSeqLowHex()

getFields()->getClockSeqLow()->toString()

getClockSequenceHex()

getFields()->getClockSeq()->toString()

getFieldsHex()

getFields() 2

getLeastSignificantBitsHex()

substr($uuid->getHex()->toString(), 0, 16)

getMostSignificantBitsHex()

substr($uuid->getHex()->toString(), 16)

getNodeHex()

getFields()->getNode()->toString()

getNumberConverter()

This method has no replacement; plan accordingly.

getTimeHiAndVersionHex()

getFields()->getTimeHiAndVersion()->toString()

getTimeLowHex()

getFields()->getTimeLow()->toString()

getTimeMidHex()

getFields()->getTimeMid()->toString()

getTimestampHex()

getFields()->getTimestamp()->toString()

getUrn()

Ramsey\Uuid\Rfc4122\UuidInterface::getUrn

getVariant()

getFields()->getVariant()

getVersion()

getFields()->getVersion()

Uuid

Uuid as an instantiable class is deprecated. In ramsey/uuid version 5, its constructor will be private, and the class will be final. For more information, see Why does ramsey/uuid use final?

Note

Uuid is being replaced by more-specific concrete classes, such as:

However, the Uuid class isn’t going away. It will still hold common constants and static methods.

  • Uuid::UUID_TYPE_IDENTIFIER is deprecated. Use Uuid::UUID_TYPE_DCE_SECURITY instead.

  • Uuid::VALID_PATTERN is deprecated. Use the following instead:

    use Ramsey\Uuid\Validator\GenericValidator;
    use Ramsey\Uuid\Rfc4122\Validator as Rfc4122Validator;
    
    $genericPattern = (new GenericValidator())->getPattern();
    $rfc4122Pattern = (new Rfc4122Validator())->getPattern();
    

The following Uuid methods are deprecated. If using these methods, you shouldn’t have any problems on version 4, but you are encouraged to update your code, since they will go away in version 5.

  • getClockSeqHiAndReserved()

  • getClockSeqLow()

  • getClockSequence()

  • getLeastSignificantBits()

  • getMostSignificantBits()

  • getNode()

  • getTimeHiAndVersion()

  • getTimeLow()

  • getTimeMid()

  • getTimestamp()

Hint

There are no direct replacements for these methods. In ramsey/uuid version 3, they returned int or Moontoast\Math\BigNumber values, depending on the environment. To update your code, you should use the recommended alternates listed in Deprecations: UuidInterface, combined with the arbitrary-precision mathematics library of your choice (e.g., brick/math, gmp, bcmath, etc.).

Using brick/math to convert a node to a string integer
use Brick\Math\BigInteger;

$node = BigInteger::fromBase($uuid->getFields()->getNode()->toString(), 16);

Interface Changes

For those who customize ramsey/uuid by implementing the interfaces provided, there are a few breaking changes to note.

Hint

Most existing methods on interfaces have type hints added to them. If you implement any interfaces, please be aware of this and update your classes.

UuidInterface

Method

Description

__toString()

New method; returns string

getDateTime()

Deprecated; now returns DateTimeInterface

getFields()

Used to return array; now returns Rfc4122\FieldsInterface

getHex()

Used to return string; now returns Type\Hexadecimal

getInteger()

New method; returns Type\Integer

UuidFactoryInterface

Method

Description

uuid2()

New method; returns Rfc4122\UuidV2

uuid6()

New method; returns Nonstandard\UuidV6

fromDateTime()

New method; returns UuidInterface

fromInteger()

Changed to accept only strings

getValidator()

New method; returns UuidInterface

Builder\UuidBuilderInterface

Method

Description

build()

The second parameter used to accept array $fields; now accepts string $bytes

Converter\TimeConverterInterface

Method

Description

calculateTime()

Used to return string[]; now returns Type\Hexadecimal

convertTime()

New method; returns Type\Time

Provider\TimeProviderInterface

Method

Description

currentTime()

Method removed from interface; use getTime() instead

getTime()

New method; returns Type\Time

Provider\NodeProviderInterface

Method

Description

getNode()

Used to return string|false|null; now returns Type\Hexadecimal

Constructor Changes

There are a handful of constructor changes that might affect your use of ramsey/uuid, especially if you customize the library.

Uuid

The constructor for Ramsey\Uuid\Uuid is deprecated. However, there are a few changes to it that might affect your use of this class.

The first constructor parameter used to be array $fields and is now Rfc4122\FieldsInterface $fields.

Converter\TimeConverterInterface $timeConverter is required as a new fourth parameter.

Builder\DefaultUuidBuilder

While Builder\DefaultUuidBuilder is deprecated, it now inherits from Rfc4122\UuidBuilder, which requires Converter\TimeConverterInterface $timeConverter as its second constructor argument.

Provider\Node\FallbackNodeProvider

Provider\Node\FallbackNodeProvider now requires iterable<Ramsey\Uuid\Provider\NodeProviderInterface> as its constructor parameter.

use MyPackage\MyCustomNodeProvider;
use Ramsey\Uuid\Provider\Node\FallbackNodeProvider;
use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Provider\Node\SystemNodeProvider;

$nodeProviders = [];
$nodeProviders[] = new MyCustomNodeProvider();
$nodeProviders[] = new SystemNodeProvider();
$nodeProviders[] = new RandomNodeProvider();

$provider = new FallbackNodeProvider($nodeProviders);

Provider\Time\FixedTimeProvider

The constructor for Provider\Time\FixedTimeProvider no longer accepts an array. It accepts Type\Time instances.


Footnotes

1

This mixed return type could have been an int, string, or Moontoast\Math\BigNumber. In version 4, ramsey/uuid cleans this up for the sake of consistency.

2

The getFields() method returns a Type\Hexadecimal instance; you will need to construct an array if you wish to match the return value of the deprecated getFieldsHex() method.