diff --git a/docs/reference/references.rst b/docs/reference/references.rst deleted file mode 100644 index 4718b4e..0000000 --- a/docs/reference/references.rst +++ /dev/null @@ -1,25 +0,0 @@ -.. _reference.references: - -========== -References -========== - -.. [DCE11SEC] Open Group. (1997). *DCE 1.1: Authentication and Security Services* - (Document No. C311). Retrieved from https://publications.opengroup.org/c311 - -.. [RFC4122] Leach, P., Mealling, M., & Salz, R. (2005, July). A universally - unique identifier (UUID) URN namespace. *Internet Requests for Comments* - (RFC 4122). RFC Editor. Retrieved from https://www.rfc-editor.org/rfc/rfc4122.html - -.. [UUIDV6] Peabody, B. (2020, February 24). UUID format update. *Internet - Drafts* (draft-peabody-dispatch-new-uuid-format-00). Working Draft, IETF - Secretariat. Retrieved from - https://tools.ietf.org/html/draft-peabody-dispatch-new-uuid-format-00 - -.. [RFC1321] Rivest, R. (1992, April). The MD5 message-digest algorithm. - *Internet Requests for Comments* (RFC 1321). RFC Editor. Retrieved from - https://www.rfc-editor.org/rfc/rfc1321.html - -.. [SHA1] U.S. Department of Commerce, National Institute of Standards and - Technology. (2015). *Secure Hash Standard (SHS)* (FIPS PUB 180-4). Retrieved - from http://dx.doi.org/10.6028/NIST.FIPS.180-4 diff --git a/docs/reference/types.rst b/docs/reference/types.rst index 0ee432b..8212eda 100644 --- a/docs/reference/types.rst +++ b/docs/reference/types.rst @@ -26,6 +26,11 @@ Types NumberInterface ensures consistency in numeric values returned by ramsey/uuid. + .. php:method:: isNegative() + + :returns: True if this number is less than zero, false otherwise. + :returntype: ``bool`` + .. php:class:: Decimal Implements :php:interface:`Ramsey\\Uuid\\Type\\NumberInterface`. diff --git a/src/Type/Decimal.php b/src/Type/Decimal.php index 982e0f5..5ba8865 100644 --- a/src/Type/Decimal.php +++ b/src/Type/Decimal.php @@ -37,6 +37,11 @@ final class Decimal implements NumberInterface */ private $value; + /** + * @var bool + */ + private $isNegative = false; + /** * @param mixed $value The decimal value to store */ @@ -61,9 +66,18 @@ final class Decimal implements NumberInterface $value = '0'; } + if (strpos($value, '-') === 0) { + $this->isNegative = true; + } + $this->value = $value; } + public function isNegative(): bool + { + return $this->isNegative; + } + public function toString(): string { return $this->value; diff --git a/src/Type/Integer.php b/src/Type/Integer.php index 0be97a7..05d420a 100644 --- a/src/Type/Integer.php +++ b/src/Type/Integer.php @@ -40,6 +40,11 @@ final class Integer implements NumberInterface */ private $value; + /** + * @var bool + */ + private $isNegative = false; + /** * @param mixed $value The integer value to store */ @@ -72,11 +77,17 @@ final class Integer implements NumberInterface // Add the negative sign back to the value. if ($sign === '-' && $value !== '0') { $value = $sign . $value; + $this->isNegative = true; } $this->value = $value; } + public function isNegative(): bool + { + return $this->isNegative; + } + public function toString(): string { return $this->value; diff --git a/src/Type/NumberInterface.php b/src/Type/NumberInterface.php index fdfa535..bf4ae9d 100644 --- a/src/Type/NumberInterface.php +++ b/src/Type/NumberInterface.php @@ -21,4 +21,8 @@ namespace Ramsey\Uuid\Type; */ interface NumberInterface extends TypeInterface { + /** + * Returns true if this number is less than zero + */ + public function isNegative(): bool; } diff --git a/tests/Type/DecimalTest.php b/tests/Type/DecimalTest.php index 09c0a6c..6870069 100644 --- a/tests/Type/DecimalTest.php +++ b/tests/Type/DecimalTest.php @@ -20,12 +20,13 @@ class DecimalTest extends TestCase * * @dataProvider provideDecimal */ - public function testDecimalValueType($value, string $expected): void + public function testDecimalValueType($value, string $expected, bool $expectedIsNegative): void { $decimal = new Decimal($value); $this->assertSame($expected, $decimal->toString()); $this->assertSame($expected, (string) $decimal); + $this->assertSame($expectedIsNegative, $decimal->isNegative()); } /** @@ -37,166 +38,207 @@ class DecimalTest extends TestCase [ 'value' => '-11386878954224802805705605120', 'expected' => '-11386878954224802805705605120', + 'expectedIsNegative' => true, ], [ 'value' => '-9223372036854775808', 'expected' => '-9223372036854775808', + 'expectedIsNegative' => true, ], [ 'value' => -99986838650880, 'expected' => '-99986838650880', + 'expectedIsNegative' => true, ], [ 'value' => -4294967296, 'expected' => '-4294967296', + 'expectedIsNegative' => true, ], [ 'value' => -2147483649, 'expected' => '-2147483649', + 'expectedIsNegative' => true, ], [ 'value' => -123456.0, 'expected' => '-123456', + 'expectedIsNegative' => true, ], [ 'value' => -1.00000000000001, 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => -1, 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => '-1', 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => 0, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => -0, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '-0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '+0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => 1, 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => '1', 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => '+1', 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => 1.00000000000001, 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => 123456.0, 'expected' => '123456', + 'expectedIsNegative' => false, ], [ 'value' => 2147483648, 'expected' => '2147483648', + 'expectedIsNegative' => false, ], [ 'value' => 4294967294, 'expected' => '4294967294', + 'expectedIsNegative' => false, ], [ 'value' => 99965363767850, 'expected' => '99965363767850', + 'expectedIsNegative' => false, ], [ 'value' => '9223372036854775808', 'expected' => '9223372036854775808', + 'expectedIsNegative' => false, ], [ 'value' => '11386878954224802805705605120', 'expected' => '11386878954224802805705605120', + 'expectedIsNegative' => false, ], [ 'value' => -9223372036854775809, 'expected' => '-9.2233720368548E+18', + 'expectedIsNegative' => true, ], [ 'value' => 9223372036854775808, 'expected' => '9.2233720368548E+18', + 'expectedIsNegative' => false, ], [ 'value' => -123456.789, 'expected' => '-123456.789', + 'expectedIsNegative' => true, ], [ 'value' => -1.0000000000001, 'expected' => '-1.0000000000001', + 'expectedIsNegative' => true, ], [ 'value' => -0.5, 'expected' => '-0.5', + 'expectedIsNegative' => true, ], [ 'value' => 0.5, 'expected' => '0.5', + 'expectedIsNegative' => false, ], [ 'value' => 1.0000000000001, 'expected' => '1.0000000000001', + 'expectedIsNegative' => false, ], [ 'value' => 123456.789, 'expected' => '123456.789', + 'expectedIsNegative' => false, ], [ 'value' => '-0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '+0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => -0.00000000, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => 0.00000000, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => -0.0001, 'expected' => '-0.0001', + 'expectedIsNegative' => true, ], [ 'value' => 0.0001, 'expected' => '0.0001', + 'expectedIsNegative' => false, ], [ 'value' => -0.00001, 'expected' => '-1.0E-5', + 'expectedIsNegative' => true, ], [ 'value' => 0.00001, 'expected' => '1.0E-5', + 'expectedIsNegative' => false, ], [ 'value' => '+1234.56', 'expected' => '1234.56', + 'expectedIsNegative' => false, ], ]; } diff --git a/tests/Type/IntegerTest.php b/tests/Type/IntegerTest.php index d2c2cbb..c57e656 100644 --- a/tests/Type/IntegerTest.php +++ b/tests/Type/IntegerTest.php @@ -20,12 +20,13 @@ class IntegerTest extends TestCase * * @dataProvider provideInteger */ - public function testIntegerType($value, string $expected): void + public function testIntegerType($value, string $expected, bool $expectedIsNegative): void { $integer = new IntegerObject($value); $this->assertSame($expected, $integer->toString()); $this->assertSame($expected, (string) $integer); + $this->assertSame($expectedIsNegative, $integer->isNegative()); } /** @@ -37,98 +38,122 @@ class IntegerTest extends TestCase [ 'value' => '-11386878954224802805705605120', 'expected' => '-11386878954224802805705605120', + 'expectedIsNegative' => true, ], [ 'value' => '-9223372036854775808', 'expected' => '-9223372036854775808', + 'expectedIsNegative' => true, ], [ 'value' => -99986838650880, 'expected' => '-99986838650880', + 'expectedIsNegative' => true, ], [ 'value' => -4294967296, 'expected' => '-4294967296', + 'expectedIsNegative' => true, ], [ 'value' => -2147483649, 'expected' => '-2147483649', + 'expectedIsNegative' => true, ], [ 'value' => -123456.0, 'expected' => '-123456', + 'expectedIsNegative' => true, ], [ 'value' => -1.00000000000001, 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => -1, 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => '-1', 'expected' => '-1', + 'expectedIsNegative' => true, ], [ 'value' => 0, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => -0, 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '-0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => '+0', 'expected' => '0', + 'expectedIsNegative' => false, ], [ 'value' => 1, 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => '1', 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => '+1', 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => 1.00000000000001, 'expected' => '1', + 'expectedIsNegative' => false, ], [ 'value' => 123456.0, 'expected' => '123456', + 'expectedIsNegative' => false, ], [ 'value' => 2147483648, 'expected' => '2147483648', + 'expectedIsNegative' => false, ], [ 'value' => 4294967294, 'expected' => '4294967294', + 'expectedIsNegative' => false, ], [ 'value' => 99965363767850, 'expected' => '99965363767850', + 'expectedIsNegative' => false, ], [ 'value' => '9223372036854775808', 'expected' => '9223372036854775808', + 'expectedIsNegative' => false, ], [ 'value' => '11386878954224802805705605120', 'expected' => '11386878954224802805705605120', + 'expectedIsNegative' => false, ], ]; }