PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / uuid / src / Math / BrickMathCalculator.php

BrickMathCalculator.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.0, at vendor/ramsey/uuid/src/Math/BrickMathCalculator.php

155 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Brick\Math\RoundingMode as BrickMathRounding;
21 use Ramsey\Uuid\Exception\InvalidArgumentException;
22 use Ramsey\Uuid\Type\Decimal;
23 use Ramsey\Uuid\Type\Hexadecimal;
24 use Ramsey\Uuid\Type\Integer as IntegerObject;
25 use Ramsey\Uuid\Type\NumberInterface;
26
27 /**
28 * A calculator using the brick/math library for arbitrary-precision arithmetic
29 *
30 * @immutable
31 */
32 final class BrickMathCalculator implements CalculatorInterface
33 {
34 private const ROUNDING_MODE_MAP = [
35 RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY,
36 RoundingMode::UP => BrickMathRounding::UP,
37 RoundingMode::DOWN => BrickMathRounding::DOWN,
38 RoundingMode::CEILING => BrickMathRounding::CEILING,
39 RoundingMode::FLOOR => BrickMathRounding::FLOOR,
40 RoundingMode::HALF_UP => BrickMathRounding::HALF_UP,
41 RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN,
42 RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING,
43 RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR,
44 RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN,
45 ];
46
47 public function add(NumberInterface $augend, NumberInterface ...$addends): NumberInterface
48 {
49 $sum = BigInteger::of($augend->toString());
50
51 foreach ($addends as $addend) {
52 $sum = $sum->plus($addend->toString());
53 }
54
55 /** @phpstan-ignore possiblyImpure.new */
56 return new IntegerObject((string) $sum);
57 }
58
59 public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends): NumberInterface
60 {
61 $difference = BigInteger::of($minuend->toString());
62
63 foreach ($subtrahends as $subtrahend) {
64 $difference = $difference->minus($subtrahend->toString());
65 }
66
67 /** @phpstan-ignore possiblyImpure.new */
68 return new IntegerObject((string) $difference);
69 }
70
71 public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers): NumberInterface
72 {
73 $product = BigInteger::of($multiplicand->toString());
74
75 foreach ($multipliers as $multiplier) {
76 $product = $product->multipliedBy($multiplier->toString());
77 }
78
79 /** @phpstan-ignore possiblyImpure.new */
80 return new IntegerObject((string) $product);
81 }
82
83 public function divide(
84 int $roundingMode,
85 int $scale,
86 NumberInterface $dividend,
87 NumberInterface ...$divisors,
88 ): NumberInterface {
89 /** @phpstan-ignore possiblyImpure.methodCall */
90 $brickRounding = $this->getBrickRoundingMode($roundingMode);
91
92 $quotient = BigDecimal::of($dividend->toString());
93
94 foreach ($divisors as $divisor) {
95 $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding);
96 }
97
98 if ($scale === 0) {
99 /** @phpstan-ignore possiblyImpure.new */
100 return new IntegerObject((string) $quotient->toBigInteger());
101 }
102
103 /** @phpstan-ignore possiblyImpure.new */
104 return new Decimal((string) $quotient);
105 }
106
107 public function fromBase(string $value, int $base): IntegerObject
108 {
109 try {
110 /** @phpstan-ignore possiblyImpure.new */
111 return new IntegerObject((string) BigInteger::fromBase($value, $base));
112 } catch (MathException |\InvalidArgumentException $exception) {
113 throw new InvalidArgumentException(
114 $exception->getMessage(),
115 (int) $exception->getCode(),
116 $exception
117 );
118 }
119 }
120
121 public function toBase(IntegerObject $value, int $base): string
122 {
123 try {
124 return BigInteger::of($value->toString())->toBase($base);
125 } catch (MathException |\InvalidArgumentException $exception) {
126 throw new InvalidArgumentException(
127 $exception->getMessage(),
128 (int) $exception->getCode(),
129 $exception
130 );
131 }
132 }
133
134 public function toHexadecimal(IntegerObject $value): Hexadecimal
135 {
136 /** @phpstan-ignore possiblyImpure.new */
137 return new Hexadecimal($this->toBase($value, 16));
138 }
139
140 public function toInteger(Hexadecimal $value): IntegerObject
141 {
142 return $this->fromBase($value->toString(), 16);
143 }
144
145 /**
146 * Maps ramsey/uuid rounding modes to those used by brick/math
147 *
148 * @return BrickMathRounding::*
149 */
150 private function getBrickRoundingMode(int $roundingMode)
151 {
152 return self::ROUNDING_MODE_MAP[$roundingMode] ?? BrickMathRounding::UNNECESSARY;
153 }
154 }
155