PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Math / BrickMathCalculator.php

BrickMathCalculator.php in Media Cloud Sync 1.4.2, at includes/sdk/google/ramsey/uuid/src/Math/BrickMathCalculator.php

110 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 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Math;
14
15 use Dudlewebs\WPMCS\GCP\Brick\Math\BigDecimal;
16 use Dudlewebs\WPMCS\GCP\Brick\Math\BigInteger;
17 use Dudlewebs\WPMCS\GCP\Brick\Math\Exception\MathException;
18 use Dudlewebs\WPMCS\GCP\Brick\Math\RoundingMode as BrickMathRounding;
19 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidArgumentException;
20 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Decimal;
21 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal;
22 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject;
23 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\NumberInterface;
24 /**
25 * A calculator using the brick/math library for arbitrary-precision arithmetic
26 *
27 * @immutable
28 */
29 final class BrickMathCalculator implements CalculatorInterface
30 {
31 private const ROUNDING_MODE_MAP = [RoundingMode::UNNECESSARY => BrickMathRounding::UNNECESSARY, RoundingMode::UP => BrickMathRounding::UP, RoundingMode::DOWN => BrickMathRounding::DOWN, RoundingMode::CEILING => BrickMathRounding::CEILING, RoundingMode::FLOOR => BrickMathRounding::FLOOR, RoundingMode::HALF_UP => BrickMathRounding::HALF_UP, RoundingMode::HALF_DOWN => BrickMathRounding::HALF_DOWN, RoundingMode::HALF_CEILING => BrickMathRounding::HALF_CEILING, RoundingMode::HALF_FLOOR => BrickMathRounding::HALF_FLOOR, RoundingMode::HALF_EVEN => BrickMathRounding::HALF_EVEN];
32 public function add(NumberInterface $augend, NumberInterface ...$addends) : NumberInterface
33 {
34 $sum = BigInteger::of($augend->toString());
35 foreach ($addends as $addend) {
36 $sum = $sum->plus($addend->toString());
37 }
38 /** @phpstan-ignore possiblyImpure.new */
39 return new IntegerObject((string) $sum);
40 }
41 public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends) : NumberInterface
42 {
43 $difference = BigInteger::of($minuend->toString());
44 foreach ($subtrahends as $subtrahend) {
45 $difference = $difference->minus($subtrahend->toString());
46 }
47 /** @phpstan-ignore possiblyImpure.new */
48 return new IntegerObject((string) $difference);
49 }
50 public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers) : NumberInterface
51 {
52 $product = BigInteger::of($multiplicand->toString());
53 foreach ($multipliers as $multiplier) {
54 $product = $product->multipliedBy($multiplier->toString());
55 }
56 /** @phpstan-ignore possiblyImpure.new */
57 return new IntegerObject((string) $product);
58 }
59 public function divide(int $roundingMode, int $scale, NumberInterface $dividend, NumberInterface ...$divisors) : NumberInterface
60 {
61 /** @phpstan-ignore possiblyImpure.methodCall */
62 $brickRounding = $this->getBrickRoundingMode($roundingMode);
63 $quotient = BigDecimal::of($dividend->toString());
64 foreach ($divisors as $divisor) {
65 $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding);
66 }
67 if ($scale === 0) {
68 /** @phpstan-ignore possiblyImpure.new */
69 return new IntegerObject((string) $quotient->toBigInteger());
70 }
71 /** @phpstan-ignore possiblyImpure.new */
72 return new Decimal((string) $quotient);
73 }
74 public function fromBase(string $value, int $base) : IntegerObject
75 {
76 try {
77 /** @phpstan-ignore possiblyImpure.new */
78 return new IntegerObject((string) BigInteger::fromBase($value, $base));
79 } catch (MathException|\InvalidArgumentException $exception) {
80 throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
81 }
82 }
83 public function toBase(IntegerObject $value, int $base) : string
84 {
85 try {
86 return BigInteger::of($value->toString())->toBase($base);
87 } catch (MathException|\InvalidArgumentException $exception) {
88 throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
89 }
90 }
91 public function toHexadecimal(IntegerObject $value) : Hexadecimal
92 {
93 /** @phpstan-ignore possiblyImpure.new */
94 return new Hexadecimal($this->toBase($value, 16));
95 }
96 public function toInteger(Hexadecimal $value) : IntegerObject
97 {
98 return $this->fromBase($value->toString(), 16);
99 }
100 /**
101 * Maps ramsey/uuid rounding modes to those used by brick/math
102 *
103 * @return BrickMathRounding::*
104 */
105 private function getBrickRoundingMode(int $roundingMode)
106 {
107 return self::ROUNDING_MODE_MAP[$roundingMode] ?? BrickMathRounding::UNNECESSARY;
108 }
109 }
110