PluginProbe
Media Cloud Sync / 1.2.6
Media Cloud Sync v1.2.6
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 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Math / BrickMathCalculator.php

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

100 lines 4.4 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 <ben@benramsey.com>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\Ramsey\Uuid\Math;
14
15 use Dudlewebs\WPMCS\Brick\Math\BigDecimal;
16 use Dudlewebs\WPMCS\Brick\Math\BigInteger;
17 use Dudlewebs\WPMCS\Brick\Math\Exception\MathException;
18 use Dudlewebs\WPMCS\Brick\Math\RoundingMode as BrickMathRounding;
19 use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException;
20 use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Decimal;
21 use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal;
22 use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject;
23 use Dudlewebs\WPMCS\Ramsey\Uuid\Type\NumberInterface;
24 /**
25 * A calculator using the brick/math library for arbitrary-precision arithmetic
26 *
27 * @psalm-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 return new IntegerObject((string) $sum);
39 }
40 public function subtract(NumberInterface $minuend, NumberInterface ...$subtrahends): NumberInterface
41 {
42 $difference = BigInteger::of($minuend->toString());
43 foreach ($subtrahends as $subtrahend) {
44 $difference = $difference->minus($subtrahend->toString());
45 }
46 return new IntegerObject((string) $difference);
47 }
48 public function multiply(NumberInterface $multiplicand, NumberInterface ...$multipliers): NumberInterface
49 {
50 $product = BigInteger::of($multiplicand->toString());
51 foreach ($multipliers as $multiplier) {
52 $product = $product->multipliedBy($multiplier->toString());
53 }
54 return new IntegerObject((string) $product);
55 }
56 public function divide(int $roundingMode, int $scale, NumberInterface $dividend, NumberInterface ...$divisors): NumberInterface
57 {
58 $brickRounding = $this->getBrickRoundingMode($roundingMode);
59 $quotient = BigDecimal::of($dividend->toString());
60 foreach ($divisors as $divisor) {
61 $quotient = $quotient->dividedBy($divisor->toString(), $scale, $brickRounding);
62 }
63 if ($scale === 0) {
64 return new IntegerObject((string) $quotient->toBigInteger());
65 }
66 return new Decimal((string) $quotient);
67 }
68 public function fromBase(string $value, int $base): IntegerObject
69 {
70 try {
71 return new IntegerObject((string) BigInteger::fromBase($value, $base));
72 } catch (MathException|\InvalidArgumentException $exception) {
73 throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
74 }
75 }
76 public function toBase(IntegerObject $value, int $base): string
77 {
78 try {
79 return BigInteger::of($value->toString())->toBase($base);
80 } catch (MathException|\InvalidArgumentException $exception) {
81 throw new InvalidArgumentException($exception->getMessage(), (int) $exception->getCode(), $exception);
82 }
83 }
84 public function toHexadecimal(IntegerObject $value): Hexadecimal
85 {
86 return new Hexadecimal($this->toBase($value, 16));
87 }
88 public function toInteger(Hexadecimal $value): IntegerObject
89 {
90 return $this->fromBase($value->toString(), 16);
91 }
92 /**
93 * Maps ramsey/uuid rounding modes to those used by brick/math
94 */
95 private function getBrickRoundingMode(int $roundingMode): int
96 {
97 return self::ROUNDING_MODE_MAP[$roundingMode] ?? 0;
98 }
99 }
100