diff --git a/CHANGELOG.md b/CHANGELOG.md index eb8d693..0b4fcad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,9 +61,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * Add `getTime()` method to `Provider\TimeProviderInterface`. * Change `Uuid::getFields()` to return an instance of `Fields\FieldsInterface`. Previously, it returned an array of integer values (on 64-bit systems only). -* Introduce `TimeConverterInterface $timeConverter` as fourth required +* Introduce `Converter\TimeConverterInterface $timeConverter` as fourth required constructor parameter for `Uuid` and second required constructor parameter for `Builder\DefaultUuidBuilder`. +* Introduce `Math\CalculatorInterface $calculator` as the fifth required + constructor parameter for `Uuid` and the third required constructor parameter + for `Builder\DefaultUuidBuilder`. * Change `UuidInterface::getInteger()` to always return a `string` value instead of `mixed`. This is a string representation of a 128-bit integer. You may then use a math library of your choice (bcmath, gmp, etc.) to operate on the @@ -89,15 +92,43 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Deprecated +The following functionality is deprecated and will be removed in ramsey/uuid +5.0.0. + +* The following methods from `UuidInterface` and `Uuid` are deprecated. Use their + counterparts on the `Rfc4122\FieldsInterface` returned by `Uuid::getFields()`. + * `getClockSeqHiAndReservedHex()` + * `getClockSeqLowHex()` + * `getClockSequenceHex()` + * `getFieldsHex()` + * `getNodeHex()` + * `getTimeHiAndVersionHex()` + * `getTimeLowHex()` + * `getTimeMidHex()` + * `getTimestampHex()` +* The following methods from `Uuid` are deprecated. Use the `Rfc4122\FieldsInterface` + instance returned by `Uuid::getFields()` to get the `Type\Hexadecimal` value + for these fields, and then use the arbitrary-precision arithmetic library of + your choice to convert them to string integers. + * `getClockSeqHiAndReserved()` + * `getClockSeqLow()` + * `getClockSequence()` + * `getNode()` + * `getTimeHiAndVersion()` + * `getTimeLow()` + * `getTimeMid()` + * `getTimestamp()` +* `UuidInterface::getNumberConverter()` and `Uuid::getNumberConverter()` are + deprecated. There is no alternative recommendation, so plan accordingly. * `Converter\Number\BigNumberConverter` is deprecated; transition to `Converter\Number\GenericNumberConverter`. * `Converter\Time\BigNumberTimeConverter` is deprecated; transition to `Converter\Time\GenericTimeConverter`. * `Provider\TimeProviderInterface::currentTime()` is deprecated; transition to the `getTimestamp()` method on the same interface. -* The classes for representing and generating *degraded* UUIDs are deprecated - and will be removed in ramsey/uuid 5.0.0. These are no longer necessary; this - library now behaves the same on 32-bit and 64-bit PHP. +* The classes for representing and generating *degraded* UUIDs are deprecated. + These are no longer necessary; this library now behaves the same on 32-bit and + 64-bit PHP. * `Builder\DegradedUuidBuilder` * `Converter\Number\DegradedNumberConverter` * `Converter\Time\DegradedTimeConverter` diff --git a/src/Builder/DefaultUuidBuilder.php b/src/Builder/DefaultUuidBuilder.php index dd4135c..dddbec1 100644 --- a/src/Builder/DefaultUuidBuilder.php +++ b/src/Builder/DefaultUuidBuilder.php @@ -17,6 +17,7 @@ namespace Ramsey\Uuid\Builder; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -37,6 +38,11 @@ class DefaultUuidBuilder implements UuidBuilderInterface */ private $timeConverter; + /** + * @var CalculatorInterface + */ + private $calculator; + /** * Constructs the DefaultUuidBuilder * @@ -44,21 +50,24 @@ class DefaultUuidBuilder implements UuidBuilderInterface * use when constructing the Uuid * @param TimeConverterInterface $timeConverter The time converter to use * for converting timestamps extracted from a UUID to Unix timestamps + * @param CalculatorInterface $calculator The calculator to use for + * performing mathematical operations on UUIDs */ public function __construct( NumberConverterInterface $numberConverter, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } /** * Builds and returns a Uuid * * @param CodecInterface $codec The codec to use for building this Uuid instance - * @param string[] $fields An array of fields from which to construct a Uuid instance; - * see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure. + * @param string[] $fields An array of fields from which to construct a Uuid instance * * @return Uuid The DefaultUuidBuilder returns an instance of Ramsey\Uuid\Uuid */ @@ -68,7 +77,8 @@ class DefaultUuidBuilder implements UuidBuilderInterface $fields, $this->numberConverter, $codec, - $this->timeConverter + $this->timeConverter, + $this->calculator ); } } diff --git a/src/Builder/DegradedUuidBuilder.php b/src/Builder/DegradedUuidBuilder.php index a1d9a13..212752f 100644 --- a/src/Builder/DegradedUuidBuilder.php +++ b/src/Builder/DegradedUuidBuilder.php @@ -19,6 +19,8 @@ use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\Time\DegradedTimeConverter; use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\DegradedUuid; +use Ramsey\Uuid\Math\BrickMathCalculator; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\UuidInterface; /** @@ -39,18 +41,27 @@ class DegradedUuidBuilder implements UuidBuilderInterface */ private $timeConverter; + /** + * @var CalculatorInterface + */ + private $calculator; + /** * @param NumberConverterInterface $numberConverter The number converter to * use when constructing the DegradedUuid - * @param TimeConverterInterface $timeConverter The time converter to use + * @param TimeConverterInterface|null $timeConverter The time converter to use * for converting timestamps extracted from a UUID to Unix timestamps + * @param CalculatorInterface|null $calculator The calculator to use for + * performing mathematical operations on UUIDs */ public function __construct( NumberConverterInterface $numberConverter, - ?TimeConverterInterface $timeConverter = null + ?TimeConverterInterface $timeConverter = null, + ?CalculatorInterface $calculator = null ) { $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter ?: new DegradedTimeConverter(); + $this->calculator = $calculator ?: new BrickMathCalculator(); } /** @@ -68,7 +79,8 @@ class DegradedUuidBuilder implements UuidBuilderInterface $fields, $this->numberConverter, $codec, - $this->timeConverter + $this->timeConverter, + $this->calculator ); } } diff --git a/src/Codec/OrderedTimeCodec.php b/src/Codec/OrderedTimeCodec.php index f1cc997..aff910d 100644 --- a/src/Codec/OrderedTimeCodec.php +++ b/src/Codec/OrderedTimeCodec.php @@ -16,6 +16,7 @@ namespace Ramsey\Uuid\Codec; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\UnsupportedOperationException; +use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -51,22 +52,23 @@ class OrderedTimeCodec extends StringCodec if ( $uuid->getVariant() !== Uuid::RFC_4122 || $uuid->getVersion() !== Uuid::UUID_TYPE_TIME + || !($uuid->getFields() instanceof FieldsInterface) ) { throw new InvalidArgumentException( - 'Expected version 1 (time-based) UUID; received ' - . var_export($uuid->toString(), true) + 'Expected RFC 4122 version 1 (time-based) UUID' ); } - $fields = $uuid->getFieldsHex(); + /** @var FieldsInterface $fields */ + $fields = $uuid->getFields(); $optimized = [ - $fields['time_hi_and_version'], - $fields['time_mid'], - $fields['time_low'], - $fields['clock_seq_hi_and_reserved'], - $fields['clock_seq_low'], - $fields['node'], + $fields->getTimeHiAndVersion()->toString(), + $fields->getTimeMid()->toString(), + $fields->getTimeLow()->toString(), + $fields->getClockSeqHiAndReserved()->toString(), + $fields->getClockSeqLow()->toString(), + $fields->getNode()->toString(), ]; return (string) hex2bin(implode('', $optimized)); diff --git a/src/Codec/StringCodec.php b/src/Codec/StringCodec.php index be63516..00ae218 100644 --- a/src/Codec/StringCodec.php +++ b/src/Codec/StringCodec.php @@ -17,6 +17,7 @@ namespace Ramsey\Uuid\Codec; use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\InvalidUuidStringException; +use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -47,11 +48,21 @@ class StringCodec implements CodecInterface */ public function encode(UuidInterface $uuid): string { - $fields = array_values($uuid->getFieldsHex()); + /** @var FieldsInterface $fields */ + $fields = $uuid->getFields(); + + $components = [ + $fields->getTimeLow()->toString(), + $fields->getTimeMid()->toString(), + $fields->getTimeHiAndVersion()->toString(), + $fields->getClockSeqHiAndReserved()->toString(), + $fields->getClockSeqLow()->toString(), + $fields->getNode()->toString(), + ]; return vsprintf( '%08s-%04s-%04s-%02s%02s-%012s', - $fields + $components ); } @@ -152,8 +163,6 @@ class StringCodec implements CodecInterface /** * Returns the fields that make up this UUID * - * @see \Ramsey\Uuid\UuidInterface::getFieldsHex() - * * @param string[] $components An array of hexadecimal strings representing * the fields of an RFC 4122 UUID * diff --git a/src/Codec/TimestampFirstCombCodec.php b/src/Codec/TimestampFirstCombCodec.php index f040b3c..bff41fa 100644 --- a/src/Codec/TimestampFirstCombCodec.php +++ b/src/Codec/TimestampFirstCombCodec.php @@ -16,6 +16,7 @@ namespace Ramsey\Uuid\Codec; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\InvalidUuidStringException; +use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\UuidInterface; /** @@ -49,7 +50,17 @@ class TimestampFirstCombCodec extends StringCodec */ public function encode(UuidInterface $uuid): string { - $sixPieceComponents = array_values($uuid->getFieldsHex()); + /** @var FieldsInterface $fields */ + $fields = $uuid->getFields(); + + $sixPieceComponents = [ + $fields->getTimeLow()->toString(), + $fields->getTimeMid()->toString(), + $fields->getTimeHiAndVersion()->toString(), + $fields->getClockSeqHiAndReserved()->toString(), + $fields->getClockSeqLow()->toString(), + $fields->getNode()->toString(), + ]; $sixPieceComponents = $this->swapTimestampAndRandomBits($sixPieceComponents); diff --git a/src/FeatureSet.php b/src/FeatureSet.php index e9d18ba..90614a0 100644 --- a/src/FeatureSet.php +++ b/src/FeatureSet.php @@ -324,12 +324,12 @@ class FeatureSet private function buildUuidBuilder(bool $useGuids = false): UuidBuilderInterface { if ($useGuids) { - return new GuidBuilder($this->numberConverter, $this->timeConverter); + return new GuidBuilder($this->numberConverter, $this->timeConverter, $this->calculator); } return new FallbackBuilder([ - new DefaultUuidBuilder($this->numberConverter, $this->timeConverter), - new NonstandardUuidBuilder($this->numberConverter, $this->timeConverter), + new DefaultUuidBuilder($this->numberConverter, $this->timeConverter, $this->calculator), + new NonstandardUuidBuilder($this->numberConverter, $this->timeConverter, $this->calculator), ]); } diff --git a/src/Guid/Guid.php b/src/Guid/Guid.php index d4ec86d..093f978 100644 --- a/src/Guid/Guid.php +++ b/src/Guid/Guid.php @@ -17,6 +17,7 @@ namespace Ramsey\Uuid\Guid; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -43,11 +44,13 @@ class Guid extends Uuid implements UuidInterface array $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->fields = new Fields((string) hex2bin(implode('', $fields))); $this->codec = $codec; $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } } diff --git a/src/Guid/GuidBuilder.php b/src/Guid/GuidBuilder.php index 5550836..4fc5e31 100644 --- a/src/Guid/GuidBuilder.php +++ b/src/Guid/GuidBuilder.php @@ -18,6 +18,7 @@ use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\UuidInterface; /** @@ -37,26 +38,34 @@ class GuidBuilder implements UuidBuilderInterface */ private $timeConverter; + /** + * @var CalculatorInterface + */ + private $calculator; + /** * @param NumberConverterInterface $numberConverter The number converter to * use when constructing the Guid * @param TimeConverterInterface $timeConverter The time converter to use * for converting timestamps extracted from a UUID to Unix timestamps + * @param CalculatorInterface $calculator The calculator to use for + * performing mathematical operations on UUIDs */ public function __construct( NumberConverterInterface $numberConverter, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } /** * Builds and returns a Guid * * @param CodecInterface $codec The codec to use for building this Guid instance - * @param string[] $fields An array of fields from which to construct a Guid instance; - * see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure. + * @param string[] $fields An array of fields from which to construct a Guid instance. * * @return Guid The GuidBuilder returns an instance of Ramsey\Uuid\Guid\Guid */ @@ -66,7 +75,8 @@ class GuidBuilder implements UuidBuilderInterface $fields, $this->numberConverter, $codec, - $this->timeConverter + $this->timeConverter, + $this->calculator ); } } diff --git a/src/Math/BrickMathCalculator.php b/src/Math/BrickMathCalculator.php index 3ffd07a..52839b5 100644 --- a/src/Math/BrickMathCalculator.php +++ b/src/Math/BrickMathCalculator.php @@ -128,6 +128,11 @@ final class BrickMathCalculator implements CalculatorInterface return new Hexadecimal($this->toBase($value, 16)); } + public function toIntegerValue(Hexadecimal $value): IntegerValue + { + return $this->fromBase($value->toString(), 16); + } + /** * Maps ramsey/uuid rounding modes to those used by brick/math */ diff --git a/src/Math/CalculatorInterface.php b/src/Math/CalculatorInterface.php index 752f051..93a9657 100644 --- a/src/Math/CalculatorInterface.php +++ b/src/Math/CalculatorInterface.php @@ -91,4 +91,9 @@ interface CalculatorInterface * Converts an IntegerValue instance to a Hexadecimal instance */ public function toHexadecimal(IntegerValue $value): Hexadecimal; + + /** + * Converts a Hexadecimal instance to an IntegerValue instance + */ + public function toIntegerValue(Hexadecimal $value): IntegerValue; } diff --git a/src/Nonstandard/Uuid.php b/src/Nonstandard/Uuid.php index f56c5fb..616d4cd 100644 --- a/src/Nonstandard/Uuid.php +++ b/src/Nonstandard/Uuid.php @@ -17,6 +17,7 @@ namespace Ramsey\Uuid\Nonstandard; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Uuid as Rfc4122Uuid; use Ramsey\Uuid\UuidInterface; @@ -34,11 +35,13 @@ class Uuid extends Rfc4122Uuid implements UuidInterface array $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->fields = new Fields((string) hex2bin(implode('', $fields))); $this->codec = $codec; $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } } diff --git a/src/Nonstandard/UuidBuilder.php b/src/Nonstandard/UuidBuilder.php index 627ff74..961940c 100644 --- a/src/Nonstandard/UuidBuilder.php +++ b/src/Nonstandard/UuidBuilder.php @@ -18,6 +18,7 @@ use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\UuidInterface; /** @@ -37,26 +38,34 @@ class UuidBuilder implements UuidBuilderInterface */ private $timeConverter; + /** + * @var CalculatorInterface + */ + private $calculator; + /** * @param NumberConverterInterface $numberConverter The number converter to * use when constructing the Nonstandard\Uuid * @param TimeConverterInterface $timeConverter The time converter to use * for converting timestamps extracted from a UUID to Unix timestamps + * @param CalculatorInterface $calculator The calculator to use for + * performing mathematical operations on UUIDs */ public function __construct( NumberConverterInterface $numberConverter, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } /** * Builds and returns a Nonstandard\Uuid * * @param CodecInterface $codec The codec to use for building this instance - * @param string[] $fields An array of fields from which to construct an instance; - * see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure. + * @param string[] $fields An array of fields from which to construct an instance. * * @return Uuid The Nonstandard\UuidBuilder returns an instance of * Nonstandard\Uuid @@ -67,7 +76,8 @@ class UuidBuilder implements UuidBuilderInterface $fields, $this->numberConverter, $codec, - $this->timeConverter + $this->timeConverter, + $this->calculator ); } } diff --git a/src/Uuid.php b/src/Uuid.php index be6a8de..1c32a26 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -20,9 +20,9 @@ use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\DateTimeException; -use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\Fields\FieldsInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; @@ -171,6 +171,11 @@ class Uuid implements UuidInterface */ protected $timeConverter; + /** + * @var CalculatorInterface + */ + protected $calculator; + /** * Creates a universally unique identifier (UUID) from an array of fields * @@ -195,17 +200,21 @@ class Uuid implements UuidInterface * UUID strings * @param TimeConverterInterface $timeConverter The time converter to use * for converting timestamps extracted from a UUID to unix timestamps + * @param CalculatorInterface $calculator The calculator to use for performing + * mathematical operations on UUIDs */ public function __construct( array $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, - TimeConverterInterface $timeConverter + TimeConverterInterface $timeConverter, + CalculatorInterface $calculator ) { $this->fields = new Fields((string) hex2bin(implode('', $fields))); $this->codec = $codec; $this->numberConverter = $numberConverter; $this->timeConverter = $timeConverter; + $this->calculator = $calculator; } public function __toString(): string @@ -242,7 +251,7 @@ class Uuid implements UuidInterface /** @var \Ramsey\Uuid\Uuid $uuid */ $uuid = self::fromString($serialized); $this->codec = $uuid->codec; - $this->numberConverter = $uuid->getNumberConverter(); + $this->numberConverter = $uuid->numberConverter; $this->fields = $uuid->fields; } @@ -282,56 +291,75 @@ class Uuid implements UuidInterface } /** - * Returns the high field of the clock sequence multiplexed with the variant + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getClockSeqHiAndReserved(): string { return $this->numberConverter->fromHex($this->fields->getClockSeqHiAndReserved()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()}. + */ public function getClockSeqHiAndReservedHex(): string { return $this->fields->getClockSeqHiAndReserved()->toString(); } /** - * Returns the low field of the clock sequence + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getClockSeqLow(): string { return $this->numberConverter->fromHex($this->fields->getClockSeqLow()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()}. + */ public function getClockSeqLowHex(): string { return $this->fields->getClockSeqLow()->toString(); } /** - * Returns the full clock sequence value - * - * For UUID version 1, the clock sequence is used to help avoid - * duplicates that could arise when the clock is set backwards in time - * or if the node ID changes. - * - * For UUID version 3 or 5, the clock sequence is a 14-bit value - * constructed from a name as described in RFC 4122, Section 4.3. - * - * For UUID version 4, clock sequence is a randomly or pseudo-randomly - * generated 14-bit value as described in RFC 4122, Section 4.4. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.5 RFC 4122, § 4.1.5: Clock Sequence + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getClockSequence(): string { return $this->numberConverter->fromHex($this->fields->getClockSeq()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()}. + */ public function getClockSequenceHex(): string { return $this->fields->getClockSeq()->toString(); } + /** + * @deprecated This method will be removed in 5.0.0. There is no alternative + * recommendation, so plan accordingly. + */ public function getNumberConverter(): NumberConverterInterface { return $this->numberConverter; @@ -349,7 +377,9 @@ class Uuid implements UuidInterface throw new UnsupportedOperationException('Not a time-based UUID'); } - $unixTime = $this->timeConverter->convertTime($this->getTimestamp()); + $unixTime = $this->timeConverter->convertTime( + $this->calculator->toIntegerValue($this->fields->getTimestamp())->toString() + ); try { return new DateTimeImmutable("@{$unixTime}"); @@ -371,6 +401,9 @@ class Uuid implements UuidInterface } /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. + * * @return string[] */ public function getFieldsHex(): array @@ -432,96 +465,99 @@ class Uuid implements UuidInterface } /** - * Returns the node value associated with this UUID - * - * For UUID version 1, the node field consists of an IEEE 802 MAC - * address, usually the host address. For systems with multiple IEEE - * 802 addresses, any available one can be used. The lowest addressed - * octet (octet number 10) contains the global/local bit and the - * unicast/multicast bit, and is the first octet of the address - * transmitted on an 802.3 LAN. - * - * For systems with no IEEE address, a randomly or pseudo-randomly - * generated value may be used; see RFC 4122, Section 4.5. The - * multicast bit must be set in such addresses, in order that they - * will never conflict with addresses obtained from network cards. - * - * For UUID version 3 or 5, the node field is a 48-bit value constructed - * from a name as described in RFC 4122, Section 4.3. - * - * For UUID version 4, the node field is a randomly or pseudo-randomly - * generated 48-bit value as described in RFC 4122, Section 4.4. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.6 RFC 4122, § 4.1.6: Node - * - * @return string Unsigned 48-bit integer value of node - * - * @throws UnsatisfiedDependencyException if large integer support is not available + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getNode()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getNode(): string { return $this->numberConverter->fromHex($this->fields->getNode()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getNode()}. + */ public function getNodeHex(): string { return $this->fields->getNode()->toString(); } /** - * Returns the high field of the timestamp multiplexed with the version + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getTimeHiAndVersion(): string { return $this->numberConverter->fromHex($this->fields->getTimeHiAndVersion()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()}. + */ public function getTimeHiAndVersionHex(): string { return $this->fields->getTimeHiAndVersion()->toString(); } /** - * Returns the low field of the timestamp + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getTimeLow(): string { return $this->numberConverter->fromHex($this->fields->getTimeLow()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()}. + */ public function getTimeLowHex(): string { return $this->fields->getTimeLow()->toString(); } /** - * Returns the middle field of the timestamp + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getTimeMid(): string { return $this->numberConverter->fromHex($this->fields->getTimeMid()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()}. + */ public function getTimeMidHex(): string { return $this->fields->getTimeMid()->toString(); } /** - * Returns the full timestamp value - * - * The 60 bit timestamp value is constructed from the time_low, - * time_mid, and time_hi fields of this UUID. The resulting - * timestamp is measured in 100-nanosecond units since midnight, - * October 15, 1582 UTC. - * - * The timestamp value is only meaningful in a time-based UUID, which - * has version type 1. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.4 RFC 4122, § 4.1.4: Timestamp - * - * @throws UnsatisfiedDependencyException if large integer support is not available - * @throws UnsupportedOperationException if UUID is not time-based + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} + * and use the arbitrary-precision math library of your choice to + * convert it to a string integer. */ public function getTimestamp(): string { @@ -532,6 +568,11 @@ class Uuid implements UuidInterface return $this->numberConverter->fromHex($this->fields->getTimestamp()->toString()); } + /** + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} + * instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()}. + */ public function getTimestampHex(): string { if ($this->getVersion() !== 1) { diff --git a/src/UuidInterface.php b/src/UuidInterface.php index 26b9710..eb0e1de 100644 --- a/src/UuidInterface.php +++ b/src/UuidInterface.php @@ -69,7 +69,8 @@ interface UuidInterface extends JsonSerializable, Serializable public function getFields(): FieldsInterface; /** - * Returns the number converter to use when converting hex values to/from integers + * @deprecated This method will be removed in 5.0.0. There is no alternative + * recommendation, so plan accordingly. */ public function getNumberConverter(): NumberConverterInterface; @@ -79,50 +80,34 @@ interface UuidInterface extends JsonSerializable, Serializable public function getHex(): string; /** - * Returns an array of the fields of the UUID, with keys named according - * to the RFC 4122 names for the fields - * - * * **time_low**: The low field of the timestamp, an unsigned 32-bit integer - * * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer - * * **time_hi_and_version**: The high field of the timestamp multiplexed with - * the version number, an unsigned 16-bit integer - * * **clock_seq_hi_and_reserved**: The high field of the clock sequence - * multiplexed with the variant, an unsigned 8-bit integer - * * **clock_seq_low**: The low field of the clock sequence, an unsigned - * 8-bit integer - * * **node**: The spatially unique node identifier, an unsigned 48-bit - * integer - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.2 RFC 4122, § 4.1.2: Layout and Byte Order + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. * * @return string[] */ public function getFieldsHex(): array; /** - * Returns the high field of the clock sequence multiplexed with the variant + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqHiAndReserved()}. */ public function getClockSeqHiAndReservedHex(): string; /** - * Returns the low field of the clock sequence + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqLow()}. */ public function getClockSeqLowHex(): string; /** - * Returns the full clock sequence value - * - * For UUID version 1, the clock sequence is used to help avoid - * duplicates that could arise when the clock is set backwards in time - * or if the node ID changes. - * - * For UUID version 3 or 5, the clock sequence is a 14-bit value - * constructed from a name as described in RFC 4122, Section 4.3. - * - * For UUID version 4, clock sequence is a randomly or pseudo-randomly - * generated 14-bit value as described in RFC 4122, Section 4.4. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.5 RFC 4122, § 4.1.5: Clock Sequence + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeq()}. */ public function getClockSequenceHex(): string; @@ -154,57 +139,42 @@ interface UuidInterface extends JsonSerializable, Serializable public function getMostSignificantBitsHex(): string; /** - * Returns the node value - * - * For UUID version 1, the node field consists of an IEEE 802 MAC - * address, usually the host address. For systems with multiple IEEE - * 802 addresses, any available one can be used. The lowest addressed - * octet (octet number 10) contains the global/local bit and the - * unicast/multicast bit, and is the first octet of the address - * transmitted on an 802.3 LAN. - * - * For systems with no IEEE address, a randomly or pseudo-randomly - * generated value may be used; see RFC 4122, Section 4.5. The - * multicast bit must be set in such addresses, in order that they - * will never conflict with addresses obtained from network cards. - * - * For UUID version 3 or 5, the node field is a 48-bit value constructed - * from a name as described in RFC 4122, Section 4.3. - * - * For UUID version 4, the node field is a randomly or pseudo-randomly - * generated 48-bit value as described in RFC 4122, Section 4.4. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.6 RFC 4122, § 4.1.6: Node + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getNode()}. */ public function getNodeHex(): string; /** - * Returns the high field of the timestamp multiplexed with the version + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeHiAndVersion()}. */ public function getTimeHiAndVersionHex(): string; /** - * Returns the low field of the timestamp + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeLow()}. */ public function getTimeLowHex(): string; /** - * Returns the middle field of the timestamp + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeMid()}. */ public function getTimeMidHex(): string; /** - * Returns the full timestamp value - * - * The 60 bit timestamp value is constructed from the time_low, - * time_mid, and time_hi fields of the UUID. The resulting - * timestamp is measured in 100-nanosecond units since midnight, - * October 15, 1582 UTC. - * - * The timestamp value is only meaningful in a time-based UUID, which - * has version type 1. - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.4 RFC 4122, § 4.1.4: Timestamp + * @deprecated Use {@see UuidInterface::getFields()} to get a + * {@see FieldsInterface} instance. If it is a + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call + * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimestamp()}. */ public function getTimestampHex(): string; diff --git a/static-analysis/UuidIsImmutable.php b/static-analysis/UuidIsImmutable.php index 0cedc3b..9fbea29 100644 --- a/static-analysis/UuidIsImmutable.php +++ b/static-analysis/UuidIsImmutable.php @@ -45,6 +45,7 @@ final class UuidIsImmutable * @return mixed[] * * @psalm-pure + * @psalm-suppress DeprecatedMethod */ public static function pureGetters(UuidInterface $a): array { diff --git a/tests/Builder/DefaultUuidBuilderTest.php b/tests/Builder/DefaultUuidBuilderTest.php index 43fafa2..a5102be 100644 --- a/tests/Builder/DefaultUuidBuilderTest.php +++ b/tests/Builder/DefaultUuidBuilderTest.php @@ -4,11 +4,12 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test\Builder; -use PHPUnit\Framework\MockObject\MockObject; +use Mockery; use Ramsey\Uuid\Builder\DefaultUuidBuilder; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; @@ -16,16 +17,12 @@ class DefaultUuidBuilderTest extends TestCase { public function testBuildCreatesUuid(): void { - /** @var MockObject & NumberConverterInterface $numberConverter */ - $numberConverter = $this->getMockBuilder(NumberConverterInterface::class)->getMock(); + $numberConverter = Mockery::mock(NumberConverterInterface::class); + $timeConverter = Mockery::mock(TimeConverterInterface::class); + $calculator = Mockery::mock(CalculatorInterface::class); + $codec = Mockery::mock(CodecInterface::class); - /** @var MockObject & TimeConverterInterface $timeConverter */ - $timeConverter = $this->getMockBuilder(TimeConverterInterface::class)->getMock(); - - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); - - /** @var MockObject & CodecInterface $codec */ - $codec = $this->getMockBuilder(CodecInterface::class)->getMock(); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $fields = [ 'time_low' => '754cd475', diff --git a/tests/Codec/GuidStringCodecTest.php b/tests/Codec/GuidStringCodecTest.php index bd6b2de..df4bab0 100644 --- a/tests/Codec/GuidStringCodecTest.php +++ b/tests/Codec/GuidStringCodecTest.php @@ -10,8 +10,10 @@ use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\GuidStringCodec; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; +use Ramsey\Uuid\Guid\Fields; use Ramsey\Uuid\Guid\Guid; use Ramsey\Uuid\Guid\GuidBuilder; +use Ramsey\Uuid\Math\CalculatorInterface; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\UuidInterface; @@ -28,7 +30,7 @@ class GuidStringCodecTest extends TestCase private $uuid; /** - * @var string[] + * @var Fields */ private $fields; @@ -37,14 +39,7 @@ class GuidStringCodecTest extends TestCase parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); $this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock(); - $this->fields = [ - 'time_low' => '12345678', - 'time_mid' => '1234', - 'time_hi_and_version' => '4bcd', - 'clock_seq_hi_and_reserved' => 'ab', - 'clock_seq_low' => 'ef', - 'node' => '1234abcd4321', - ]; + $this->fields = new Fields((string) hex2bin('785634123412cd4babef1234abcd4321')); } protected function tearDown(): void @@ -56,7 +51,7 @@ class GuidStringCodecTest extends TestCase public function testEncodeUsesFieldsArray(): void { $this->uuid->expects($this->once()) - ->method('getFieldsHex') + ->method('getFields') ->willReturn($this->fields); $codec = new GuidStringCodec($this->builder); $codec->encode($this->uuid); @@ -64,7 +59,7 @@ class GuidStringCodecTest extends TestCase public function testEncodeReturnsFormattedString(): void { - $this->uuid->method('getFieldsHex') + $this->uuid->method('getFields') ->willReturn($this->fields); $codec = new GuidStringCodec($this->builder); $result = $codec->encode($this->uuid); @@ -87,8 +82,9 @@ class GuidStringCodecTest extends TestCase $codec = new GuidStringCodec($this->builder); $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); + $calculator = Mockery::mock(CalculatorInterface::class); - $uuid = new Guid($fields, $numberConverter, $codec, $timeConverter); + $uuid = new Guid($fields, $numberConverter, $codec, $timeConverter, $calculator); $bytes = $codec->encodeBinary($uuid); @@ -101,7 +97,8 @@ class GuidStringCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new GuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new GuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new GuidStringCodec($builder); $guid = $codec->decode($string); diff --git a/tests/Codec/OrderedTimeCodecTest.php b/tests/Codec/OrderedTimeCodecTest.php index 8f2c633..799c8b2 100644 --- a/tests/Codec/OrderedTimeCodecTest.php +++ b/tests/Codec/OrderedTimeCodecTest.php @@ -13,6 +13,8 @@ use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\UnsupportedOperationException; +use Ramsey\Uuid\Math\CalculatorInterface; +use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidFactory; @@ -31,7 +33,7 @@ class OrderedTimeCodecTest extends TestCase private $uuid; /** - * @var string[] + * @var Fields */ private $fields; @@ -50,14 +52,7 @@ class OrderedTimeCodecTest extends TestCase parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); $this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock(); - $this->fields = [ - 'time_low' => '58e0a7d7', - 'time_mid' => 'eebc', - 'time_hi_and_version' => '11d8', - 'clock_seq_hi_and_reserved' => '96', - 'clock_seq_low' => '69', - 'node' => '0800200c9a66', - ]; + $this->fields = new Fields((string) hex2bin('58e0a7d7eebc11d896690800200c9a66')); } protected function tearDown(): void @@ -69,7 +64,7 @@ class OrderedTimeCodecTest extends TestCase public function testEncodeUsesFieldsArray(): void { $this->uuid->expects($this->once()) - ->method('getFieldsHex') + ->method('getFields') ->willReturn($this->fields); $codec = new OrderedTimeCodec($this->builder); $codec->encode($this->uuid); @@ -77,7 +72,7 @@ class OrderedTimeCodecTest extends TestCase public function testEncodeReturnsFormattedString(): void { - $this->uuid->method('getFieldsHex') + $this->uuid->method('getFields') ->willReturn($this->fields); $codec = new OrderedTimeCodec($this->builder); $result = $codec->encode($this->uuid); @@ -90,7 +85,8 @@ class OrderedTimeCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new OrderedTimeCodec($builder); $factory = new UuidFactory(); @@ -128,7 +124,8 @@ class OrderedTimeCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new OrderedTimeCodec($builder); $factory = new UuidFactory(); @@ -146,7 +143,8 @@ class OrderedTimeCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new OrderedTimeCodec($builder); $uuid = Mockery::mock(UuidInterface::class, [ @@ -156,8 +154,7 @@ class OrderedTimeCodecTest extends TestCase $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Expected version 1 (time-based) UUID; received ' - . "'{$nonRfc4122Uuid}'" + 'Expected RFC 4122 version 1 (time-based) UUID' ); $codec->encodeBinary($uuid); @@ -169,7 +166,8 @@ class OrderedTimeCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new OrderedTimeCodec($builder); $factory = new UuidFactory(); @@ -179,8 +177,7 @@ class OrderedTimeCodecTest extends TestCase $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( - 'Expected version 1 (time-based) UUID; received ' - . "'{$nonTimeBasedUuid}'" + 'Expected RFC 4122 version 1 (time-based) UUID' ); $codec->encodeBinary($uuid); @@ -216,7 +213,8 @@ class OrderedTimeCodecTest extends TestCase $numberConverter = Mockery::mock(NumberConverterInterface::class); $timeConverter = Mockery::mock(TimeConverterInterface::class); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $calculator = Mockery::mock(CalculatorInterface::class); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new OrderedTimeCodec($builder); $factory = new UuidFactory(); diff --git a/tests/Codec/StringCodecTest.php b/tests/Codec/StringCodecTest.php index 174604a..92e4d25 100644 --- a/tests/Codec/StringCodecTest.php +++ b/tests/Codec/StringCodecTest.php @@ -7,6 +7,7 @@ namespace Ramsey\Uuid\Test\Codec; use PHPUnit\Framework\MockObject\MockObject; use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\StringCodec; +use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\UuidInterface; @@ -23,27 +24,21 @@ class StringCodecTest extends TestCase private $uuid; /** - * @var string[] + * @var Fields */ private $fields; /** * @var string */ - private $uuidString = '12345678-1234-abcd-abef-1234abcd4321'; + private $uuidString = '12345678-1234-4bcd-abef-1234abcd4321'; protected function setUp(): void { parent::setUp(); $this->builder = $this->getMockBuilder(UuidBuilderInterface::class)->getMock(); $this->uuid = $this->getMockBuilder(UuidInterface::class)->getMock(); - $this->fields = ['time_low' => '12345678', - 'time_mid' => '1234', - 'time_hi_and_version' => 'abcd', - 'clock_seq_hi_and_reserved' => 'ab', - 'clock_seq_low' => 'ef', - 'node' => '1234abcd4321', - ]; + $this->fields = new Fields((string) hex2bin('1234567812344bcdabef1234abcd4321')); } protected function tearDown(): void @@ -55,7 +50,7 @@ class StringCodecTest extends TestCase public function testEncodeUsesFieldsArray(): void { $this->uuid->expects($this->once()) - ->method('getFieldsHex') + ->method('getFields') ->willReturn($this->fields); $codec = new StringCodec($this->builder); $codec->encode($this->uuid); @@ -63,7 +58,7 @@ class StringCodecTest extends TestCase public function testEncodeReturnsFormattedString(): void { - $this->uuid->method('getFieldsHex') + $this->uuid->method('getFields') ->willReturn($this->fields); $codec = new StringCodec($this->builder); $result = $codec->encode($this->uuid); @@ -84,10 +79,19 @@ class StringCodecTest extends TestCase public function testDecodeUsesBuilderOnFields(): void { - $string = 'uuid:12345678-1234-abcd-abef-1234abcd4321'; + $fields = [ + 'time_low' => $this->fields->getTimeLow()->toString(), + 'time_mid' => $this->fields->getTimeMid()->toString(), + 'time_hi_and_version' => $this->fields->getTimeHiAndVersion()->toString(), + 'clock_seq_hi_and_reserved' => $this->fields->getClockSeqHiAndReserved()->toString(), + 'clock_seq_low' => $this->fields->getClockSeqLow()->toString(), + 'node' => $this->fields->getNode()->toString(), + ]; + + $string = 'uuid:12345678-1234-4bcd-abef-1234abcd4321'; $this->builder->expects($this->once()) ->method('build') - ->with($this->isInstanceOf(StringCodec::class), $this->fields); + ->with($this->isInstanceOf(StringCodec::class), $fields); $codec = new StringCodec($this->builder); $codec->decode($string); } diff --git a/tests/Encoder/TimestampFirstCombCodecTest.php b/tests/Encoder/TimestampFirstCombCodecTest.php index 61e390e..d73ac3a 100644 --- a/tests/Encoder/TimestampFirstCombCodecTest.php +++ b/tests/Encoder/TimestampFirstCombCodecTest.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test\Encoder; +use Mockery; use PHPUnit\Framework\MockObject\MockObject; use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Codec\TimestampFirstCombCodec; +use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\UuidInterface; @@ -31,11 +33,12 @@ class TimestampFirstCombCodecTest extends TestCase public function testEncoding(): void { - /** @var MockObject & UuidInterface $uuidMock */ - $uuidMock = $this->getMockBuilder(UuidInterface::class)->getMock(); - $uuidMock->expects($this->any()) - ->method('getFieldsHex') - ->willReturn(['ff6f8cb0', 'c57d', '11e1', '9b', '21', '0800200c9a66']); + $fields = new Fields((string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66')); + + $uuidMock = Mockery::mock(UuidInterface::class, [ + 'getFields' => $fields, + ]); + $encodedUuid = $this->codec->encode($uuidMock); $this->assertSame('0800200c-9a66-11e1-9b21-ff6f8cb0c57d', $encodedUuid); @@ -43,11 +46,12 @@ class TimestampFirstCombCodecTest extends TestCase public function testBinaryEncoding(): void { - /** @var MockObject & UuidInterface $uuidMock */ - $uuidMock = $this->getMockBuilder(UuidInterface::class)->getMock(); - $uuidMock->expects($this->any()) - ->method('getFieldsHex') - ->willReturn(['ff6f8cb0', 'c57d', '11e1', '9b', '21', '0800200c9a66']); + $fields = new Fields((string) hex2bin('ff6f8cb0c57d11e19b210800200c9a66')); + + $uuidMock = Mockery::mock(UuidInterface::class, [ + 'getFields' => $fields, + ]); + $encodedUuid = $this->codec->encodeBinary($uuidMock); $this->assertSame(hex2bin('0800200c9a6611e19b21ff6f8cb0c57d'), $encodedUuid); diff --git a/tests/Encoder/TimestampLastCombCodecTest.php b/tests/Encoder/TimestampLastCombCodecTest.php index b26275a..7ef6c53 100644 --- a/tests/Encoder/TimestampLastCombCodecTest.php +++ b/tests/Encoder/TimestampLastCombCodecTest.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test\Encoder; +use Mockery; use PHPUnit\Framework\MockObject\MockObject; use Ramsey\Uuid\Builder\UuidBuilderInterface; use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Codec\TimestampLastCombCodec; +use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\UuidInterface; @@ -31,11 +33,12 @@ class TimestampLastCombCodecTest extends TestCase public function testEncoding(): void { - /** @var MockObject & UuidInterface $uuidMock */ - $uuidMock = $this->getMockBuilder(UuidInterface::class)->getMock(); - $uuidMock->expects($this->any()) - ->method('getFieldsHex') - ->willReturn(['0800200c', '9a66', '11e1', '9b', '21', 'ff6f8cb0c57d']); + $fields = new Fields((string) hex2bin('0800200c9a6611e19b21ff6f8cb0c57d')); + + $uuidMock = Mockery::mock(UuidInterface::class, [ + 'getFields' => $fields, + ]); + $encodedUuid = $this->codec->encode($uuidMock); $this->assertSame('0800200c-9a66-11e1-9b21-ff6f8cb0c57d', $encodedUuid); diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 594e9f1..9b8707a 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -24,6 +24,7 @@ use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Generator\RandomGeneratorFactory; use Ramsey\Uuid\Generator\RandomGeneratorInterface; use Ramsey\Uuid\Guid\Guid; +use Ramsey\Uuid\Math\BrickMathCalculator; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Type\Time; @@ -1407,13 +1408,14 @@ class UuidTest extends TestCase { $numberConverter = new BigNumberConverter(); $timeConverter = Mockery::mock(TimeConverterInterface::class); + $calculator = new BrickMathCalculator(); $timeConverter ->shouldReceive('convertTime') ->once() ->andReturn('foobar'); - $builder = new DefaultUuidBuilder($numberConverter, $timeConverter); + $builder = new DefaultUuidBuilder($numberConverter, $timeConverter, $calculator); $codec = new StringCodec($builder); $factory = new UuidFactory();