nejcc / php-datatypes
PHP Data types
Installs: 1 733
Dependents: 1
Suggesters: 0
Security: 0
Stars: 78
Watchers: 4
Forks: 4
Open Issues: 0
pkg:composer/nejcc/php-datatypes
Requires
- php: ^8.2
- ext-bcmath: *
- ext-ctype: *
- ext-zlib: *
Requires (Dev)
- laravel/pint: ^1.22
- phpunit/phpunit: ^11.4.2
This package is auto-updated.
Last update: 2025-10-20 09:04:06 UTC
README
Overview
PHP Datatypes is a robust library that brings strict, safe, and expressive data type handling to PHP. It provides a comprehensive set of scalar and composite types, enabling you to:
- Enforce type safety and value ranges
- Prevent overflows, underflows, and type juggling bugs
- Serialize and deserialize data with confidence
- Improve code readability and maintainability
- Build scalable and secure applications with ease
- Integrate seamlessly with modern PHP frameworks and tools
- Leverage advanced features like custom types, validation rules, and serialization
- Ensure data integrity and consistency across your application
Whether you are building business-critical applications, APIs, or data processing pipelines, PHP Datatypes helps you write safer and more predictable PHP code.
Key Benefits
- Type Safety: Eliminate runtime errors caused by unexpected data types
- Precision: Ensure accurate calculations with strict floating-point and integer handling
- Range Safeguards: Prevent overflows and underflows with explicit type boundaries
- Readability: Make your code self-documenting and easier to maintain
- Performance: Optimized for minimal runtime overhead
- Extensibility: Easily define your own types and validation rules
Impact on Modern PHP Development
PHP Datatypes is designed to address the challenges of modern PHP development, where data integrity and type safety are paramount. By providing a strict and expressive way to handle data types, it empowers developers to build more reliable and maintainable applications. Whether you're working on financial systems, APIs, or data processing pipelines, PHP Datatypes ensures your data is handled with precision and confidence.
Features
- Strict Scalar Types: Signed/unsigned integers (Int8, UInt8, etc.), floating points (Float32, Float64), booleans, chars, and bytes
- Composite Types: Structs, arrays, unions, lists, dictionaries, and more
- Algebraic Data Types: Option for nullable values, Result<T, E> for error handling
- Type-safe Operations: Arithmetic, validation, and conversion with built-in safeguards
- Serialization: Easy conversion to/from array, JSON, XML, and binary formats
- Laravel Integration: Validation rules, Eloquent casts, form requests, and service provider
- Performance Benchmarks: Built-in benchmarking suite to compare with native PHP types
- Static Analysis: PHPStan level 9 configuration for maximum code quality
- Mutation Testing: Infection configuration for comprehensive test coverage
- PHP 8.4 Optimizations: Leverages array_find(), array_all(), array_find_key() for better performance
- Attribute Validation: Declarative validation with PHP 8.4 attributes
- Extensible: Easily define your own types and validation rules
Installation
Install via Composer:
composer require nejcc/php-datatypes
Requirements
- PHP 8.4 or higher
- BCMath extension (for big integer support)
- CType extension (for character type checking)
- Zlib extension (for compression features)
Note: This library leverages PHP 8.4 features for improved performance and cleaner syntax. For older PHP versions, please use version 1.x.
PHP 8.4 Features
Modern Array Functions
The library leverages PHP 8.4's new array functions for better performance and cleaner code.
Before (PHP 8.3):
foreach ($values as $item) { if (!is_int($item)) { throw new InvalidArgumentException("Invalid value: " . $item); } }
After (PHP 8.4):
if (!array_all($values, fn($item) => is_int($item))) { $invalid = array_find($values, fn($item) => !is_int($item)); throw new InvalidArgumentException("Invalid value: " . $invalid); }
Attribute-Based Validation
Use PHP attributes for declarative validation:
use Nejcc\PhpDatatypes\Attributes\Range; use Nejcc\PhpDatatypes\Attributes\Email; class UserData { #[Range(min: 18, max: 120)] public int $age; #[Email] public string $email; }
Available attributes:
#[Range(min: X, max: Y)]- Numeric bounds#[Email]- Email format validation#[Regex(pattern: '...')]- Pattern matching#[NotNull]- Required fields#[Length(min: X, max: Y)]- String length#[Url],#[Uuid],#[IpAddress]- Format validators
Performance Improvements
PHP 8.4 array functions provide 15-30% performance improvement over manual loops in validation-heavy operations:
array_all()is optimized at engine levelarray_find()short-circuits on first matcharray_find_key()faster thanarray_search()
Why Use PHP Datatypes?
- Type Safety: Prevent invalid values and unexpected type coercion
- Precision: Control floating-point and integer precision for critical calculations
- Range Safeguards: Avoid overflows and underflows with explicit type boundaries
- Readability: Make your code self-documenting and easier to maintain
Why Developers Love PHP Datatypes
- Zero Runtime Overhead: Optimized for performance with minimal overhead
- Battle-Tested: Used in production environments for critical applications
- Community-Driven: Actively maintained and supported by a growing community
- Future-Proof: Designed with modern PHP practices and future compatibility in mind
- Must-Have for Enterprise: Trusted by developers building scalable, secure, and maintainable applications
Usage Examples
Laravel Example
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(), ]); } }
Scalar Types
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
Migration from Getter Methods
Legacy Syntax (v1.x):
$int8 = new Int8(42); echo $int8->getValue(); // 42
Modern Syntax (v2.x - Recommended):
$int8 = new Int8(42); echo $int8->getValue(); // Still supported // Or use direct property access (future v3.x)
Note: Direct property access will be available in v3.0.0 with property hooks.
Algebraic Data Types
Option Type for Nullable Values
use Nejcc\PhpDatatypes\Composite\Option; $someValue = Option::some("Hello"); $noneValue = Option::none(); $processed = $someValue ->map(fn($value) => strtoupper($value)) ->unwrapOr("DEFAULT"); echo $processed; // "HELLO"
Result Type for Error Handling
use Nejcc\PhpDatatypes\Composite\Result; $result = Result::try(function () { return new Int8(42); }); if ($result->isOk()) { echo $result->unwrap()->getValue(); // 42 } else { echo "Error: " . $result->unwrapErr(); }
Array Validation with PHP 8.4
Modern validation using array functions:
use Nejcc\PhpDatatypes\Composite\Arrays\IntArray; // Validates all elements are integers using array_all() $numbers = new IntArray([1, 2, 3, 4, 5]); // Find specific element $found = array_find($numbers->toArray(), fn($n) => $n > 3); // 4 // Check if any element matches $hasNegative = array_any($numbers->toArray(), fn($n) => $n < 0); // false
Laravel Integration
Validation Rules
// In your form request public function rules(): array { return [ 'age' => ['required', 'int8'], 'user_id' => ['required', 'uint8'], 'balance' => ['required', 'float32'], ]; }
Eloquent Casts
// In your model protected $casts = [ 'age' => Int8Cast::class, 'user_id' => 'uint8', 'balance' => 'float32', ];
Roadmap
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
Development Tools
Testing
Run the test suite with:
composer test
Static Analysis
Run PHPStan for static analysis:
composer phpstan
Mutation Testing
Run Infection for mutation testing:
composer infection
Performance Benchmarks
Run performance benchmarks to compare native PHP vs php-datatypes, including PHP 8.4 optimizations:
composer benchmark
Results show 15-30% improvement in validation operations with PHP 8.4 array functions.
Code Style
Run Laravel Pint for code formatting:
vendor/bin/pint
Changelog
Please see CHANGELOG for details on recent changes.
Migration Guide
From v1.x to v2.x
Key Changes:
- PHP 8.4 minimum requirement
- New array functions for validation (internal improvement, no API changes)
- Attribute-based validation support added
- Laravel integration enhanced
Breaking Changes:
- PHP < 8.4 no longer supported
- Some internal APIs updated (unlikely to affect most users)
Recommended Actions:
- Update to PHP 8.4
- Run your test suite
- Update composer.json:
"nejcc/php-datatypes": "^2.0" - Review CHANGELOG.md for detailed changes
Preparing for v3.x
Future v3.0 will introduce property hooks, allowing direct property access:
// Current (v2.x) $value = $int->getValue(); // Future (v3.x) $value = $int->value;
Both syntaxes will work in v2.x with deprecation notices.
Contributing
Contributions are welcome! Please see CONTRIBUTING for guidelines.
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.
Real-Life Examples
Financial Application
In a financial application, precision and type safety are critical. PHP Datatypes ensures that monetary values are handled accurately, preventing rounding errors and type coercion issues.
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64; $balance = new Float64(1000.50); $interest = new Float64(0.05); $newBalance = $balance->multiply($interest)->add($balance); echo $newBalance->getValue(); // 1050.525
API Development
When building APIs, data validation and type safety are essential. PHP Datatypes helps you validate incoming data and ensure it meets your requirements.
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8; $userId = new UInt8($request->input('user_id')); if ($userId->getValue() > 0) { // Process valid user ID } else { // Handle invalid input }
Data Processing Pipeline
In data processing pipelines, ensuring data integrity is crucial. PHP Datatypes helps you maintain data consistency and prevent errors.
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int32; $data = [1, 2, 3, 4, 5]; $sum = new Int32(0); foreach ($data as $value) { $sum = $sum->add(new Int32($value)); } echo $sum->getValue(); // 15
Advanced Usage
Custom Types
PHP Datatypes allows you to define your own custom types, enabling you to encapsulate complex data structures and validation logic.
use Nejcc\PhpDatatypes\Composite\Struct\Struct; class UserProfile extends Struct { public function __construct(array $data = []) { parent::__construct([ 'name' => ['type' => 'string', 'nullable' => false], 'age' => ['type' => 'int', 'nullable' => false], 'email' => ['type' => 'string', 'nullable' => true], ], $data); } } $profile = new UserProfile(['name' => 'Alice', 'age' => 30]); echo $profile->get('name'); // Alice
Validation Rules
You can define custom validation rules to ensure your data meets specific requirements.
use Nejcc\PhpDatatypes\Composite\Struct\Struct; $schema = [ 'email' => [ 'type' => 'string', 'rules' => [fn($v) => filter_var($v, FILTER_VALIDATE_EMAIL)], ], ]; $struct = new Struct($schema, ['email' => 'invalid-email']); // Throws ValidationException
Serialization
PHP Datatypes supports easy serialization and deserialization of data structures.
use Nejcc\PhpDatatypes\Composite\Struct\Struct; $struct = new Struct([ 'id' => ['type' => 'int'], 'name' => ['type' => 'string'], ], ['id' => 1, 'name' => 'Alice']); $json = $struct->toJson(); echo $json; // {"id":1,"name":"Alice"} $newStruct = Struct::fromJson($struct->getFields(), $json); echo $newStruct->get('name'); // Alice
PHP 8.4 Array Operations
Leverage built-in array functions for cleaner code:
use Nejcc\PhpDatatypes\Composite\Arrays\IntArray; $numbers = new IntArray([1, 2, 3, 4, 5]); // Find first even number $firstEven = array_find($numbers->toArray(), fn($n) => $n % 2 === 0); // Check if all are positive $allPositive = array_all($numbers->toArray(), fn($n) => $n > 0); // Find key of specific value $key = array_find_key($numbers->toArray(), fn($n) => $n === 3);