Home

Awesome

Introducing PHP Datatypes: A Strict and Safe Way to Handle Primitive Data Types

Latest Version on Packagist Total Downloads GitHub Actions

Quality Gate Status Security Rating Maintainability Rating Vulnerabilities Bugs

Coverage

Duplicated Lines (%)

Lines of Code

I'm excited to share my latest PHP package, PHP Datatypes. This library introduces a flexible yet strict way of handling primitive data types like integers, floats, and strings in PHP. It emphasizes type safety and precision, supporting operations for signed and unsigned integers (Int8, UInt8, etc.) and various floating-point formats (Float32, Float64, etc.).

With PHP Datatypes, you get fine-grained control over the data you handle, ensuring your operations stay within valid ranges. It's perfect for anyone looking to avoid common pitfalls like overflows, division by zero, and unexpected type juggling in PHP.

Installation

You can install the package via composer:

composer require nejcc/php-datatypes

Usage

Below are examples of how to use the basic integer and float classes in your project.

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:

namespace App\Http\Controllers;

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

class TestController
{
    public UInt8 $user_id;
    public Float32 $account_balance;

    public function __invoke(Request $request)
    {
        // Validating and assigning UInt8 (ensures non-negative user ID)
        $this->user_id = uint8($request->input('user_id'));
        
        // Validating and assigning Float32 (ensures correct precision)
        $this->account_balance = float32($request->input('account_balance'));
        
        // Now you can safely use the $user_id and $account_balance knowing they are in the right range
        dd([
            'user_id' => $this->user_id->getValue(),
            'account_balance' => $this->account_balance->getValue(),
        ]);
    }
}

Here, we're not only safeguarding user IDs but also handling potentially complex floating-point operations, where precision is critical. This could be especially beneficial for applications in fields like finance or analytics where data integrity is paramount.

PHP examples

Integers

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

$uint8 = new UInt8(255); // Maximum value for UInt8
echo $uint8->getValue(); // 255

Floats

use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;

$float32 = new Float32(3.14);
echo $float32->getValue(); // 3.14

$float64 = new Float64(1.7976931348623157e308); // Maximum value for Float64
echo $float64->getValue(); // 1.7976931348623157e308

Arithmetic Operations

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

$int1 = new Int8(50);
$int2 = new Int8(30);

$result = $int1->add($int2); // Performs addition
echo $result->getValue(); // 80

ROAD MAP

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

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email nejc.cotic@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate by Beyond Code.