Skip to content

Commit 00c3856

Browse files
committed
update wip
1 parent f859185 commit 00c3856

13 files changed

+520
-4
lines changed

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.4|^8.0"
19+
"php": "^8.2",
20+
"ext-bcmath": "*"
2021
},
2122
"require-dev": {
22-
"phpunit/phpunit": "^9.0"
23+
"phpunit/phpunit": "^11.3.0"
2324
},
2425
"autoload": {
2526
"psr-4": {
2627
"Nejcc\\PhpDatatypes\\": "src"
27-
}
28+
},
29+
"files": [
30+
"src/helpers.php"
31+
]
2832
},
2933
"autoload-dev": {
3034
"psr-4": {
@@ -34,7 +38,6 @@
3438
"scripts": {
3539
"test": "vendor/bin/phpunit",
3640
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
37-
3841
},
3942
"config": {
4043
"sort-packages": true

index.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Nejcc\PhpDatatypes\Integers\Signed\Int128;
4+
use Nejcc\PhpDatatypes\Integers\Signed\Int16;
5+
use Nejcc\PhpDatatypes\Integers\Signed\Int32;
6+
use Nejcc\PhpDatatypes\Integers\Signed\Int64;
7+
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
8+
9+
require_once __DIR__ . '/vendor/autoload.php';
10+
11+
class test
12+
{
13+
public Int8 $min;
14+
public Int16 $max;
15+
public Int32 $max2;
16+
// public Int64 $max3;
17+
// public Int128 $max4;
18+
19+
public function __construct(int $int, int $max, int $max2, int $max3, int $max4)
20+
{
21+
$this->min = int8($int);
22+
$this->max = int16($max);
23+
$this->max2 = int32($max2);
24+
// $this->max3 = int64($max3);
25+
// $this->max4 = int128($max4);
26+
}
27+
28+
public function __invoke(): Int8
29+
{
30+
return $this->min;
31+
}
32+
}
33+
34+
$c = new test(8, 20000, 23213,23213,123123);
35+
var_dump($c);
36+
37+

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
verbose="true">
5+
<testsuites>
6+
<testsuite name="Default">
7+
<directory>tests/</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

run.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Create directories
2+
mkdir -p src/Integers/Signed
3+
mkdir -p src/Integers/Unsigned
4+
mkdir -p src/Floats
5+
mkdir -p src/Strings
6+
7+
# Create class files for signed integers
8+
touch src/Integers/Signed/Int8.php
9+
touch src/Integers/Signed/Int16.php
10+
touch src/Integers/Signed/Int32.php
11+
touch src/Integers/Signed/Int64.php
12+
touch src/Integers/Signed/Int128.php
13+
14+
# Create class files for unsigned integers
15+
touch src/Integers/Unsigned/UInt8.php
16+
touch src/Integers/Unsigned/UInt16.php
17+
touch src/Integers/Unsigned/UInt32.php
18+
touch src/Integers/Unsigned/UInt64.php
19+
touch src/Integers/Unsigned/UInt128.php
20+
21+
# Create class files for floats
22+
touch src/Floats/Float32.php
23+
touch src/Floats/Float64.php
24+
touch src/Floats/Float128.php
25+
26+
# Create class file for strings
27+
touch src/Strings/StringType.php
28+
29+
# Initialize Git repository
30+
git init
31+
git add .
32+
git commit -m "Initial directory structure and class files"

src/Abstract/AbstractBigInteger.php

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

src/Floats/AbstractFloat.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Floats;
6+
7+
abstract class AbstractFloat implements FloatInterface
8+
{
9+
protected float|string $value;
10+
11+
public function __construct(float|string $value)
12+
{
13+
$this->value = $value;
14+
}
15+
16+
public function getValue(): float|string
17+
{
18+
return $this->value;
19+
}
20+
21+
public function equals(FloatInterface $other): bool
22+
{
23+
return bccomp((string)$this->value, (string)$other->getValue(), $this->getScale()) === 0;
24+
}
25+
26+
public function compare(FloatInterface $other): int
27+
{
28+
return bccomp((string)$this->value, (string)$other->getValue(), $this->getScale());
29+
}
30+
31+
abstract protected function getScale(): int;
32+
33+
// Abstract methods for arithmetic operations
34+
abstract public function add(FloatInterface $other): static;
35+
36+
abstract public function subtract(FloatInterface $other): static;
37+
38+
abstract public function multiply(FloatInterface $other): static;
39+
40+
abstract public function divide(FloatInterface $other): static;
41+
}

src/Floats/FloatInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Floats;
4+
5+
interface FloatInterface
6+
{
7+
public function getValue(): float|string;
8+
9+
public function add(FloatInterface $other): static;
10+
11+
public function subtract(FloatInterface $other): static;
12+
13+
public function multiply(FloatInterface $other): static;
14+
15+
public function divide(FloatInterface $other): static;
16+
17+
public function equals(FloatInterface $other): bool;
18+
19+
public function compare(FloatInterface $other): int;
20+
}

0 commit comments

Comments
 (0)