|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nejcc\PhpDatatypes\Abstract; |
| 6 | + |
| 7 | +use Nejcc\PhpDatatypes\Interfaces\BigIntegerInterface; |
| 8 | +use Nejcc\PhpDatatypes\Interfaces\NativeIntegerInterface; |
| 9 | +use Nejcc\PhpDatatypes\Traits\ArithmeticOperationsTrait; |
| 10 | +use Nejcc\PhpDatatypes\Traits\IntegerComparisonTrait; |
| 11 | + |
| 12 | +/** |
| 13 | + * Abstract class for big integer types using arbitrary-precision arithmetic. |
| 14 | + * |
| 15 | + * @package Nejcc\PhpDatatypes\Integers |
| 16 | + */ |
| 17 | +abstract class AbstractBigInteger implements BigIntegerInterface, NativeIntegerInterface |
| 18 | +{ |
| 19 | + use ArithmeticOperationsTrait; |
| 20 | + use IntegerComparisonTrait; |
| 21 | + |
| 22 | + protected readonly string $value; |
| 23 | + |
| 24 | + public const MIN_VALUE = null; |
| 25 | + public const MAX_VALUE = null; |
| 26 | + |
| 27 | + public function __construct(int|string $value) |
| 28 | + { |
| 29 | + $this->setValue($value); |
| 30 | + } |
| 31 | + |
| 32 | + protected function setValue(int|string $value): void |
| 33 | + { |
| 34 | + $valueStr = (string)$value; |
| 35 | + |
| 36 | + if (bccomp($valueStr, (string)static::MIN_VALUE) < 0 || bccomp($valueStr, (string)static::MAX_VALUE) > 0) { |
| 37 | + throw new \OutOfRangeException(sprintf( |
| 38 | + 'Value must be between %s and %s.', |
| 39 | + static::MIN_VALUE, |
| 40 | + static::MAX_VALUE |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + $this->value = $valueStr; |
| 45 | + } |
| 46 | + |
| 47 | + public function getValue(): string |
| 48 | + { |
| 49 | + return $this->value; |
| 50 | + } |
| 51 | + |
| 52 | + // Implement comparison method |
| 53 | + public function compare(NativeIntegerInterface|BigIntegerInterface $other): int |
| 54 | + { |
| 55 | + return bccomp($this->value, (string)$other->getValue()); |
| 56 | + } |
| 57 | + |
| 58 | + // Implement operation methods required by the trait |
| 59 | + protected function performOperation( |
| 60 | + BigIntegerInterface|NativeIntegerInterface $other, |
| 61 | + callable $operation, |
| 62 | + string $operationName |
| 63 | + ): static { |
| 64 | + $result = $operation($this->value, (string)$other->getValue()); |
| 65 | + |
| 66 | + if (bccomp($result, (string)static::MIN_VALUE) < 0 || bccomp($result, (string)static::MAX_VALUE) > 0) { |
| 67 | + $exceptionClass = bccomp($result, (string)static::MAX_VALUE) > 0 ? \OverflowException::class : \UnderflowException::class; |
| 68 | + throw new $exceptionClass('Result is out of bounds.'); |
| 69 | + } |
| 70 | + |
| 71 | + return new static($result); |
| 72 | + } |
| 73 | + |
| 74 | + protected function addValues(string $a, string $b): string |
| 75 | + { |
| 76 | + return bcadd($a, $b, 0); |
| 77 | + } |
| 78 | + |
| 79 | + protected function subtractValues(string $a, string $b): string |
| 80 | + { |
| 81 | + return bcsub($a, $b, 0); |
| 82 | + } |
| 83 | + |
| 84 | + protected function multiplyValues(string $a, string $b): string |
| 85 | + { |
| 86 | + return bcmul($a, $b, 0); |
| 87 | + } |
| 88 | + |
| 89 | + protected function divideValues(string $a, string $b): string |
| 90 | + { |
| 91 | + if ($b === '0') { |
| 92 | + throw new \DivisionByZeroError('Division by zero.'); |
| 93 | + } |
| 94 | + |
| 95 | + $result = bcdiv($a, $b, 0); |
| 96 | + |
| 97 | + if (str_contains($result, '.')) { |
| 98 | + throw new \UnexpectedValueException('Division result is not an integer.'); |
| 99 | + } |
| 100 | + |
| 101 | + return $result; |
| 102 | + } |
| 103 | + |
| 104 | + protected function modValues(string $a, string $b): string |
| 105 | + { |
| 106 | + if ($b === '0') { |
| 107 | + throw new \DivisionByZeroError('Division by zero.'); |
| 108 | + } |
| 109 | + |
| 110 | + return bcmod($a, $b); |
| 111 | + } |
| 112 | +} |
0 commit comments