Add isNegative() to the Type\NumberInterface

This commit is contained in:
Ben Ramsey
2020-03-06 16:59:04 -06:00
parent b31703e7c9
commit 8a8d5d4ba8
7 changed files with 103 additions and 27 deletions
-25
View File
@@ -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
+5
View File
@@ -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`.
+14
View File
@@ -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;
+11
View File
@@ -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;
+4
View File
@@ -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;
}
+43 -1
View File
@@ -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,
],
];
}
+26 -1
View File
@@ -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,
],
];
}