← All changes
|
includes/sdk/google/brick/math/src/BigDecimal.php
+101
-196
1.2.9
→
1.4.1
View file →
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | declare (strict_types=1); |
| 4 | -namespace Dudlewebs\WPMCS\Brick\Math; | |
| 4 | +namespace Dudlewebs\WPMCS\GCP\Brick\Math; | |
| 5 | 5 | |
| 6 | -use Dudlewebs\WPMCS\Brick\Math\Exception\DivisionByZeroException; | |
| 7 | -use Dudlewebs\WPMCS\Brick\Math\Exception\MathException; | |
| 8 | -use Dudlewebs\WPMCS\Brick\Math\Exception\NegativeNumberException; | |
| 9 | -use Dudlewebs\WPMCS\Brick\Math\Internal\Calculator; | |
| 6 | +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\DivisionByZeroException; | |
| 7 | +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\MathException; | |
| 8 | +use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\NegativeNumberException; | |
| 9 | +use Dudlewebs\WPMCS\GCP\Brick\Math\Internal\Calculator; | |
| 10 | +use Dudlewebs\WPMCS\GCP\Override; | |
| 10 | 11 | /** |
| 11 | 12 | * Immutable, arbitrary-precision signed decimal numbers. |
| 12 | 13 | * |
| 13 | 14 | * @psalm-immutable |
| @@ -19,20 +20,16 @@ | ||
| 19 | 20 | * |
| 20 | 21 | * This is a string of digits with an optional leading minus sign. |
| 21 | 22 | * No leading zero must be present. |
| 22 | 23 | * No leading minus sign must be present if the value is 0. |
| 23 | - * | |
| 24 | - * @var string | |
| 25 | 24 | */ |
| 26 | - private $value; | |
| 25 | + private readonly string $value; | |
| 27 | 26 | /** |
| 28 | 27 | * The scale (number of digits after the decimal point) of this decimal number. |
| 29 | 28 | * |
| 30 | 29 | * This must be zero or more. |
| 31 | - * | |
| 32 | - * @var int | |
| 33 | 30 | */ |
| 34 | - private $scale; | |
| 31 | + private readonly int $scale; | |
| 35 | 32 | /** |
| 36 | 33 | * Protected constructor. Use a factory method to obtain an instance. |
| 37 | 34 | * |
| 38 | 35 | * @param string $value The unscaled value, validated. |
| @@ -43,21 +40,14 @@ | ||
| 43 | 40 | $this->value = $value; |
| 44 | 41 | $this->scale = $scale; |
| 45 | 42 | } |
| 46 | 43 | /** |
| 47 | - * Creates a BigDecimal of the given value. | |
| 48 | - * | |
| 49 | - * @param BigNumber|int|float|string $value | |
| 50 | - * | |
| 51 | - * @return BigDecimal | |
| 52 | - * | |
| 53 | - * @throws MathException If the value cannot be converted to a BigDecimal. | |
| 54 | - * | |
| 55 | 44 | * @psalm-pure |
| 56 | 45 | */ |
| 57 | - public static function of($value): BigNumber | |
| 46 | + #[\Override] | |
| 47 | + protected static function from(BigNumber $number) : static | |
| 58 | 48 | { |
| 59 | - return parent::of($value)->toBigDecimal(); | |
| 49 | + return $number->toBigDecimal(); | |
| 60 | 50 | } |
| 61 | 51 | /** |
| 62 | 52 | * Creates a BigDecimal from an unscaled value and a scale. |
| 63 | 53 | * |
| @@ -63,31 +53,30 @@ | ||
| 63 | 53 | * |
| 64 | 54 | * Example: `(12345, 3)` will result in the BigDecimal `12.345`. |
| 65 | 55 | * |
| 66 | 56 | * @param BigNumber|int|float|string $value The unscaled value. Must be convertible to a BigInteger. |
| 67 | - * @param int $scale The scale of the number, positive or zero. | |
| 57 | + * @param int $scale The scale of the number. If negative, the scale will be set to zero | |
| 58 | + * and the unscaled value will be adjusted accordingly. | |
| 68 | 59 | * |
| 69 | - * @return BigDecimal | |
| 70 | - * | |
| 71 | - * @throws \InvalidArgumentException If the scale is negative. | |
| 72 | - * | |
| 73 | 60 | * @psalm-pure |
| 74 | 61 | */ |
| 75 | - public static function ofUnscaledValue($value, int $scale = 0): BigDecimal | |
| 62 | + public static function ofUnscaledValue(BigNumber|int|float|string $value, int $scale = 0) : BigDecimal | |
| 76 | 63 | { |
| 64 | + $value = (string) BigInteger::of($value); | |
| 77 | 65 | if ($scale < 0) { |
| 78 | - throw new \InvalidArgumentException('The scale cannot be negative.'); | |
| 66 | + if ($value !== '0') { | |
| 67 | + $value .= \str_repeat('0', -$scale); | |
| 68 | + } | |
| 69 | + $scale = 0; | |
| 79 | 70 | } |
| 80 | - return new BigDecimal((string) BigInteger::of($value), $scale); | |
| 71 | + return new BigDecimal($value, $scale); | |
| 81 | 72 | } |
| 82 | 73 | /** |
| 83 | 74 | * Returns a BigDecimal representing zero, with a scale of zero. |
| 84 | 75 | * |
| 85 | - * @return BigDecimal | |
| 86 | - * | |
| 87 | 76 | * @psalm-pure |
| 88 | 77 | */ |
| 89 | - public static function zero(): BigDecimal | |
| 78 | + public static function zero() : BigDecimal | |
| 90 | 79 | { |
| 91 | 80 | /** |
| 92 | 81 | * @psalm-suppress ImpureStaticVariable |
| 93 | 82 | * @var BigDecimal|null $zero |
| @@ -100,13 +89,11 @@ | ||
| 100 | 89 | } |
| 101 | 90 | /** |
| 102 | 91 | * Returns a BigDecimal representing one, with a scale of zero. |
| 103 | 92 | * |
| 104 | - * @return BigDecimal | |
| 105 | - * | |
| 106 | 93 | * @psalm-pure |
| 107 | 94 | */ |
| 108 | - public static function one(): BigDecimal | |
| 95 | + public static function one() : BigDecimal | |
| 109 | 96 | { |
| 110 | 97 | /** |
| 111 | 98 | * @psalm-suppress ImpureStaticVariable |
| 112 | 99 | * @var BigDecimal|null $one |
| @@ -119,13 +106,11 @@ | ||
| 119 | 106 | } |
| 120 | 107 | /** |
| 121 | 108 | * Returns a BigDecimal representing ten, with a scale of zero. |
| 122 | 109 | * |
| 123 | - * @return BigDecimal | |
| 124 | - * | |
| 125 | 110 | * @psalm-pure |
| 126 | 111 | */ |
| 127 | - public static function ten(): BigDecimal | |
| 112 | + public static function ten() : BigDecimal | |
| 128 | 113 | { |
| 129 | 114 | /** |
| 130 | 115 | * @psalm-suppress ImpureStaticVariable |
| 131 | 116 | * @var BigDecimal|null $ten |
| @@ -142,13 +127,11 @@ | ||
| 142 | 127 | * The result has a scale of `max($this->scale, $that->scale)`. |
| 143 | 128 | * |
| 144 | 129 | * @param BigNumber|int|float|string $that The number to add. Must be convertible to a BigDecimal. |
| 145 | 130 | * |
| 146 | - * @return BigDecimal The result. | |
| 147 | - * | |
| 148 | 131 | * @throws MathException If the number is not valid, or is not convertible to a BigDecimal. |
| 149 | 132 | */ |
| 150 | - public function plus($that): BigDecimal | |
| 133 | + public function plus(BigNumber|int|float|string $that) : BigDecimal | |
| 151 | 134 | { |
| 152 | 135 | $that = BigDecimal::of($that); |
| 153 | 136 | if ($that->value === '0' && $that->scale <= $this->scale) { |
| 154 | 137 | return $this; |
| @@ -167,13 +150,11 @@ | ||
| 167 | 150 | * The result has a scale of `max($this->scale, $that->scale)`. |
| 168 | 151 | * |
| 169 | 152 | * @param BigNumber|int|float|string $that The number to subtract. Must be convertible to a BigDecimal. |
| 170 | 153 | * |
| 171 | - * @return BigDecimal The result. | |
| 172 | - * | |
| 173 | 154 | * @throws MathException If the number is not valid, or is not convertible to a BigDecimal. |
| 174 | 155 | */ |
| 175 | - public function minus($that): BigDecimal | |
| 156 | + public function minus(BigNumber|int|float|string $that) : BigDecimal | |
| 176 | 157 | { |
| 177 | 158 | $that = BigDecimal::of($that); |
| 178 | 159 | if ($that->value === '0' && $that->scale <= $this->scale) { |
| 179 | 160 | return $this; |
| @@ -189,13 +170,11 @@ | ||
| 189 | 170 | * The result has a scale of `$this->scale + $that->scale`. |
| 190 | 171 | * |
| 191 | 172 | * @param BigNumber|int|float|string $that The multiplier. Must be convertible to a BigDecimal. |
| 192 | 173 | * |
| 193 | - * @return BigDecimal The result. | |
| 194 | - * | |
| 195 | 174 | * @throws MathException If the multiplier is not a valid number, or is not convertible to a BigDecimal. |
| 196 | 175 | */ |
| 197 | - public function multipliedBy($that): BigDecimal | |
| 176 | + public function multipliedBy(BigNumber|int|float|string $that) : BigDecimal | |
| 198 | 177 | { |
| 199 | 178 | $that = BigDecimal::of($that); |
| 200 | 179 | if ($that->value === '1' && $that->scale === 0) { |
| 201 | 180 | return $this; |
| @@ -211,16 +190,14 @@ | ||
| 211 | 190 | * Returns the result of the division of this number by the given one, at the given scale. |
| 212 | 191 | * |
| 213 | 192 | * @param BigNumber|int|float|string $that The divisor. |
| 214 | 193 | * @param int|null $scale The desired scale, or null to use the scale of this number. |
| 215 | - * @param int $roundingMode An optional rounding mode. | |
| 194 | + * @param RoundingMode $roundingMode An optional rounding mode, defaults to UNNECESSARY. | |
| 216 | 195 | * |
| 217 | - * @return BigDecimal | |
| 218 | - * | |
| 219 | 196 | * @throws \InvalidArgumentException If the scale or rounding mode is invalid. |
| 220 | 197 | * @throws MathException If the number is invalid, is zero, or rounding was necessary. |
| 221 | 198 | */ |
| 222 | - public function dividedBy($that, ?int $scale = null, int $roundingMode = RoundingMode::UNNECESSARY): BigDecimal | |
| 199 | + public function dividedBy(BigNumber|int|float|string $that, ?int $scale = null, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal | |
| 223 | 200 | { |
| 224 | 201 | $that = BigDecimal::of($that); |
| 225 | 202 | if ($that->isZero()) { |
| 226 | 203 | throw DivisionByZeroException::divisionByZero(); |
| @@ -244,14 +221,12 @@ | ||
| 244 | 221 | * The scale of the result is automatically calculated to fit all the fraction digits. |
| 245 | 222 | * |
| 246 | 223 | * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal. |
| 247 | 224 | * |
| 248 | - * @return BigDecimal The result. | |
| 249 | - * | |
| 250 | 225 | * @throws MathException If the divisor is not a valid number, is not convertible to a BigDecimal, is zero, |
| 251 | 226 | * or the result yields an infinite number of digits. |
| 252 | 227 | */ |
| 253 | - public function exactlyDividedBy($that): BigDecimal | |
| 228 | + public function exactlyDividedBy(BigNumber|int|float|string $that) : BigDecimal | |
| 254 | 229 | { |
| 255 | 230 | $that = BigDecimal::of($that); |
| 256 | 231 | if ($that->value === '0') { |
| 257 | 232 | throw DivisionByZeroException::divisionByZero(); |
| @@ -276,15 +251,11 @@ | ||
| 276 | 251 | * Returns this number exponentiated to the given value. |
| 277 | 252 | * |
| 278 | 253 | * The result has a scale of `$this->scale * $exponent`. |
| 279 | 254 | * |
| 280 | - * @param int $exponent The exponent. | |
| 281 | - * | |
| 282 | - * @return BigDecimal The result. | |
| 283 | - * | |
| 284 | 255 | * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000. |
| 285 | 256 | */ |
| 286 | - public function power(int $exponent): BigDecimal | |
| 257 | + public function power(int $exponent) : BigDecimal | |
| 287 | 258 | { |
| 288 | 259 | if ($exponent === 0) { |
| 289 | 260 | return BigDecimal::one(); |
| 290 | 261 | } |
| @@ -296,19 +267,17 @@ | ||
| 296 | 267 | } |
| 297 | 268 | return new BigDecimal(Calculator::get()->pow($this->value, $exponent), $this->scale * $exponent); |
| 298 | 269 | } |
| 299 | 270 | /** |
| 300 | - * Returns the quotient of the division of this number by this given one. | |
| 271 | + * Returns the quotient of the division of this number by the given one. | |
| 301 | 272 | * |
| 302 | 273 | * The quotient has a scale of `0`. |
| 303 | 274 | * |
| 304 | 275 | * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal. |
| 305 | 276 | * |
| 306 | - * @return BigDecimal The quotient. | |
| 307 | - * | |
| 308 | 277 | * @throws MathException If the divisor is not a valid decimal number, or is zero. |
| 309 | 278 | */ |
| 310 | - public function quotient($that): BigDecimal | |
| 279 | + public function quotient(BigNumber|int|float|string $that) : BigDecimal | |
| 311 | 280 | { |
| 312 | 281 | $that = BigDecimal::of($that); |
| 313 | 282 | if ($that->isZero()) { |
| 314 | 283 | throw DivisionByZeroException::divisionByZero(); |
| @@ -318,19 +287,17 @@ | ||
| 318 | 287 | $quotient = Calculator::get()->divQ($p, $q); |
| 319 | 288 | return new BigDecimal($quotient, 0); |
| 320 | 289 | } |
| 321 | 290 | /** |
| 322 | - * Returns the remainder of the division of this number by this given one. | |
| 291 | + * Returns the remainder of the division of this number by the given one. | |
| 323 | 292 | * |
| 324 | 293 | * The remainder has a scale of `max($this->scale, $that->scale)`. |
| 325 | 294 | * |
| 326 | 295 | * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal. |
| 327 | 296 | * |
| 328 | - * @return BigDecimal The remainder. | |
| 329 | - * | |
| 330 | 297 | * @throws MathException If the divisor is not a valid decimal number, or is zero. |
| 331 | 298 | */ |
| 332 | - public function remainder($that): BigDecimal | |
| 299 | + public function remainder(BigNumber|int|float|string $that) : BigDecimal | |
| 333 | 300 | { |
| 334 | 301 | $that = BigDecimal::of($that); |
| 335 | 302 | if ($that->isZero()) { |
| 336 | 303 | throw DivisionByZeroException::divisionByZero(); |
| @@ -349,11 +316,13 @@ | ||
| 349 | 316 | * @param BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal. |
| 350 | 317 | * |
| 351 | 318 | * @return BigDecimal[] An array containing the quotient and the remainder. |
| 352 | 319 | * |
| 320 | + * @psalm-return array{BigDecimal, BigDecimal} | |
| 321 | + * | |
| 353 | 322 | * @throws MathException If the divisor is not a valid decimal number, or is zero. |
| 354 | 323 | */ |
| 355 | - public function quotientAndRemainder($that): array | |
| 324 | + public function quotientAndRemainder(BigNumber|int|float|string $that) : array | |
| 356 | 325 | { |
| 357 | 326 | $that = BigDecimal::of($that); |
| 358 | 327 | if ($that->isZero()) { |
| 359 | 328 | throw DivisionByZeroException::divisionByZero(); |
| @@ -368,16 +337,12 @@ | ||
| 368 | 337 | } |
| 369 | 338 | /** |
| 370 | 339 | * Returns the square root of this number, rounded down to the given number of decimals. |
| 371 | 340 | * |
| 372 | - * @param int $scale | |
| 373 | - * | |
| 374 | - * @return BigDecimal | |
| 375 | - * | |
| 376 | 341 | * @throws \InvalidArgumentException If the scale is negative. |
| 377 | 342 | * @throws NegativeNumberException If this number is negative. |
| 378 | 343 | */ |
| 379 | - public function sqrt(int $scale): BigDecimal | |
| 344 | + public function sqrt(int $scale) : BigDecimal | |
| 380 | 345 | { |
| 381 | 346 | if ($scale < 0) { |
| 382 | 347 | throw new \InvalidArgumentException('Scale cannot be negative.'); |
| 383 | 348 | } |
| @@ -404,14 +369,10 @@ | ||
| 404 | 369 | return new BigDecimal($value, $scale); |
| 405 | 370 | } |
| 406 | 371 | /** |
| 407 | 372 | * Returns a copy of this BigDecimal with the decimal point moved $n places to the left. |
| 408 | - * | |
| 409 | - * @param int $n | |
| 410 | - * | |
| 411 | - * @return BigDecimal | |
| 412 | 373 | */ |
| 413 | - public function withPointMovedLeft(int $n): BigDecimal | |
| 374 | + public function withPointMovedLeft(int $n) : BigDecimal | |
| 414 | 375 | { |
| 415 | 376 | if ($n === 0) { |
| 416 | 377 | return $this; |
| 417 | 378 | } |
| @@ -421,14 +382,10 @@ | ||
| 421 | 382 | return new BigDecimal($this->value, $this->scale + $n); |
| 422 | 383 | } |
| 423 | 384 | /** |
| 424 | 385 | * Returns a copy of this BigDecimal with the decimal point moved $n places to the right. |
| 425 | - * | |
| 426 | - * @param int $n | |
| 427 | - * | |
| 428 | - * @return BigDecimal | |
| 429 | 386 | */ |
| 430 | - public function withPointMovedRight(int $n): BigDecimal | |
| 387 | + public function withPointMovedRight(int $n) : BigDecimal | |
| 431 | 388 | { |
| 432 | 389 | if ($n === 0) { |
| 433 | 390 | return $this; |
| 434 | 391 | } |
| @@ -446,12 +403,10 @@ | ||
| 446 | 403 | return new BigDecimal($value, $scale); |
| 447 | 404 | } |
| 448 | 405 | /** |
| 449 | 406 | * Returns a copy of this BigDecimal with any trailing zeros removed from the fractional part. |
| 450 | - * | |
| 451 | - * @return BigDecimal | |
| 452 | 407 | */ |
| 453 | - public function stripTrailingZeros(): BigDecimal | |
| 408 | + public function stripTrailingZeros() : BigDecimal | |
| 454 | 409 | { |
| 455 | 410 | if ($this->scale === 0) { |
| 456 | 411 | return $this; |
| 457 | 412 | } |
| @@ -471,28 +426,22 @@ | ||
| 471 | 426 | return new BigDecimal($value, $scale); |
| 472 | 427 | } |
| 473 | 428 | /** |
| 474 | 429 | * Returns the absolute value of this number. |
| 475 | - * | |
| 476 | - * @return BigDecimal | |
| 477 | 430 | */ |
| 478 | - public function abs(): BigDecimal | |
| 431 | + public function abs() : BigDecimal | |
| 479 | 432 | { |
| 480 | 433 | return $this->isNegative() ? $this->negated() : $this; |
| 481 | 434 | } |
| 482 | 435 | /** |
| 483 | 436 | * Returns the negated value of this number. |
| 484 | - * | |
| 485 | - * @return BigDecimal | |
| 486 | 437 | */ |
| 487 | - public function negated(): BigDecimal | |
| 438 | + public function negated() : BigDecimal | |
| 488 | 439 | { |
| 489 | 440 | return new BigDecimal(Calculator::get()->neg($this->value), $this->scale); |
| 490 | 441 | } |
| 491 | - /** | |
| 492 | - * {@inheritdoc} | |
| 493 | - */ | |
| 494 | - public function compareTo($that): int | |
| 442 | + #[\Override] | |
| 443 | + public function compareTo(BigNumber|int|float|string $that) : int | |
| 495 | 444 | { |
| 496 | 445 | $that = BigNumber::of($that); |
| 497 | 446 | if ($that instanceof BigInteger) { |
| 498 | 447 | $that = $that->toBigDecimal(); |
| @@ -502,37 +451,50 @@ | ||
| 502 | 451 | return Calculator::get()->cmp($a, $b); |
| 503 | 452 | } |
| 504 | 453 | return -$that->compareTo($this); |
| 505 | 454 | } |
| 506 | - /** | |
| 507 | - * {@inheritdoc} | |
| 508 | - */ | |
| 509 | - public function getSign(): int | |
| 455 | + #[\Override] | |
| 456 | + public function getSign() : int | |
| 510 | 457 | { |
| 511 | 458 | return $this->value === '0' ? 0 : ($this->value[0] === '-' ? -1 : 1); |
| 512 | 459 | } |
| 513 | - /** | |
| 514 | - * @return BigInteger | |
| 515 | - */ | |
| 516 | - public function getUnscaledValue(): BigInteger | |
| 460 | + public function getUnscaledValue() : BigInteger | |
| 517 | 461 | { |
| 518 | - return BigInteger::create($this->value); | |
| 462 | + return self::newBigInteger($this->value); | |
| 519 | 463 | } |
| 464 | + public function getScale() : int | |
| 465 | + { | |
| 466 | + return $this->scale; | |
| 467 | + } | |
| 520 | 468 | /** |
| 521 | - * @return int | |
| 469 | + * Returns the number of significant digits in the number. | |
| 470 | + * | |
| 471 | + * This is the number of digits to both sides of the decimal point, stripped of leading zeros. | |
| 472 | + * The sign has no impact on the result. | |
| 473 | + * | |
| 474 | + * Examples: | |
| 475 | + * 0 => 0 | |
| 476 | + * 0.0 => 0 | |
| 477 | + * 123 => 3 | |
| 478 | + * 123.456 => 6 | |
| 479 | + * 0.00123 => 3 | |
| 480 | + * 0.0012300 => 5 | |
| 522 | 481 | */ |
| 523 | - public function getScale(): int | |
| 482 | + public function getPrecision() : int | |
| 524 | 483 | { |
| 525 | - return $this->scale; | |
| 484 | + $value = $this->value; | |
| 485 | + if ($value === '0') { | |
| 486 | + return 0; | |
| 487 | + } | |
| 488 | + $length = \strlen($value); | |
| 489 | + return $value[0] === '-' ? $length - 1 : $length; | |
| 526 | 490 | } |
| 527 | 491 | /** |
| 528 | 492 | * Returns a string representing the integral part of this decimal number. |
| 529 | 493 | * |
| 530 | 494 | * Example: `-123.456` => `-123`. |
| 531 | - * | |
| 532 | - * @return string | |
| 533 | 495 | */ |
| 534 | - public function getIntegralPart(): string | |
| 496 | + public function getIntegralPart() : string | |
| 535 | 497 | { |
| 536 | 498 | if ($this->scale === 0) { |
| 537 | 499 | return $this->value; |
| 538 | 500 | } |
| @@ -544,12 +506,10 @@ | ||
| 544 | 506 | * |
| 545 | 507 | * If the scale is zero, an empty string is returned. |
| 546 | 508 | * |
| 547 | 509 | * Examples: `-123.456` => '456', `123` => ''. |
| 548 | - * | |
| 549 | - * @return string | |
| 550 | 510 | */ |
| 551 | - public function getFractionalPart(): string | |
| 511 | + public function getFractionalPart() : string | |
| 552 | 512 | { |
| 553 | 513 | if ($this->scale === 0) { |
| 554 | 514 | return ''; |
| 555 | 515 | } |
| @@ -557,43 +517,33 @@ | ||
| 557 | 517 | return \substr($value, -$this->scale); |
| 558 | 518 | } |
| 559 | 519 | /** |
| 560 | 520 | * Returns whether this decimal number has a non-zero fractional part. |
| 561 | - * | |
| 562 | - * @return bool | |
| 563 | 521 | */ |
| 564 | - public function hasNonZeroFractionalPart(): bool | |
| 522 | + public function hasNonZeroFractionalPart() : bool | |
| 565 | 523 | { |
| 566 | 524 | return $this->getFractionalPart() !== \str_repeat('0', $this->scale); |
| 567 | 525 | } |
| 568 | - /** | |
| 569 | - * {@inheritdoc} | |
| 570 | - */ | |
| 571 | - public function toBigInteger(): BigInteger | |
| 526 | + #[\Override] | |
| 527 | + public function toBigInteger() : BigInteger | |
| 572 | 528 | { |
| 573 | 529 | $zeroScaleDecimal = $this->scale === 0 ? $this : $this->dividedBy(1, 0); |
| 574 | - return BigInteger::create($zeroScaleDecimal->value); | |
| 530 | + return self::newBigInteger($zeroScaleDecimal->value); | |
| 575 | 531 | } |
| 576 | - /** | |
| 577 | - * {@inheritdoc} | |
| 578 | - */ | |
| 579 | - public function toBigDecimal(): BigDecimal | |
| 532 | + #[\Override] | |
| 533 | + public function toBigDecimal() : BigDecimal | |
| 580 | 534 | { |
| 581 | 535 | return $this; |
| 582 | 536 | } |
| 583 | - /** | |
| 584 | - * {@inheritdoc} | |
| 585 | - */ | |
| 586 | - public function toBigRational(): BigRational | |
| 537 | + #[\Override] | |
| 538 | + public function toBigRational() : BigRational | |
| 587 | 539 | { |
| 588 | - $numerator = BigInteger::create($this->value); | |
| 589 | - $denominator = BigInteger::create('1' . \str_repeat('0', $this->scale)); | |
| 590 | - return BigRational::create($numerator, $denominator, \false); | |
| 540 | + $numerator = self::newBigInteger($this->value); | |
| 541 | + $denominator = self::newBigInteger('1' . \str_repeat('0', $this->scale)); | |
| 542 | + return self::newBigRational($numerator, $denominator, \false); | |
| 591 | 543 | } |
| 592 | - /** | |
| 593 | - * {@inheritdoc} | |
| 594 | - */ | |
| 595 | - public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY): BigDecimal | |
| 544 | + #[\Override] | |
| 545 | + public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal | |
| 596 | 546 | { |
| 597 | 547 | if ($scale === $this->scale) { |
| 598 | 548 | return $this; |
| 599 | 549 | } |
| @@ -598,31 +548,30 @@ | ||
| 598 | 548 | return $this; |
| 599 | 549 | } |
| 600 | 550 | return $this->dividedBy(BigDecimal::one(), $scale, $roundingMode); |
| 601 | 551 | } |
| 602 | - /** | |
| 603 | - * {@inheritdoc} | |
| 604 | - */ | |
| 605 | - public function toInt(): int | |
| 552 | + #[\Override] | |
| 553 | + public function toInt() : int | |
| 606 | 554 | { |
| 607 | 555 | return $this->toBigInteger()->toInt(); |
| 608 | 556 | } |
| 609 | - /** | |
| 610 | - * {@inheritdoc} | |
| 611 | - */ | |
| 612 | - public function toFloat(): float | |
| 557 | + #[\Override] | |
| 558 | + public function toFloat() : float | |
| 613 | 559 | { |
| 614 | 560 | return (float) (string) $this; |
| 615 | 561 | } |
| 616 | 562 | /** |
| 617 | - * {@inheritdoc} | |
| 563 | + * @return numeric-string | |
| 618 | 564 | */ |
| 619 | - public function __toString(): string | |
| 565 | + #[\Override] | |
| 566 | + public function __toString() : string | |
| 620 | 567 | { |
| 621 | 568 | if ($this->scale === 0) { |
| 569 | + /** @var numeric-string */ | |
| 622 | 570 | return $this->value; |
| 623 | 571 | } |
| 624 | 572 | $value = $this->getUnscaledValueWithLeadingZeros(); |
| 573 | + /** @var numeric-string */ | |
| 625 | 574 | return \substr($value, 0, -$this->scale) . '.' . \substr($value, -$this->scale); |
| 626 | 575 | } |
| 627 | 576 | /** |
| 628 | 577 | * This method is required for serializing the object and SHOULD NOT be accessed directly. |
| @@ -630,9 +579,9 @@ | ||
| 630 | 579 | * @internal |
| 631 | 580 | * |
| 632 | 581 | * @return array{value: string, scale: int} |
| 633 | 582 | */ |
| 634 | - public function __serialize(): array | |
| 583 | + public function __serialize() : array | |
| 635 | 584 | { |
| 636 | 585 | return ['value' => $this->value, 'scale' => $this->scale]; |
| 637 | 586 | } |
| 638 | 587 | /** |
| @@ -642,13 +591,11 @@ | ||
| 642 | 591 | * @psalm-suppress RedundantPropertyInitializationCheck |
| 643 | 592 | * |
| 644 | 593 | * @param array{value: string, scale: int} $data |
| 645 | 594 | * |
| 646 | - * @return void | |
| 647 | - * | |
| 648 | 595 | * @throws \LogicException |
| 649 | 596 | */ |
| 650 | - public function __unserialize(array $data): void | |
| 597 | + public function __unserialize(array $data) : void | |
| 651 | 598 | { |
| 652 | 599 | if (isset($this->value)) { |
| 653 | 600 | throw new \LogicException('__unserialize() is an internal function, it must not be called directly.'); |
| 654 | 601 | } |
| @@ -655,48 +602,13 @@ | ||
| 655 | 602 | $this->value = $data['value']; |
| 656 | 603 | $this->scale = $data['scale']; |
| 657 | 604 | } |
| 658 | 605 | /** |
| 659 | - * This method is required by interface Serializable and SHOULD NOT be accessed directly. | |
| 660 | - * | |
| 661 | - * @internal | |
| 662 | - * | |
| 663 | - * @return string | |
| 664 | - */ | |
| 665 | - public function serialize(): string | |
| 666 | - { | |
| 667 | - return $this->value . ':' . $this->scale; | |
| 668 | - } | |
| 669 | - /** | |
| 670 | - * This method is only here to implement interface Serializable and cannot be accessed directly. | |
| 671 | - * | |
| 672 | - * @internal | |
| 673 | - * @psalm-suppress RedundantPropertyInitializationCheck | |
| 674 | - * | |
| 675 | - * @param string $value | |
| 676 | - * | |
| 677 | - * @return void | |
| 678 | - * | |
| 679 | - * @throws \LogicException | |
| 680 | - */ | |
| 681 | - public function unserialize($value): void | |
| 682 | - { | |
| 683 | - if (isset($this->value)) { | |
| 684 | - throw new \LogicException('unserialize() is an internal function, it must not be called directly.'); | |
| 685 | - } | |
| 686 | - [$value, $scale] = \explode(':', $value); | |
| 687 | - $this->value = $value; | |
| 688 | - $this->scale = (int) $scale; | |
| 689 | - } | |
| 690 | - /** | |
| 691 | 606 | * Puts the internal values of the given decimal numbers on the same scale. |
| 692 | 607 | * |
| 693 | - * @param BigDecimal $x The first decimal number. | |
| 694 | - * @param BigDecimal $y The second decimal number. | |
| 695 | - * | |
| 696 | 608 | * @return array{string, string} The scaled integer values of $x and $y. |
| 697 | 609 | */ |
| 698 | - private function scaleValues(BigDecimal $x, BigDecimal $y): array | |
| 610 | + private function scaleValues(BigDecimal $x, BigDecimal $y) : array | |
| 699 | 611 | { |
| 700 | 612 | $a = $x->value; |
| 701 | 613 | $b = $y->value; |
| 702 | 614 | if ($b !== '0' && $x->scale > $y->scale) { |
| @@ -705,14 +617,9 @@ | ||
| 705 | 617 | $a .= \str_repeat('0', $y->scale - $x->scale); |
| 706 | 618 | } |
| 707 | 619 | return [$a, $b]; |
| 708 | 620 | } |
| 709 | - /** | |
| 710 | - * @param int $scale | |
| 711 | - * | |
| 712 | - * @return string | |
| 713 | - */ | |
| 714 | - private function valueWithMinScale(int $scale): string | |
| 621 | + private function valueWithMinScale(int $scale) : string | |
| 715 | 622 | { |
| 716 | 623 | $value = $this->value; |
| 717 | 624 | if ($this->value !== '0' && $scale > $this->scale) { |
| 718 | 625 | $value .= \str_repeat('0', $scale - $this->scale); |
| @@ -720,12 +627,10 @@ | ||
| 720 | 627 | return $value; |
| 721 | 628 | } |
| 722 | 629 | /** |
| 723 | 630 | * Adds leading zeros if necessary to the unscaled value to represent the full decimal number. |
| 724 | - * | |
| 725 | - * @return string | |
| 726 | 631 | */ |
| 727 | - private function getUnscaledValueWithLeadingZeros(): string | |
| 632 | + private function getUnscaledValueWithLeadingZeros() : string | |
| 728 | 633 | { |
| 729 | 634 | $value = $this->value; |
| 730 | 635 | $targetLength = $this->scale + 1; |
| 731 | 636 | $negative = $value[0] === '-'; |