Debian Codegraph @

vendor/ramsey/uuid/src/Math/BrickMathCalculator.php

raw
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Math;
16
17 use Brick\Math\BigDecimal;
18 use Brick\Math\BigInteger;
19 use Brick\Math\Exception\MathException;
20 use Ramsey\Uuid\Exception\InvalidArgumentException;
21 use Ramsey\Uuid\Type\Decimal;
22 use Ramsey\Uuid\Type\Hexadecimal;
23 use Ramsey\Uuid\Type\Integer as IntegerObject;
24 use Ramsey\Uuid\Type\NumberInterface;
25
26 /**
27 * A calculator using the brick/math library for arbitrary-precision arithmetic
28 *
29 * @immutable
30 */
31 final class BrickMathCalculator implements CalculatorInterface
32 {
33 public function add(NumberInterface $augend, NumberInterface ...$addends): NumberInterface
34 {
35 $sum = BigInteger::of($augend->toString());
36
37 foreach ($addends as $addend) {
38 $sum = $sum->plus($addend->toString());
39 }
40
41 /** @phpstan-ignore possiblyImpure.new */
42 return new IntegerObject((string) $sum);
43 }
44
45 public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends): NumberInterface
46 {
47 $difference = BigInteger::of($minuend->toString());
48
49 foreach ($subtrahends as $subtrahend) {
50 $difference = $difference->minus($subtrahend->toString());
51 }
52
53 /** @phpstan-ignore possiblyImpure.new */
54 return new IntegerObject((string) $difference);
55 }
56
57 public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers): NumberInterface
58 {
59 $product = BigInteger::of($multiplicand->toString());
60
61 foreach ($multipliers as $multiplier) {
62 $product = $product->multipliedBy($multiplier->toString());
63 }
64
65 /** @phpstan-ignore possiblyImpure.new */
66 return new IntegerObject((string) $product);
67 }
68
69 public function divide(
70 int $roundingMode,
71 int $scale,
72 NumberInterface $dividend,
73 NumberInterface ...$divisors,
74 ): NumberInterface {
75 /** @phpstan-ignore possiblyImpure.methodCall */
76 $brickRounding = BrickMathRoundingMode::resolve($roundingMode);
77
78 $quotient = BigDecimal::of($dividend->toString());
79
80 foreach ($divisors as $divisor) {
81 $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding);
82 }
83
84 if ($scale === 0) {
85 /** @phpstan-ignore possiblyImpure.new */
86 return new IntegerObject((string) $quotient->toBigInteger());
87 }
88
89 /** @phpstan-ignore possiblyImpure.new */
90 return new Decimal((string) $quotient);
91 }
92
93 public function fromBase(string $value, int $base): IntegerObject
94 {
95 try {
96 /** @phpstan-ignore possiblyImpure.new */
97 return new IntegerObject((string) BigInteger::fromBase($value, $base));
98 } catch (MathException | \InvalidArgumentException $exception) {
99 throw new InvalidArgumentException(
100 $exception->getMessage(), /** @phpstan-ignore possiblyImpure.methodCall */
101 (int) $exception->getCode(), /** @phpstan-ignore possiblyImpure.methodCall */
102 $exception
103 );
104 }
105 }
106
107 public function toBase(IntegerObject $value, int $base): string
108 {
109 try {
110 return BigInteger::of($value->toString())->toBase($base);
111 } catch (MathException | \InvalidArgumentException $exception) {
112 throw new InvalidArgumentException(
113 $exception->getMessage(), /** @phpstan-ignore possiblyImpure.methodCall */
114 (int) $exception->getCode(), /** @phpstan-ignore possiblyImpure.methodCall */
115 $exception
116 );
117 }
118 }
119
120 public function toHexadecimal(IntegerObject $value): Hexadecimal
121 {
122 /** @phpstan-ignore possiblyImpure.new */
123 return new Hexadecimal($this->toBase($value, 16));
124 }
125
126 public function toInteger(Hexadecimal $value): IntegerObject
127 {
128 return $this->fromBase($value->toString(), 16);
129 }
130 }