Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/php-datatypes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 68 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ This approach has a few key benefits:
### Laravel example

here's how it can be used in practice across different types, focusing on strict handling for both integers and floats:

```php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
use Nejcc\PhpDatatypes\Floats\Float32;
use Illuminate\Http\Request;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;

class TestController
{
Expand Down Expand Up @@ -86,9 +85,9 @@ Here, we're not only safeguarding user IDs but also handling potentially complex
PHP examples

### Integers

```php
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;

$int8 = new Int8(-128); // Minimum value for Int8
echo $int8->getValue(); // -128
Expand All @@ -98,9 +97,9 @@ echo $uint8->getValue(); // 255
```

### Floats

```php
use Nejcc\PhpDatatypes\Floats\Float32;
use Nejcc\PhpDatatypes\Floats\Float64;
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;

$float32 = new Float32(3.14);
echo $float32->getValue(); // 3.14
Expand All @@ -110,8 +109,9 @@ echo $float64->getValue(); // 1.7976931348623157e308
```

### Arithmetic Operations

```php
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;

$int1 = new Int8(50);
$int2 = new Int8(30);
Expand All @@ -121,6 +121,66 @@ echo $result->getValue(); // 80

```

# ROAD MAP

```md
Data Types
├── Scalar Types
│ ├── Integer Types
│ │ ├── Signed Integers
│ │ │ ├── ✓ Int8
│ │ │ ├── ✓ Int16
│ │ │ ├── ✓ Int32
│ │ │ ├── Int64
│ │ │ └── Int128
│ │ └── Unsigned Integers
│ │ ├── ✓ UInt8
│ │ ├── ✓ UInt16
│ │ ├── ✓ UInt32
│ │ ├── UInt64
│ │ └── UInt128
│ ├── Floating Point Types
│ │ ├── ✓ Float32
│ │ ├── ✓ Float64
│ │ ├── Double
│ │ └── Double Floating Point
│ ├── Boolean
│ │ └── Boolean (true/false)
│ ├── Char
│ └── Byte
├── Composite Types
│ ├── Arrays
│ │ ├── StringArray
│ │ ├── IntArray
│ │ ├── FloatArray
│ │ └── Byte Slice
│ ├── Struct
│ │ └── struct { fields of different types }
│ ├── Union
│ │ └── union { shared memory for different types }
│ ├── List
│ └── Dictionary
├── Reference Types
│ ├── Reference Pointer
│ ├── Void (Nullable)
│ └── Channel (Concurrency)
├── Map Types
│ ├── Hashmap
│ └── Map
└── Specialized Types
├── String
├── Double
├── Slice
├── Map
└── Channel
```


### Testing

```bash
Expand Down
12 changes: 8 additions & 4 deletions tests/Floats/Float32Test.php → Tests/Floats/Float32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

declare(strict_types=1);

namespace Nejcc\PhpDatatypes\Tests;
namespace Nejcc\PhpDatatypes\Tests\Floats;

use Nejcc\PhpDatatypes\Floats\Float32;
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;
use PHPUnit\Framework\TestCase;

final class Float32Test extends TestCase
{
public function testSetAndGetValue(): void
{
$float = new Float32(1.234567e10);
$float = float32(1.234567e10);
$this->assertEquals(1.234567e10, $float->getValue());
}

public function testOutOfRangeException(): void
{
// Expect the exception to be thrown before the code that triggers it.
$this->expectException(\OutOfRangeException::class);
new Float32(4.0e38); // Value out of Float32 range

// Now trigger the exception with an out-of-range value.
\float32(4.0e38); // This value is beyond Float32's range.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Nejcc\PhpDatatypes\Tests;

use Nejcc\PhpDatatypes\Floats\Float64;
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
use PHPUnit\Framework\TestCase;

final class Float64Test extends TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Nejcc\PhpDatatypes\Tests\Integers\Signed;

use Nejcc\PhpDatatypes\Integers\Signed\Int16;
use Nejcc\PhpDatatypes\Interfaces\IntegerInterface;

use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16;
use PHPUnit\Framework\TestCase;

class Int16Test extends TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Nejcc\PhpDatatypes\Tests\Integers\Signed;

use Nejcc\PhpDatatypes\Integers\Signed\Int32;
use Nejcc\PhpDatatypes\Interfaces\IntegerInterface;
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int32;
use PHPUnit\Framework\TestCase;

class Int32Test extends TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Nejcc\PhpDatatypes\Tests\Integers\Signed;

use Nejcc\PhpDatatypes\Integers\Signed\Int8;
use Nejcc\PhpDatatypes\Interfaces\IntegerInterface;

use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
use PHPUnit\Framework\TestCase;

class Int8Test extends TestCase
Expand All @@ -24,7 +24,7 @@ public function testInvalidInitialization()

public function testAdditionWithinBounds()
{
$int8a = new Int8(50);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(50);
$int8b = new Int8(77);
$int8c = $int8a->add($int8b);
$this->assertSame(127, $int8c->getValue());
Expand All @@ -34,14 +34,14 @@ public function testAdditionOverflow()
{
$this->expectException(\OverflowException::class);
$int8a = new Int8(100);
$int8b = new Int8(50);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(50);
$int8a->add($int8b);
}

public function testSubtractionWithinBounds()
{
$int8a = new Int8(-50);
$int8b = new Int8(-77);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(-50);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(-77);
$int8c = $int8a->subtract($int8b);
$this->assertSame(27, $int8c->getValue());
}
Expand All @@ -56,17 +56,17 @@ public function testSubtractionUnderflow()

public function testMultiplicationWithinBounds()
{
$int8a = new Int8(10);
$int8b = new Int8(12);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(10);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(12);
$int8c = $int8a->multiply($int8b);
$this->assertSame(120, $int8c->getValue());
}

public function testMultiplicationOverflow()
{
$this->expectException(\OverflowException::class);
$int8a = new Int8(16);
$int8b = new Int8(8);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(16);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(8);
$int8a->multiply($int8b);
}

Expand All @@ -81,22 +81,22 @@ public function testDivisionWithinBounds()
public function testDivisionByZero()
{
$this->expectException(\DivisionByZeroError::class);
$int8a = new Int8(100);
$int8b = new Int8(0);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(100);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(0);
$int8a->divide($int8b);
}

public function testDivisionResultNotInteger()
{
$this->expectException(\UnexpectedValueException::class);
$int8a = new Int8(5);
$int8b = new Int8(2);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(5);
$int8b = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(2);
$int8a->divide($int8b);
}

public function testModulusWithinBounds()
{
$int8a = new Int8(100);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(100);
$int8b = new Int8(30);
$int8c = $int8a->mod($int8b);
$this->assertSame(10, $int8c->getValue());
Expand All @@ -111,7 +111,7 @@ public function testEquality()

public function testInequality()
{
$int8a = new Int8(50);
$int8a = new \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8(50);
$int8b = new Int8(60);
$this->assertFalse($int8a->equals($int8b));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Nejcc\PhpDatatypes\Tests\Integers\Unsigned;

use Nejcc\PhpDatatypes\Integers\Unsigned\UInt16;
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt16;
use PHPUnit\Framework\TestCase;

class UInt16Test extends TestCase
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testSubtractionUnderflow()

public function testMultiplicationWithinBounds()
{
$uint16a = new UInt16(3000);
$uint16a = new \Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt16(3000);
$uint16b = new UInt16(20);
$uint16c = $uint16a->multiply($uint16b);
$this->assertSame(60000, $uint16c->getValue());
Expand Down
Loading
Loading