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:
Support version 6 UUIDs.
Support version 2 (DCE Security) UUIDs.
Add classes to represent each version of RFC 4122 UUID. When generating new UUIDs or creating UUIDs from existing strings, bytes, or integers, if the UUID is an RFC 4122 variant, one of these instances will be returned:
Rfc4122\NilUuid
Add classes to represent version 6 UUIDs, GUIDs, and nonstandard (non-RFC 4122 variants) UUIDs:
Add
Uuid::fromDateTime()to create version 1 UUIDs from instances of DateTimeInterface.
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
Uuidclass. They’ve changed slightly, but this won’t affect you if your type hints useUuidInterface.The return types for three methods defined on
UuidInterfacehave 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.
Method |
3.x Returned |
4.x Returns |
|---|---|---|
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.
Method |
3.x Returned |
4.x Returns |
|---|---|---|
|
||
|
||
|
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 Method |
Update To |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This method has no replacement; plan accordingly. |
|
|
|
|
|
|
|
|
|
|
|
|
|
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_IDENTIFIERis deprecated. UseUuid::UUID_TYPE_DCE_SECURITYinstead.Uuid::VALID_PATTERNis 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.).
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 |
|---|---|
New method; returns |
|
|
Deprecated; now returns DateTimeInterface |
Used to return |
|
Used to return |
|
New method; returns |
UuidFactoryInterface
Method |
Description |
|---|---|
New method; returns |
|
New method; returns |
|
New method; returns |
|
Changed to accept only strings |
|
New method; returns |
Builder\UuidBuilderInterface
Method |
Description |
|---|---|
|
The second parameter used to accept |
Converter\TimeConverterInterface
Method |
Description |
|---|---|
|
Used to return |
|
New method; returns |
Provider\TimeProviderInterface
Method |
Description |
|---|---|
|
Method removed from interface; use |
|
New method; returns |
Provider\NodeProviderInterface
Method |
Description |
|---|---|
|
Used to return |
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