PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
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 / lib / packages / Ramsey / Uuid / Math / BrickMathCalculator.php

BrickMathCalculator.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at lib/packages/Ramsey/Uuid/Math/BrickMathCalculator.php

131 lines 4.3 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 WP_DEFENDER_VENDOR\Ramsey\Uuid\Math;
16
17 use WP_DEFENDER_VENDOR\Brick\Math\BigDecimal;
18 use WP_DEFENDER_VENDOR\Brick\Math\BigInteger;
19 use WP_DEFENDER_VENDOR\Brick\Math\Exception\MathException;
20 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Exception\InvalidArgumentException;
21 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Type\Decimal;
22 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Type\Hexadecimal;
23 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Type\Integer as IntegerObject;
24 use WP_DEFENDER_VENDOR\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 }
131