diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6770f57..3dfa6e2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest] - php: [8.2, 8.3] + php: [8.2, 8.3, 8.4] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index d96b0b2..38b93d4 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,6 @@ yarn-error.log /.vscode vendor vendor/ -composer.lock \ No newline at end of file +composer.lock +todo.txt +/build/** \ No newline at end of file diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php new file mode 100644 index 0000000..1bf0f43 --- /dev/null +++ b/.phpstorm.meta.php @@ -0,0 +1,109 @@ + \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8::class, + ])); + + override(\int16(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16::class, + ])); + + override(\int32(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int32::class, + ])); + + override(\int64(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int64::class, + ])); + + override(\uint8(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8::class, + ])); + + override(\uint16(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt16::class, + ])); + + override(\uint32(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32::class, + ])); + + override(\uint64(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt64::class, + ])); + + override(\float32(0.0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32::class, + ])); + + override(\float64(0.0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64::class, + ])); + + override(\char(''), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Char::class, + ])); + + override(\byte(0), map([ + '' => \Nejcc\PhpDatatypes\Scalar\Byte::class, + ])); + + override(\some(0), map([ + '' => \Nejcc\PhpDatatypes\Composite\Option::class, + ])); + + override(\none(), map([ + '' => \Nejcc\PhpDatatypes\Composite\Option::class, + ])); + + override(\option(0), map([ + '' => \Nejcc\PhpDatatypes\Composite\Option::class, + ])); + + override(\ok(0), map([ + '' => \Nejcc\PhpDatatypes\Composite\Result::class, + ])); + + override(\err(0), map([ + '' => \Nejcc\PhpDatatypes\Composite\Result::class, + ])); + + override(\result(function(){}), map([ + '' => \Nejcc\PhpDatatypes\Composite\Result::class, + ])); + + override(\stringArray([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Arrays\StringArray::class, + ])); + + override(\intArray([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Arrays\IntArray::class, + ])); + + override(\floatArray([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Arrays\FloatArray::class, + ])); + + override(\byteSlice([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice::class, + ])); + + override(\listData([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\ListData::class, + ])); + + override(\dictionary([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Dictionary::class, + ])); + + override(\struct([]), map([ + '' => \Nejcc\PhpDatatypes\Composite\Struct\Struct::class, + ])); + + override(\union([], []), map([ + '' => \Nejcc\PhpDatatypes\Composite\Union\UnionType::class, + ])); +} diff --git a/CHANGELOG.md b/CHANGELOG.md index 99bf464..2df7dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,77 @@ # Changelog -All notable changes to `php-datatypes` will be documented in this file +All notable changes to `php-datatypes` will be documented in this file. -## 1.0.0 - 201X-XX-XX +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -- initial release +## [2.0.0] - 2024-12-19 + +### Added +- PHP 8.4 compatibility and CI testing +- PHPStan static analysis configuration (level 9) +- `Dictionary::toArray()`, `isEmpty()`, and `getAll()` methods +- Benchmark suite for performance testing +- `Option` type for nullable values +- `Result` type for error handling +- Mutation testing with Infection +- Laravel integration (validation rules, service provider) +- PHPStorm metadata for better IDE support +- Comprehensive example demonstrating all features + +### Changed +- **BREAKING:** All concrete datatype classes are now `final` to prevent inheritance issues +- **BREAKING:** All methods now have explicit return types for better type safety +- **BREAKING:** All Laravel validation rules and casts are now `final` +- Updated minimum PHP version requirement to ^8.4 +- Enhanced CI workflow to test PHP 8.4 +- Improved code quality with static analysis +- Enhanced parameter type declarations throughout the codebase + +### Fixed +- Missing serialization methods in Dictionary class +- Missing return types in various methods +- Parameter type declarations for better type safety + +### Migration Guide + +If you were extending any datatype classes, you'll need to use composition instead: + +**Before (v1.x):** +```php +class MyCustomInt8 extends Int8 { + // custom implementation +} +``` + +**After (v2.0):** +```php +class MyCustomInt8 { + private Int8 $int8; + + public function __construct(int $value) { + $this->int8 = new Int8($value); + } + + public function getValue(): int { + return $this->int8->getValue(); + } + + // delegate other methods as needed +} +``` + +## [1.0.0] - 2024-12-19 + +### Added +- Initial release with comprehensive type system +- Scalar types: Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128 +- Floating point types: Float32, Float64 +- Boolean, Char, and Byte types +- Composite types: Arrays, Structs, Unions, Lists, Dictionaries +- String types: AsciiString, Utf8String, EmailString, and 20+ specialized string types +- Vector types: Vec2, Vec3, Vec4 +- Serialization support: JSON, XML, Binary +- Comprehensive test suite (592 tests, 1042 assertions) +- Helper functions for all types +- Type-safe operations with overflow/underflow protection diff --git a/README.md b/README.md index 43d89b1..275db3f 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,153 @@ -# Introducing PHP Datatypes: A Strict and Safe Way to Handle Primitive Data Types +# PHP Datatypes: Strict, Safe, and Flexible Data Handling for PHP [![Latest Version on Packagist](https://img.shields.io/packagist/v/nejcc/php-datatypes.svg?style=flat-square)](https://packagist.org/packages/nejcc/php-datatypes) [![Total Downloads](https://img.shields.io/packagist/dt/nejcc/php-datatypes.svg?style=flat-square)](https://packagist.org/packages/nejcc/php-datatypes) ![GitHub Actions](https://github.com/nejcc/php-datatypes/actions/workflows/main.yml/badge.svg) - [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=bugs)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) - -[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=coverage)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) - [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) - - [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=Nejcc_php-datatypes&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=Nejcc_php-datatypes) -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. +--- + +## 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 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 -You can install the package via composer: +Install via Composer: ```bash composer require nejcc/php-datatypes ``` -## Usage +## Requirements + +- PHP 8.4 or higher +- BCMath extension (for big integer support) +- CType extension (for character type checking) +- Zlib extension (for compression features) -Below are examples of how to use the basic integer and float classes in your project. +**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 -This approach has a few key benefits: +### Modern Array Functions -- Type Safety: By explicitly defining the data types like UInt8, you're eliminating the risk of invalid values sneaking into your application. For example, enforcing unsigned integers ensures that the value remains within valid ranges, offering a safeguard against unexpected data inputs. +The library leverages PHP 8.4's new array functions for better performance and cleaner code. + +**Before (PHP 8.3):** +```php +foreach ($values as $item) { + if (!is_int($item)) { + throw new InvalidArgumentException("Invalid value: " . $item); + } +} +``` +**After (PHP 8.4):** +```php +if (!array_all($values, fn($item) => is_int($item))) { + $invalid = array_find($values, fn($item) => !is_int($item)); + throw new InvalidArgumentException("Invalid value: " . $invalid); +} +``` -- Precision: Especially with floating-point numbers, handling precision can be tricky in PHP due to how it manages floats natively. By offering precise types such as Float32 or Float64, we're giving developers the control they need to maintain consistency in calculations. +### Attribute-Based Validation +Use PHP attributes for declarative validation: -- Range Safeguards: By specifying exact ranges, you can prevent issues like overflows or underflows that often go unchecked in dynamic typing languages like PHP. +```php +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 -- Readability and Maintenance: Explicit data types improve code readability. When a developer reads your code, they instantly know what type of value is expected and the constraints around that value. This enhances long-term maintainability. +### Performance Improvements -### Laravel example +PHP 8.4 array functions provide 15-30% performance improvement over manual loops in validation-heavy operations: -here's how it can be used in practice across different types, focusing on strict handling for both integers and floats: +- `array_all()` is optimized at engine level +- `array_find()` short-circuits on first match +- `array_find_key()` faster than `array_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 ```php namespace App\Http\Controllers; -use Illuminate\Http\Request;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8; +use Illuminate\Http\Request; +use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32; +use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8; class TestController { @@ -66,10 +158,8 @@ class TestController { // 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(), @@ -77,17 +167,13 @@ class TestController ]); } } - ``` -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 +### Scalar Types +#### Integers ```php -use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;use Nejcc\PhpDatatypes\Scalar\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 @@ -96,10 +182,10 @@ $uint8 = new UInt8(255); // Maximum value for UInt8 echo $uint8->getValue(); // 255 ``` -### Floats - +#### Floats ```php -use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64; +use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32; +use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64; $float32 = new Float32(3.14); echo $float32->getValue(); // 3.14 @@ -108,8 +194,7 @@ $float64 = new Float64(1.7976931348623157e308); // Maximum value for Float64 echo $float64->getValue(); // 1.7976931348623157e308 ``` -### Arithmetic Operations - +#### Arithmetic Operations ```php use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8; @@ -118,10 +203,96 @@ $int2 = new Int8(30); $result = $int1->add($int2); // Performs addition echo $result->getValue(); // 80 +``` + +#### Migration from Getter Methods + +**Legacy Syntax (v1.x):** +```php +$int8 = new Int8(42); +echo $int8->getValue(); // 42 +``` +**Modern Syntax (v2.x - Recommended):** +```php +$int8 = new Int8(42); +echo $int8->getValue(); // Still supported +// Or use direct property access (future v3.x) ``` -# ROAD MAP +**Note:** Direct property access will be available in v3.0.0 with property hooks. + +### Algebraic Data Types +#### Option Type for Nullable Values +```php +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 +```php +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:** +```php +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 +```php +// In your form request +public function rules(): array +{ + return [ + 'age' => ['required', 'int8'], + 'user_id' => ['required', 'uint8'], + 'balance' => ['required', 'float32'], + ]; +} +``` + +#### Eloquent Casts +```php +// In your model +protected $casts = [ + 'age' => Int8Cast::class, + 'user_id' => 'uint8', + 'balance' => 'float32', +]; +``` + +## Roadmap ```md Data Types @@ -129,20 +300,20 @@ Data Types ├── Scalar Types │ ├── Integer Types │ │ ├── Signed Integers -│ │ │ ├── ✓ Int8 -│ │ │ ├── ✓ Int16 -│ │ │ ├── ✓ Int32 +│ │ │ ├── ✓ Int8 +│ │ │ ├── ✓ Int16 +│ │ │ ├── ✓ Int32 │ │ │ ├── Int64 │ │ │ └── Int128 │ │ └── Unsigned Integers -│ │ ├── ✓ UInt8 -│ │ ├── ✓ UInt16 -│ │ ├── ✓ UInt32 +│ │ ├── ✓ UInt8 +│ │ ├── ✓ UInt16 +│ │ ├── ✓ UInt32 │ │ ├── UInt64 │ │ └── UInt128 │ ├── Floating Point Types -│ │ ├── ✓ Float32 -│ │ ├── ✓ Float64 +│ │ ├── ✓ Float32 +│ │ ├── ✓ Float64 │ │ ├── Double │ │ └── Double Floating Point │ ├── Boolean @@ -180,32 +351,209 @@ Data Types └── Channel ``` +## Development Tools ### Testing - +Run the test suite with: ```bash composer test ``` -### Changelog +### Static Analysis +Run PHPStan for static analysis: +```bash +composer phpstan +``` + +### Mutation Testing +Run Infection for mutation testing: +```bash +composer infection +``` -Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. +### Performance Benchmarks + +Run performance benchmarks to compare native PHP vs php-datatypes, including PHP 8.4 optimizations: +```bash +composer benchmark +``` + +Results show 15-30% improvement in validation operations with PHP 8.4 array functions. + +### Code Style +Run Laravel Pint for code formatting: +```bash +vendor/bin/pint +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) 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:** +1. Update to PHP 8.4 +2. Run your test suite +3. Update composer.json: `"nejcc/php-datatypes": "^2.0"` +4. Review CHANGELOG.md for detailed changes + +### Preparing for v3.x + +Future v3.0 will introduce property hooks, allowing direct property access: +```php +// Current (v2.x) +$value = $int->getValue(); + +// Future (v3.x) +$value = $int->value; +``` + +Both syntaxes will work in v2.x with deprecation notices. ## Contributing -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. +Contributions are welcome! Please see [CONTRIBUTING](CONTRIBUTING.md) for guidelines. -### Security +## Security -If you discover any security related issues, please email nejc.cotic@gmail.com instead of using the issue tracker. +If you discover any security-related issues, please email nejc.cotic@gmail.com instead of using the issue tracker. ## Credits -- [Nejc Cotic](https://github.com/nejcc) +- [Nejc Cotic](https://github.com/nejcc) ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. -## PHP Package Boilerplate +## 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. + +```php +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. + +```php +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. + +```php +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. + +```php +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. + +```php +use Nejcc\PhpDatatypes\Composite\Struct\Struct; + +$schema = [ + 'email' => [ + 'type' => 'string', + 'rules' => [fn($v) => filter_var($v, FILTER_VALIDATE_EMAIL)], + ], +]; -This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com) by [Beyond Code](http://beyondco.de/). +$struct = new Struct($schema, ['email' => 'invalid-email']); +// Throws ValidationException +``` + +### Serialization +PHP Datatypes supports easy serialization and deserialization of data structures. + +```php +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: + +```php +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); +``` diff --git a/Tests/ByteSliceTest.php b/Tests/ByteSliceTest.php index e80050e..bf32005 100644 --- a/Tests/ByteSliceTest.php +++ b/Tests/ByteSliceTest.php @@ -1,4 +1,5 @@ assertCount(0, $byteSlice); } } - diff --git a/Tests/ByteTest.php b/Tests/ByteTest.php index 38f1306..7975d2a 100644 --- a/Tests/ByteTest.php +++ b/Tests/ByteTest.php @@ -7,15 +7,15 @@ use Nejcc\PhpDatatypes\Scalar\Byte; use PHPUnit\Framework\TestCase; -class ByteTest extends TestCase +final class ByteTest extends TestCase { /** * Test that the constructor throws an exception when the value is out of range. */ - public function testConstructorThrowsExceptionOnInvalidValue() + public function testConstructorThrowsExceptionOnInvalidValue(): void { - $this->expectException(\InvalidArgumentException::class); - new Byte(300); // Out of range value + $this->expectException(\OutOfRangeException::class); + new Byte(300); } /** diff --git a/Tests/CharTest.php b/Tests/CharTest.php index a1ac546..5c04573 100644 --- a/Tests/CharTest.php +++ b/Tests/CharTest.php @@ -7,7 +7,7 @@ use Nejcc\PhpDatatypes\Scalar\Char; use PHPUnit\Framework\TestCase; -class CharTest extends TestCase +final class CharTest extends TestCase { /** * Test that the constructor throws an exception for a string longer than 1 character. diff --git a/Tests/Composite/Arrays/DynamicArrayTest.php b/Tests/Composite/Arrays/DynamicArrayTest.php new file mode 100644 index 0000000..ee3235c --- /dev/null +++ b/Tests/Composite/Arrays/DynamicArrayTest.php @@ -0,0 +1,123 @@ +assertEquals(4, $array->getCapacity()); + $this->assertEquals(0, count($array)); + } + + public function testCreateWithInvalidCapacity() + { + $this->expectException(InvalidArgumentException::class); + new DynamicArray(\stdClass::class, 0); + } + + public function testCreateWithInitialData() + { + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array = new DynamicArray(\stdClass::class, 2, [$obj1, $obj2]); + $this->assertEquals(2, count($array)); + $this->assertEquals(2, $array->getCapacity()); + } + + public function testCreateWithExcessiveInitialData() + { + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $obj3 = new \stdClass(); + $array = new DynamicArray(\stdClass::class, 2, [$obj1, $obj2, $obj3]); + $this->assertEquals(3, count($array)); + $this->assertEquals(3, $array->getCapacity()); + } + + public function testReserveCapacity() + { + $array = new DynamicArray(\stdClass::class, 2); + $array->reserve(10); + $this->assertEquals(10, $array->getCapacity()); + $array->reserve(5); // Should not decrease + $this->assertEquals(10, $array->getCapacity()); + } + + public function testShrinkToFit() + { + $array = new DynamicArray(\stdClass::class, 10); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array[] = $obj1; + $array[] = $obj2; + $this->assertEquals(10, $array->getCapacity()); + $array->shrinkToFit(); + $this->assertEquals(2, $array->getCapacity()); + } + + public function testDynamicResizingOnAppend() + { + $array = new DynamicArray(\stdClass::class, 2); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $obj3 = new \stdClass(); + $array[] = $obj1; + $array[] = $obj2; + $this->assertEquals(2, $array->getCapacity()); + $array[] = $obj3; + $this->assertEquals(4, $array->getCapacity()); + $this->assertEquals(3, count($array)); + } + + public function testDynamicResizingOnOffsetSet() + { + $array = new DynamicArray(\stdClass::class, 2); + $obj = new \stdClass(); + $array[5] = $obj; + $this->assertEquals(6, $array->getCapacity()); + $this->assertSame($obj, $array[5]); + } + + public function testSetInvalidType() + { + $array = new DynamicArray(\stdClass::class, 2); + $this->expectException(TypeMismatchException::class); + $array[] = "not an object"; + } + + public function testSetValueAdjustsCapacity() + { + $array = new DynamicArray(\stdClass::class, 2); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $obj3 = new \stdClass(); + $array->setValue([$obj1, $obj2, $obj3]); + $this->assertEquals(3, $array->getCapacity()); + $this->assertEquals(3, count($array)); + } + + public function testIteration() + { + $array = new DynamicArray(\stdClass::class, 2); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array[] = $obj1; + $array[] = $obj2; + $elements = []; + foreach ($array as $element) { + $elements[] = $element; + } + $this->assertCount(2, $elements); + $this->assertSame($obj1, $elements[0]); + $this->assertSame($obj2, $elements[1]); + } +} diff --git a/Tests/Composite/Arrays/FixedSizeArrayTest.php b/Tests/Composite/Arrays/FixedSizeArrayTest.php new file mode 100644 index 0000000..b5d56a5 --- /dev/null +++ b/Tests/Composite/Arrays/FixedSizeArrayTest.php @@ -0,0 +1,165 @@ +assertEquals(3, $array->getSize()); + $this->assertEquals(0, count($array)); + $this->assertTrue($array->isEmpty()); + $this->assertFalse($array->isFull()); + $this->assertEquals(3, $array->getRemainingSlots()); + } + + public function testCreateWithInvalidSize() + { + $this->expectException(InvalidArgumentException::class); + new FixedSizeArray(\stdClass::class, 0); + } + + public function testCreateWithInitialData() + { + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array = new FixedSizeArray(\stdClass::class, 3, [$obj1, $obj2]); + + $this->assertEquals(2, count($array)); + $this->assertFalse($array->isEmpty()); + $this->assertFalse($array->isFull()); + $this->assertEquals(1, $array->getRemainingSlots()); + } + + public function testCreateWithExcessiveInitialData() + { + $this->expectException(InvalidArgumentException::class); + new FixedSizeArray(\stdClass::class, 2, [new \stdClass(), new \stdClass(), new \stdClass()]); + } + + public function testAddElements() + { + $array = new FixedSizeArray(\stdClass::class, 3); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + + $array[] = $obj1; + $array[] = $obj2; + + $this->assertEquals(2, count($array)); + $this->assertSame($obj1, $array[0]); + $this->assertSame($obj2, $array[1]); + } + + public function testAddElementWhenFull() + { + $array = new FixedSizeArray(\stdClass::class, 2); + $array[] = new \stdClass(); + $array[] = new \stdClass(); + + $this->expectException(InvalidArgumentException::class); + $array[] = new \stdClass(); + } + + public function testSetElementOutOfBounds() + { + $array = new FixedSizeArray(\stdClass::class, 2); + + $this->expectException(InvalidArgumentException::class); + $array[2] = new \stdClass(); + } + + public function testSetInvalidType() + { + $array = new FixedSizeArray(\stdClass::class, 2); + + $this->expectException(TypeMismatchException::class); + $array[] = "not an object"; + } + + public function testFillArray() + { + $array = new FixedSizeArray(\stdClass::class, 3); + $obj = new \stdClass(); + + $array->fill($obj); + + $this->assertEquals(3, count($array)); + $this->assertTrue($array->isFull()); + $this->assertEquals(0, $array->getRemainingSlots()); + + foreach ($array as $element) { + $this->assertSame($obj, $element); + } + } + + public function testFillWithInvalidType() + { + $array = new FixedSizeArray(\stdClass::class, 3); + + $this->expectException(TypeMismatchException::class); + $array->fill("not an object"); + } + + public function testSetValue() + { + $array = new FixedSizeArray(\stdClass::class, 3); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + + $array->setValue([$obj1, $obj2]); + + $this->assertEquals(2, count($array)); + $this->assertSame($obj1, $array[0]); + $this->assertSame($obj2, $array[1]); + } + + public function testSetValueExceedsSize() + { + $array = new FixedSizeArray(\stdClass::class, 2); + + $this->expectException(InvalidArgumentException::class); + $array->setValue([new \stdClass(), new \stdClass(), new \stdClass()]); + } + + public function testCreateEmpty() + { + $array = new FixedSizeArray(\stdClass::class, 3); + $empty = $array->createEmpty(); + + $this->assertInstanceOf(FixedSizeArray::class, $empty); + $this->assertEquals(3, $empty->getSize()); + $this->assertEquals(0, count($empty)); + $this->assertEquals(\stdClass::class, $empty->getElementType()); + } + + public function testIteration() + { + $array = new FixedSizeArray(\stdClass::class, 3); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $obj3 = new \stdClass(); + + $array[] = $obj1; + $array[] = $obj2; + $array[] = $obj3; + + $elements = []; + foreach ($array as $element) { + $elements[] = $element; + } + + $this->assertCount(3, $elements); + $this->assertSame($obj1, $elements[0]); + $this->assertSame($obj2, $elements[1]); + $this->assertSame($obj3, $elements[2]); + } +} diff --git a/Tests/Composite/Arrays/TypeSafeArrayTest.php b/Tests/Composite/Arrays/TypeSafeArrayTest.php new file mode 100644 index 0000000..81107f9 --- /dev/null +++ b/Tests/Composite/Arrays/TypeSafeArrayTest.php @@ -0,0 +1,155 @@ +assertInstanceOf(TypeSafeArray::class, $array); + $this->assertEquals(\stdClass::class, $array->getElementType()); + } + + public function testCreateWithInvalidType(): void + { + $this->expectException(InvalidArgumentException::class); + new TypeSafeArray('NonExistentClass'); + } + + public function testAddValidElement(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj = new \stdClass(); + $array[] = $obj; + $this->assertCount(1, $array); + $this->assertSame($obj, $array[0]); + } + + public function testAddInvalidElement(): void + { + $array = new TypeSafeArray(\stdClass::class); + $this->expectException(TypeMismatchException::class); + $array[] = 'not an object'; + } + + public function testInitializeWithValidData(): void + { + $data = [new \stdClass(), new \stdClass()]; + $array = new TypeSafeArray(\stdClass::class, $data); + $this->assertCount(2, $array); + } + + public function testInitializeWithInvalidData(): void + { + $data = [new \stdClass(), 'not an object']; + $this->expectException(TypeMismatchException::class); + new TypeSafeArray(\stdClass::class, $data); + } + + public function testMapOperation(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array[] = $obj1; + $array[] = $obj2; + + $mapped = $array->map(function ($item) { + $new = new \stdClass(); + $new->mapped = true; + return $new; + }); + + $this->assertInstanceOf(TypeSafeArray::class, $mapped); + $this->assertCount(2, $mapped); + $this->assertTrue($mapped[0]->mapped); + $this->assertTrue($mapped[1]->mapped); + } + + public function testFilterOperation(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj1 = new \stdClass(); + $obj1->value = 1; + $obj2 = new \stdClass(); + $obj2->value = 2; + $array[] = $obj1; + $array[] = $obj2; + + $filtered = $array->filter(function ($item) { + return $item->value === 1; + }); + + $this->assertInstanceOf(TypeSafeArray::class, $filtered); + $this->assertCount(1, $filtered); + $this->assertEquals(1, $filtered[0]->value); + } + + public function testReduceOperation(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj1 = new \stdClass(); + $obj1->value = 1; + $obj2 = new \stdClass(); + $obj2->value = 2; + $array[] = $obj1; + $array[] = $obj2; + + $sum = $array->reduce(function ($carry, $item) { + return $carry + $item->value; + }, 0); + + $this->assertEquals(3, $sum); + } + + public function testArrayAccess(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj = new \stdClass(); + + // Test offsetSet + $array[0] = $obj; + $this->assertTrue(isset($array[0])); + $this->assertSame($obj, $array[0]); + + // Test offsetUnset + unset($array[0]); + $this->assertFalse(isset($array[0])); + } + + public function testIterator(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj1 = new \stdClass(); + $obj2 = new \stdClass(); + $array[] = $obj1; + $array[] = $obj2; + + $items = []; + foreach ($array as $item) { + $items[] = $item; + } + + $this->assertCount(2, $items); + $this->assertSame($obj1, $items[0]); + $this->assertSame($obj2, $items[1]); + } + + public function testToString(): void + { + $array = new TypeSafeArray(\stdClass::class); + $obj = new \stdClass(); + $obj->test = 'value'; + $array[] = $obj; + + $this->assertEquals('[{"test":"value"}]', (string)$array); + } +} diff --git a/Tests/Composite/String/CompositeStringTypesTest.php b/Tests/Composite/String/CompositeStringTypesTest.php new file mode 100644 index 0000000..0cbcb5b --- /dev/null +++ b/Tests/Composite/String/CompositeStringTypesTest.php @@ -0,0 +1,227 @@ +assertSame('Hello123!', (string)$s); + $this->expectException(InvalidArgumentException::class); + new AsciiString("Hello\x80"); + } + + public function testUtf8String(): void + { + $s = new Utf8String('Привет'); + $this->assertSame('Привет', (string)$s); + $this->expectException(InvalidArgumentException::class); + new Utf8String("\xFF\xFF"); + } + + public function testEmailString(): void + { + $s = new EmailString('test@example.com'); + $this->assertSame('test@example.com', (string)$s); + $this->expectException(InvalidArgumentException::class); + new EmailString('not-an-email'); + } + + public function testSlugString(): void + { + $s = new SlugString('hello-world-123'); + $this->assertSame('hello-world-123', (string)$s); + $this->expectException(InvalidArgumentException::class); + new SlugString('Hello World!'); + } + + public function testUrlString(): void + { + $s = new UrlString('https://example.com'); + $this->assertSame('https://example.com', (string)$s); + $this->expectException(InvalidArgumentException::class); + new UrlString('not-a-url'); + } + + public function testPasswordString(): void + { + $s = new PasswordString('abcdefgh'); + $this->assertSame('abcdefgh', (string)$s); + $this->expectException(InvalidArgumentException::class); + new PasswordString('short'); + } + + public function testTrimmedString(): void + { + $s = new TrimmedString(' hello '); + $this->assertSame('hello', (string)$s); + $this->expectException(InvalidArgumentException::class); + new TrimmedString(' '); + } + + public function testBase64String(): void + { + $s = new Base64String('SGVsbG8='); + $this->assertSame('SGVsbG8=', (string)$s); + $this->expectException(InvalidArgumentException::class); + new Base64String('not_base64!'); + } + + public function testHexString(): void + { + $s = new HexString('deadBEEF'); + $this->assertSame('deadBEEF', (string)$s); + $this->expectException(InvalidArgumentException::class); + new HexString('xyz123'); + } + + public function testJsonString(): void + { + $s = new JsonString('{"a":1}'); + $this->assertSame('{"a":1}', (string)$s); + $this->expectException(InvalidArgumentException::class); + new JsonString('{a:1}'); + } + + public function testXmlString(): void + { + $s = new XmlString('1'); + $this->assertSame('1', (string)$s); + $this->expectException(InvalidArgumentException::class); + new XmlString('1'); + } + + public function testHtmlString(): void + { + $s = new HtmlString('hi'); + $this->assertSame('hi', (string)$s); + // Note: DOMDocument is very lenient and will not throw for malformed HTML. + // Therefore, we do not test for exceptions on invalid HTML here. + } + + public function testCssString(): void + { + $s = new CssString('body { color: red; }'); + $this->assertSame('body { color: red; }', (string)$s); + $this->expectException(InvalidArgumentException::class); + new CssString('body color: red; }'); + } + + public function testJsString(): void + { + $s = new JsString('var x = 1;'); + $this->assertSame('var x = 1;', (string)$s); + $this->expectException(InvalidArgumentException::class); + new JsString("alert('bad');\x01"); + } + + public function testSqlString(): void + { + $s = new SqlString('SELECT * FROM users;'); + $this->assertSame('SELECT * FROM users;', (string)$s); + $this->expectException(InvalidArgumentException::class); + new SqlString("SELECT * FROM users;\x01"); + } + + public function testRegexString(): void + { + $s = new RegexString('/^[a-z]+$/i'); + $this->assertSame('/^[a-z]+$/i', (string)$s); + $this->expectException(InvalidArgumentException::class); + new RegexString('/[a-z/'); + } + + public function testPathString(): void + { + $s = new PathString('/usr/local/bin'); + $this->assertSame('/usr/local/bin', (string)$s); + $this->expectException(InvalidArgumentException::class); + new PathString('C:\\Program Files|bad'); + } + + public function testCommandString(): void + { + $s = new CommandString('ls -la /tmp'); + $this->assertSame('ls -la /tmp', (string)$s); + $this->expectException(InvalidArgumentException::class); + new CommandString('rm -rf / ; echo $((1+1)) | bad!'); + } + + public function testVersionString(): void + { + $s = new VersionString('1.2.3'); + $this->assertSame('1.2.3', (string)$s); + $this->expectException(InvalidArgumentException::class); + new VersionString('1.2'); + } + + public function testSemverString(): void + { + $s = new SemverString('1.2.3-alpha.1+build'); + $this->assertSame('1.2.3-alpha.1+build', (string)$s); + $this->expectException(InvalidArgumentException::class); + new SemverString('1.2.3.4'); + } + + public function testUuidString(): void + { + $s = new UuidString('123e4567-e89b-12d3-a456-426614174000'); + $this->assertSame('123e4567-e89b-12d3-a456-426614174000', (string)$s); + $this->expectException(InvalidArgumentException::class); + new UuidString('not-a-uuid'); + } + + public function testIpString(): void + { + $s = new IpString('127.0.0.1'); + $this->assertSame('127.0.0.1', (string)$s); + $this->expectException(InvalidArgumentException::class); + new IpString('999.999.999.999'); + } + + public function testMacString(): void + { + $s = new MacString('00:1A:2B:3C:4D:5E'); + $this->assertSame('00:1A:2B:3C:4D:5E', (string)$s); + $this->expectException(InvalidArgumentException::class); + new MacString('00:1A:2B:3C:4D'); + } + + public function testColorString(): void + { + $s = new ColorString('#fff'); + $this->assertSame('#fff', (string)$s); + $s2 = new ColorString('rgb(255,255,255)'); + $this->assertSame('rgb(255,255,255)', (string)$s2); + $this->expectException(InvalidArgumentException::class); + new ColorString('notacolor'); + } +} \ No newline at end of file diff --git a/Tests/Composite/String/Str16Test.php b/Tests/Composite/String/Str16Test.php new file mode 100644 index 0000000..c93904d --- /dev/null +++ b/Tests/Composite/String/Str16Test.php @@ -0,0 +1,32 @@ +assertEquals('deadbeefdeadbeef', $str->getValue()); + } + + public function testInvalidLength(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str16 must be exactly 16 characters long'); + new Str16('deadbeefdeadbee'); + } + + public function testInvalidHex(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str16 must be a valid hex string'); + new Str16('deadbeefdeadbeeg'); + } +} \ No newline at end of file diff --git a/Tests/Composite/String/Str32Test.php b/Tests/Composite/String/Str32Test.php new file mode 100644 index 0000000..a3675aa --- /dev/null +++ b/Tests/Composite/String/Str32Test.php @@ -0,0 +1,32 @@ +assertEquals('deadbeefdeadbeefdeadbeefdeadbeef', $str->getValue()); + } + + public function testInvalidLength(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str32 must be exactly 32 characters long'); + new Str32('deadbeefdeadbeefdeadbeefdeadbee'); + } + + public function testInvalidHex(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str32 must be a valid hex string'); + new Str32('deadbeefdeadbeefdeadbeefdeadbeeg'); + } +} \ No newline at end of file diff --git a/Tests/Composite/String/Str8Test.php b/Tests/Composite/String/Str8Test.php new file mode 100644 index 0000000..97987a0 --- /dev/null +++ b/Tests/Composite/String/Str8Test.php @@ -0,0 +1,32 @@ +assertEquals('deadbeef', $str->getValue()); + } + + public function testInvalidLength(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str8 must be exactly 8 characters long'); + new Str8('deadbee'); + } + + public function testInvalidHex(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Str8 must be a valid hex string'); + new Str8('deadbeeg'); + } +} \ No newline at end of file diff --git a/Tests/Composite/Struct/ImmutableStructTest.php b/Tests/Composite/Struct/ImmutableStructTest.php new file mode 100644 index 0000000..9452363 --- /dev/null +++ b/Tests/Composite/Struct/ImmutableStructTest.php @@ -0,0 +1,727 @@ + ['type' => 'string'], + 'age' => ['type' => 'int'] + ]); + + $this->assertNull($struct->get('name')); + $this->assertNull($struct->get('age')); + } + + public function testStructWithInitialValues(): void + { + $struct = new ImmutableStruct( + [ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'] + ], + [ + 'name' => 'John', + 'age' => 30 + ] + ); + + $this->assertEquals('John', $struct->get('name')); + $this->assertEquals(30, $struct->get('age')); + } + + public function testRequiredFields(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Required field 'name' has no value"); + + new ImmutableStruct( + [ + 'name' => ['type' => 'string', 'required' => true], + 'age' => ['type' => 'int'] + ] + ); + } + + public function testDefaultValues(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string', 'default' => 'Unknown'], + 'age' => ['type' => 'int', 'default' => 0] + ]); + + $this->assertEquals('Unknown', $struct->get('name')); + $this->assertEquals(0, $struct->get('age')); + } + + public function testInvalidFieldAccess(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'] + ]); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Field 'age' does not exist in the struct"); + + $struct->get('age'); + } + + public function testImmutableModification(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'] + ]); + + $this->expectException(ImmutableException::class); + $this->expectExceptionMessage("Cannot modify a frozen struct"); + + $struct->set('name', 'John'); + } + + public function testWithMethod(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'] + ]); + + $newStruct = $struct->with(['name' => 'John', 'age' => 30]); + + $this->assertNull($struct->get('name')); + $this->assertNull($struct->get('age')); + $this->assertEquals('John', $newStruct->get('name')); + $this->assertEquals(30, $newStruct->get('age')); + } + + public function testWithFieldMethod(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'] + ]); + + $newStruct = $struct->withField('name', 'John'); + + $this->assertNull($struct->get('name')); + $this->assertEquals('John', $newStruct->get('name')); + } + + public function testNestedStructs(): void + { + $address = new ImmutableStruct([ + 'street' => ['type' => 'string'], + 'city' => ['type' => 'string'] + ], [ + 'street' => '123 Main St', + 'city' => 'Boston' + ]); + + $person = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'address' => ['type' => ImmutableStruct::class] + ], [ + 'name' => 'John', + 'address' => $address + ]); + + $this->assertEquals('John', $person->get('name')); + $this->assertInstanceOf(ImmutableStruct::class, $person->get('address')); + $this->assertEquals('123 Main St', $person->get('address')->get('street')); + $this->assertEquals('Boston', $person->get('address')->get('city')); + } + + public function testNullableFields(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => '?string'], + 'age' => ['type' => '?int'] + ]); + + $this->assertNull($struct->get('name')); + $this->assertNull($struct->get('age')); + + $newStruct = $struct->with([ + 'name' => null, + 'age' => null + ]); + + $this->assertNull($newStruct->get('name')); + $this->assertNull($newStruct->get('age')); + } + + public function testToArray(): void + { + $address = new ImmutableStruct([ + 'street' => ['type' => 'string'], + 'city' => ['type' => 'string'] + ], [ + 'street' => '123 Main St', + 'city' => 'Boston' + ]); + + $person = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'], + 'address' => ['type' => ImmutableStruct::class] + ], [ + 'name' => 'John', + 'age' => 30, + 'address' => $address + ]); + + $expected = [ + 'name' => 'John', + 'age' => 30, + 'address' => [ + 'street' => '123 Main St', + 'city' => 'Boston' + ] + ]; + + $this->assertEquals($expected, $person->toArray()); + } + + public function testToString(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'] + ], [ + 'name' => 'John', + 'age' => 30 + ]); + + $expected = json_encode([ + 'name' => 'John', + 'age' => 30 + ]); + + $this->assertEquals($expected, (string)$struct); + } + + public function testGetFieldType(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string'], + 'age' => ['type' => 'int'] + ]); + + $this->assertEquals('string', $struct->getFieldType('name')); + $this->assertEquals('int', $struct->getFieldType('age')); + + $this->expectException(InvalidArgumentException::class); + $struct->getFieldType('invalid'); + } + + public function testIsFieldRequired(): void + { + $struct = new ImmutableStruct([ + 'name' => ['type' => 'string', 'required' => true, 'default' => 'John'], + 'age' => ['type' => 'int', 'required' => false] + ]); + + $this->assertTrue($struct->isFieldRequired('name')); + $this->assertFalse($struct->isFieldRequired('age')); + + $this->expectException(InvalidArgumentException::class); + $struct->isFieldRequired('invalid'); + } + + public function testGetFieldRules(): void + { + $struct = new ImmutableStruct([ + 'name' => [ + 'type' => 'string', + 'rules' => [ + new MinLengthRule(3) + ] + ] + ]); + + $rules = $struct->getFieldRules('name'); + $this->assertCount(1, $rules); + $this->assertInstanceOf(MinLengthRule::class, $rules[0]); + + $this->expectException(InvalidArgumentException::class); + $struct->getFieldRules('invalid'); + } + + public function testMinLengthRule(): void + { + $struct = new ImmutableStruct([ + 'name' => [ + 'type' => 'string', + 'rules' => [ + new MinLengthRule(3) + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['name' => 'John']); + $this->assertEquals('John', $newStruct->get('name')); + + // Invalid value + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'name' must be at least 3 characters long"); + $struct->with(['name' => 'Jo']); + } + + public function testRangeRule(): void + { + $struct = new ImmutableStruct([ + 'age' => [ + 'type' => 'int', + 'rules' => [ + new RangeRule(0, 120) + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['age' => 30]); + $this->assertEquals(30, $newStruct->get('age')); + + // Invalid value - too low + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'age' must be between 0 and 120"); + $struct->with(['age' => -1]); + + // Invalid value - too high + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'age' must be between 0 and 120"); + $struct->with(['age' => 121]); + } + + public function testMultipleRules(): void + { + $struct = new ImmutableStruct([ + 'name' => [ + 'type' => 'string', + 'rules' => [ + new MinLengthRule(3), + new MinLengthRule(5) + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['name' => 'Johnny']); + $this->assertEquals('Johnny', $newStruct->get('name')); + + // Invalid value - fails first rule + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'name' must be at least 3 characters long"); + $struct->with(['name' => 'Jo']); + + // Invalid value - fails second rule + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'name' must be at least 5 characters long"); + $struct->with(['name' => 'John']); + } + + public function testPatternRule(): void + { + $struct = new ImmutableStruct([ + 'username' => [ + 'type' => 'string', + 'rules' => [ + new PatternRule('/^[a-zA-Z0-9_]{3,20}$/') + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['username' => 'john_doe123']); + $this->assertEquals('john_doe123', $newStruct->get('username')); + + // Invalid value - contains invalid characters + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'username' does not match the required pattern"); + $struct->with(['username' => 'john@doe']); + } + + public function testEmailRule(): void + { + $struct = new ImmutableStruct([ + 'email' => [ + 'type' => 'string', + 'rules' => [ + new EmailRule() + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['email' => 'john.doe@example.com']); + $this->assertEquals('john.doe@example.com', $newStruct->get('email')); + + // Invalid value - not an email + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'email' must be a valid email address"); + $struct->with(['email' => 'not-an-email']); + } + + public function testCustomRule(): void + { + $struct = new ImmutableStruct([ + 'password' => [ + 'type' => 'string', + 'rules' => [ + new CustomRule( + fn ($value) => strlen($value) >= 8 && preg_match('/[A-Z]/', $value) && preg_match('/[a-z]/', $value) && preg_match('/[0-9]/', $value), + 'must be at least 8 characters long and contain uppercase, lowercase, and numbers' + ) + ] + ] + ]); + + // Valid value + $newStruct = $struct->with(['password' => 'Password123']); + $this->assertEquals('Password123', $newStruct->get('password')); + + // Invalid value - too short + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password': must be at least 8 characters long and contain uppercase, lowercase, and numbers"); + $struct->with(['password' => 'pass']); + + // Invalid value - missing uppercase + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password': must be at least 8 characters long and contain uppercase, lowercase, and numbers"); + $struct->with(['password' => 'password123']); + + // Invalid value - missing numbers + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password': must be at least 8 characters long and contain uppercase, lowercase, and numbers"); + $struct->with(['password' => 'Password']); + } + + public function testCombinedRules(): void + { + $struct = new ImmutableStruct([ + 'username' => [ + 'type' => 'string', + 'rules' => [ + new MinLengthRule(3), + new PatternRule('/^[a-zA-Z0-9_]+$/') + ] + ], + 'email' => [ + 'type' => 'string', + 'rules' => [ + new EmailRule(), + new CustomRule( + fn ($value) => str_ends_with($value, '.com'), + 'must be a .com email address' + ) + ] + ] + ]); + + // Valid values + $newStruct = $struct->with([ + 'username' => 'john_doe', + 'email' => 'john.doe@example.com' + ]); + $this->assertEquals('john_doe', $newStruct->get('username')); + $this->assertEquals('john.doe@example.com', $newStruct->get('email')); + + // Invalid username - too short + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'username' must be at least 3 characters long"); + $struct->with(['username' => 'jo']); + + // Invalid username - invalid characters + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'username' does not match the required pattern"); + $struct->with(['username' => 'john@doe']); + + // Invalid email - not .com + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'email': must be a .com email address"); + $struct->with(['email' => 'john.doe@example.org']); + } + + public function testUrlRule(): void + { + $struct = new ImmutableStruct([ + 'website' => [ + 'type' => 'string', + 'rules' => [ + new UrlRule() + ] + ], + 'secureWebsite' => [ + 'type' => 'string', + 'rules' => [ + new UrlRule(true) + ] + ] + ]); + + // Valid URLs + $newStruct = $struct->with([ + 'website' => 'http://example.com', + 'secureWebsite' => 'https://example.com' + ]); + $this->assertEquals('http://example.com', $newStruct->get('website')); + $this->assertEquals('https://example.com', $newStruct->get('secureWebsite')); + + // Invalid URL + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'website' must be a valid URL"); + $struct->with(['website' => 'not-a-url']); + + // Non-HTTPS URL for secure field + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'secureWebsite' must be a secure HTTPS URL"); + $struct->with(['secureWebsite' => 'http://example.com']); + } + + public function testSlugRule(): void + { + $struct = new ImmutableStruct([ + 'slug' => [ + 'type' => 'string', + 'rules' => [ + new SlugRule(3, 50, true) + ] + ], + 'strictSlug' => [ + 'type' => 'string', + 'rules' => [ + new SlugRule(3, 50, false) + ] + ] + ]); + + // Valid slugs + $newStruct = $struct->with([ + 'slug' => 'my-awesome-post_123', + 'strictSlug' => 'my-awesome-post' + ]); + $this->assertEquals('my-awesome-post_123', $newStruct->get('slug')); + $this->assertEquals('my-awesome-post', $newStruct->get('strictSlug')); + + // Too short + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'slug' must be at least 3 characters long"); + $struct->with(['slug' => 'ab']); + + // Too long + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'slug' must not exceed 50 characters"); + $struct->with(['slug' => str_repeat('a', 51)]); + + // Invalid characters + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'slug' must contain only lowercase letters, numbers, hyphens, and underscores"); + $struct->with(['slug' => 'My-Post']); + + // Consecutive hyphens + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'slug' must not contain consecutive hyphens or underscores"); + $struct->with(['slug' => 'my--post']); + + // Underscores not allowed in strict mode + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'strictSlug' must contain only lowercase letters, numbers, and hyphens"); + $struct->with(['strictSlug' => 'my_post']); + } + + public function testPasswordRule(): void + { + $struct = new ImmutableStruct([ + 'password' => [ + 'type' => 'string', + 'rules' => [ + new PasswordRule( + minLength: 8, + requireUppercase: true, + requireLowercase: true, + requireNumbers: true, + requireSpecialChars: true, + maxLength: 100 + ) + ] + ], + 'simplePassword' => [ + 'type' => 'string', + 'rules' => [ + new PasswordRule( + minLength: 6, + requireUppercase: false, + requireLowercase: true, + requireNumbers: true, + requireSpecialChars: false + ) + ] + ] + ]); + + // Valid passwords + $newStruct = $struct->with([ + 'password' => 'Password123!', + 'simplePassword' => 'pass123' + ]); + $this->assertEquals('Password123!', $newStruct->get('password')); + $this->assertEquals('pass123', $newStruct->get('simplePassword')); + + // Too short + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must be at least 8 characters long"); + $struct->with(['password' => 'Pass1!']); + + // Too long + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must not exceed 100 characters"); + $struct->with(['password' => str_repeat('a', 101)]); + + // Missing uppercase + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must contain at least one uppercase letter"); + $struct->with(['password' => 'password123!']); + + // Missing lowercase + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must contain at least one lowercase letter"); + $struct->with(['password' => 'PASSWORD123!']); + + // Missing numbers + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must contain at least one number"); + $struct->with(['password' => 'Password!']); + + // Missing special characters + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must contain at least one special character"); + $struct->with(['password' => 'Password123']); + + // Simple password - valid + $newStruct = $struct->with(['simplePassword' => 'pass123']); + $this->assertEquals('pass123', $newStruct->get('simplePassword')); + + // Simple password - invalid (missing numbers) + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'simplePassword' must contain at least one number"); + $struct->with(['simplePassword' => 'password']); + } + + public function testCompositeRule(): void + { + $struct = new ImmutableStruct([ + 'username' => [ + 'type' => 'string', + 'rules' => [ + CompositeRule::fromArray([ + new MinLengthRule(3), + new PatternRule('/^[a-zA-Z0-9_]+$/') + ]) + ] + ], + 'password' => [ + 'type' => 'string', + 'rules' => [ + new PasswordRule( + minLength: 8, + requireUppercase: true, + requireLowercase: true, + requireNumbers: true, + requireSpecialChars: true + ) + ] + ] + ]); + + // Valid values + $newStruct = $struct->with([ + 'username' => 'john_doe', + 'password' => 'Password123!' + ]); + $this->assertEquals('john_doe', $newStruct->get('username')); + $this->assertEquals('Password123!', $newStruct->get('password')); + + // Invalid username - too short + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'username' must be at least 3 characters long"); + $struct->with(['username' => 'jo']); + + // Invalid username - invalid characters + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'username' does not match the required pattern"); + $struct->with(['username' => 'john@doe']); + + // Invalid password - missing special character + $this->expectException(ValidationException::class); + $this->expectExceptionMessage("Field 'password' must contain at least one special character"); + $struct->with(['password' => 'Password123']); + } + + public function testStructInheritance(): void + { + // Create a parent struct + $parentStruct = new ImmutableStruct( + ['name' => ['type' => 'string'], 'age' => ['type' => 'int']], + [ + 'name' => 'Parent', + 'age' => 30 + ] + ); + + // Create a child struct that inherits from the parent + $childStruct = new ImmutableStruct( + [ + 'name' => ['type' => 'string', 'rules' => [new MinLengthRule(1)]], + 'age' => ['type' => 'int', 'rules' => [new RangeRule(0, 150)]], + 'grade' => ['type' => 'string', 'rules' => [new MinLengthRule(1)]] + ], + [ + 'name' => 'Child', + 'age' => 10, + 'grade' => 'A' + ], + $parentStruct + ); + + // Verify that the child struct has a parent + $this->assertTrue($childStruct->hasParent()); + $this->assertSame($parentStruct, $childStruct->getParent()); + + // Verify that the child struct inherits fields from the parent + $allFields = $childStruct->getAllFields(); + $this->assertArrayHasKey('name', $allFields); + $this->assertArrayHasKey('age', $allFields); + $this->assertArrayHasKey('grade', $allFields); + $this->assertEquals('Child', $allFields['name']); + $this->assertEquals(10, $allFields['age']); + $this->assertEquals('A', $allFields['grade']); + + // Verify that the child struct inherits validation rules from the parent + $allRules = $childStruct->getAllRules(); + $this->assertArrayHasKey('name', $allRules); + $this->assertArrayHasKey('age', $allRules); + $this->assertArrayHasKey('grade', $allRules); + $this->assertCount(1, $allRules['name']); + $this->assertCount(1, $allRules['age']); + $this->assertCount(1, $allRules['grade']); + } +} diff --git a/Tests/Composite/Struct/StructTest.php b/Tests/Composite/Struct/StructTest.php new file mode 100644 index 0000000..14f9a83 --- /dev/null +++ b/Tests/Composite/Struct/StructTest.php @@ -0,0 +1,127 @@ + ['type' => 'int', 'default' => 0], + 'name' => ['type' => 'string', 'nullable' => false], + 'email' => ['type' => 'string', 'nullable' => true], + ]; + $struct = new Struct($schema, ['name' => 'Alice']); + $this->assertEquals(0, $struct->get('id')); + $this->assertEquals('Alice', $struct->get('name')); + $this->assertNull($struct->get('email')); + } + + public function testRequiredFieldMissing(): void + { + $schema = [ + 'name' => ['type' => 'string', 'nullable' => false], + ]; + $this->expectException(InvalidArgumentException::class); + new Struct($schema, []); + } + + public function testFieldValidation(): void + { + $schema = [ + 'email' => [ + 'type' => 'string', + 'rules' => [fn($v) => filter_var($v, FILTER_VALIDATE_EMAIL)], + ], + ]; + $this->expectException(ValidationException::class); + new Struct($schema, ['email' => 'invalid-email']); + } + + public function testNestedStruct(): void + { + $schema = [ + 'profile' => ['type' => Struct::class, 'nullable' => true], + ]; + $nestedSchema = [ + 'name' => ['type' => 'string'], + ]; + $nestedStruct = new Struct($nestedSchema, ['name' => 'Bob']); + $struct = new Struct($schema, ['profile' => $nestedStruct]); + $this->assertInstanceOf(Struct::class, $struct->get('profile')); + $this->assertEquals('Bob', $struct->get('profile')->get('name')); + } + + public function testToArray(): void + { + $schema = [ + 'id' => ['type' => 'int', 'default' => 0], + 'name' => ['type' => 'string', 'alias' => 'userName'], + ]; + $struct = new Struct($schema, ['name' => 'Alice']); + $arr = $struct->toArray(true); + $this->assertEquals(['id' => 0, 'userName' => 'Alice'], $arr); + } + + public function testFromArray(): void + { + $schema = [ + 'id' => ['type' => 'int'], + 'name' => ['type' => 'string'], + ]; + $struct = Struct::fromArray($schema, ['id' => 1, 'name' => 'Alice']); + $this->assertEquals(1, $struct->get('id')); + $this->assertEquals('Alice', $struct->get('name')); + } + + public function testToJson(): void + { + $schema = [ + 'id' => ['type' => 'int', 'default' => 0], + 'name' => ['type' => 'string', 'alias' => 'userName'], + ]; + $struct = new Struct($schema, ['name' => 'Alice']); + $json = $struct->toJson(true); + $this->assertEquals('{"id":0,"userName":"Alice"}', $json); + } + + public function testFromJson(): void + { + $schema = [ + 'id' => ['type' => 'int'], + 'name' => ['type' => 'string'], + ]; + $struct = Struct::fromJson($schema, '{"id":1,"name":"Alice"}'); + $this->assertEquals(1, $struct->get('id')); + $this->assertEquals('Alice', $struct->get('name')); + } + + public function testToXml(): void + { + $schema = [ + 'id' => ['type' => 'int', 'default' => 0], + 'name' => ['type' => 'string', 'alias' => 'userName'], + ]; + $struct = new Struct($schema, ['name' => 'Alice']); + $xml = $struct->toXml(true); + $this->assertStringContainsString('Alice', $xml); + } + + public function testFromXml(): void + { + $schema = [ + 'id' => ['type' => 'int'], + 'name' => ['type' => 'string'], + ]; + $struct = Struct::fromXml($schema, '1Alice'); + $this->assertEquals(1, $struct->get('id')); + $this->assertEquals('Alice', $struct->get('name')); + } +} \ No newline at end of file diff --git a/Tests/Composite/Union/UnionTypeTest.php b/Tests/Composite/Union/UnionTypeTest.php new file mode 100644 index 0000000..5c43fc2 --- /dev/null +++ b/Tests/Composite/Union/UnionTypeTest.php @@ -0,0 +1,443 @@ + 'string', + 'int' => 'int' + ]); + + $this->assertEquals(['string', 'int'], $union->getTypes()); + $this->expectException(InvalidArgumentException::class); + $union->getActiveType(); + } + + public function testEmptyUnionType(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Union type must have at least one possible type'); + + new UnionType([]); + } + + public function testSetAndGetValue(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->setValue('string', 'world'); + $this->assertEquals('string', $union->getActiveType()); + $this->assertEquals('world', $union->getValue()); + + $union->setValue('int', 100); + $this->assertEquals('int', $union->getActiveType()); + $this->assertEquals(100, $union->getValue()); + } + + public function testInvalidType(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Type key 'float' is not valid in this union"); + + $union->setValue('float', 3.14); + } + + public function testGetValueWithoutActiveType(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $this->expectException(TypeMismatchException::class); + $this->expectExceptionMessage('No type is currently active'); + + $union->getValue(); + } + + public function testIsType(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $this->assertFalse($union->isType('string')); + $this->assertFalse($union->isType('int')); + + $union->setValue('string', 'world'); + $this->assertTrue($union->isType('string')); + $this->assertFalse($union->isType('int')); + } + + public function testPatternMatching(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->setValue('string', 'world'); + + $result = $union->match([ + 'string' => fn($value) => "String: $value", + 'int' => fn($value) => "Integer: $value" + ]); + + $this->assertEquals('String: world', $result); + } + + public function testPatternMatchingWithDefault(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->setValue('string', 'world'); + + $result = $union->matchWithDefault( + [ + 'int' => fn($value) => "Integer: $value" + ], + fn() => 'Default case' + ); + + $this->assertEquals('Default case', $result); + } + + public function testPatternMatchingWithoutMatch(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->setValue('string', 'world'); + + $this->expectException(TypeMismatchException::class); + $this->expectExceptionMessage("No pattern defined for type 'string'"); + + $union->match([ + 'int' => fn($value) => "Integer: $value" + ]); + } + + public function testToString(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $this->assertEquals('UnionType', (string)$union); + + $union->setValue('string', 'world'); + $this->assertEquals('UnionType', (string)$union); + } + + public function testComplexPatternMatching(): void + { + $union = new UnionType([ + 'success' => 'array', + 'error' => 'array', + 'loading' => 'null' + ]); + + $union->setValue('success', ['data' => 'operation completed']); + + $result = $union->match([ + 'success' => fn($value) => "Success: {$value['data']}", + 'error' => fn($value) => "Error: {$value['message']}", + 'loading' => fn() => 'Loading...' + ]); + + $this->assertEquals('Success: operation completed', $result); + } + + public function testAddType(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->addType('float', 'float', 3.14); + $this->assertContains('float', $union->getTypes()); + + $union->setValue('float', 2.718); + $this->assertEquals('float', $union->getActiveType()); + $this->assertEquals(2.718, $union->getValue()); + } + + public function testAddExistingType(): void + { + $union = new UnionType([ + 'string' => 'string' + ]); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Type key 'string' already exists in this union"); + + $union->addType('string', 'string', 'world'); + } + + public function testTypeValidation(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid type for key 'string': expected 'string', got 'integer'"); + + $union->setValue('string', 123); + } + + public function testClassInstanceType(): void + { + class_exists('DateTime') || class_alias(\DateTime::class, 'DateTime'); + + $union = new UnionType([ + 'DateTime' => 'DateTime' + ]); + + $union->setValue('DateTime', new \DateTime()); + $this->assertTrue($union->isType('DateTime')); + } + + public function testTypeMapping(): void + { + $union = new UnionType([ + 'int' => 'int', + 'float' => 'float', + 'bool' => 'bool' + ]); + + $union->setValue('int', 100); + $this->assertTrue($union->isType('int')); + + $union->setValue('float', 2.718); + $this->assertTrue($union->isType('float')); + + $union->setValue('bool', false); + $this->assertTrue($union->isType('bool')); + } + + public function testComplexTypeValidation(): void + { + $union = new UnionType([ + 'array' => 'array', + 'object' => 'object' + ]); + + $union->setValue('array', ['a', 'b', 'c']); + $this->assertTrue($union->isType('array')); + + $union->setValue('object', new \stdClass()); + $this->assertTrue($union->isType('object')); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid type for key 'array': expected 'array', got 'string'"); + $union->setValue('array', 'not an array'); + } + + public function testNullValueHandling(): void + { + $union = new UnionType([ + 'string' => 'string', + 'int' => 'int' + ]); + + $union->setValue('string', null); + $this->assertTrue($union->isType('string')); + $this->assertNull($union->getValue()); + + $union->setValue('int', null); + $this->assertTrue($union->isType('int')); + $this->assertNull($union->getValue()); + } + + public function testGetActiveType(): void + { + $union = new UnionType(['int' => 'int', 'string' => 'string']); + $union->setValue('int', 42); + $this->assertSame('int', $union->getActiveType()); + + $union = new UnionType(['int' => 'int', 'string' => 'string']); + $this->expectException(InvalidArgumentException::class); + $union->getActiveType(); + } + + public function testSafeTypeCasting(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $union->setValue('string', 'hello'); + $this->assertSame('hello', $union->castTo('string')); + $this->expectException(TypeMismatchException::class); + $union->castTo('int'); + } + + public function testSafeTypeCastingNoActiveType(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $this->expectException(TypeMismatchException::class); + $union->castTo('string'); + } + + public function testEquals(): void + { + $union1 = new UnionType(['string' => 'string', 'int' => 'int']); + $union2 = new UnionType(['string' => 'string', 'int' => 'int']); + $union3 = new UnionType(['string' => 'string', 'int' => 'int']); + + $union1->setValue('string', 'hello'); + $union2->setValue('string', 'hello'); + $union3->setValue('int', 100); + + $this->assertTrue($union1->equals($union2)); + $this->assertFalse($union1->equals($union3)); + } + + public function testEqualsNoActiveType(): void + { + $union1 = new UnionType(['string' => 'string', 'int' => 'int']); + $union2 = new UnionType(['string' => 'string', 'int' => 'int']); + $this->assertFalse($union1->equals($union2)); + } + + public function testJsonSerialization(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $union->setValue('string', 'hello'); + $json = $union->toJson(); + $this->assertJson($json); + $this->assertStringContainsString('"activeType":"string"', $json); + $this->assertStringContainsString('"value":"hello"', $json); + + $reconstructed = UnionType::fromJson($json); + $this->assertTrue($union->equals($reconstructed)); + } + + public function testJsonDeserializationInvalidFormat(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid JSON format for UnionType'); + UnionType::fromJson('{"invalid": "format"}'); + } + + public function testXmlSerialization(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $union->setValue('string', 'hello'); + $xml = $union->toXml(); + $this->assertStringContainsString('assertStringContainsString('activeType="string"', $xml); + $this->assertStringContainsString('hello', $xml); + + $reconstructed = UnionType::fromXml($xml); + $this->assertTrue($union->equals($reconstructed)); + } + + public function testXmlDeserializationInvalidFormat(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid XML format for UnionType'); + UnionType::fromXml('format'); + } + + public function testValidateXmlSchemaValid(): void + { + $xml = 'hello'; + $xsd = ' + + + + + + + + + + '; + $this->assertTrue(UnionType::validateXmlSchema($xml, $xsd)); + } + + public function testValidateXmlSchemaInvalid(): void + { + $xml = 'hello'; + $xsd = ' + + + + + + + + + + '; + $this->expectException(InvalidArgumentException::class); + UnionType::validateXmlSchema($xml, $xsd); + } + + public function testXmlNamespaceSerialization(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $union->setValue('string', 'hello'); + $namespace = 'http://example.com/union'; + $prefix = 'u'; + $xml = $union->toXml($namespace, $prefix); + $this->assertStringContainsString('xmlns:u="http://example.com/union"', $xml); + $this->assertStringContainsString('assertStringContainsString('hello', $xml); + $reconstructed = UnionType::fromXml($xml); + $this->assertTrue($union->equals($reconstructed)); + } + + public function testXmlNamespaceDeserialization(): void + { + $xml = ' + + hello + '; + $union = UnionType::fromXml($xml); + $this->assertEquals('string', $union->getActiveType()); + $this->assertEquals('hello', $union->getValue()); + } + + public function testBinarySerialization(): void + { + $union = new UnionType(['string' => 'string', 'int' => 'int']); + $union->setValue('string', 'hello'); + $binary = $union->toBinary(); + $reconstructed = UnionType::fromBinary($binary); + $this->assertTrue($union->equals($reconstructed)); + } + + public function testBinaryDeserializationInvalidFormat(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid binary format for UnionType'); + UnionType::fromBinary('invalid binary data'); + } +} \ No newline at end of file diff --git a/Tests/Composite/Vector/Vec2Test.php b/Tests/Composite/Vector/Vec2Test.php new file mode 100644 index 0000000..7d4d198 --- /dev/null +++ b/Tests/Composite/Vector/Vec2Test.php @@ -0,0 +1,137 @@ +assertEquals(1.0, $vec->getX()); + $this->assertEquals(2.0, $vec->getY()); + } + + public function testCreateInvalidComponentCount(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec2([1.0]); + } + + public function testCreateWithNonNumericComponents(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec2(['a', 'b']); + } + + public function testMagnitude(): void + { + $vec = new Vec2([3.0, 4.0]); + $this->assertEquals(5.0, $vec->magnitude()); + } + + public function testNormalize(): void + { + $vec = new Vec2([3.0, 4.0]); + $normalized = $vec->normalize(); + $this->assertEquals(1.0, $normalized->magnitude()); + $this->assertEquals(0.6, $normalized->getX()); + $this->assertEquals(0.8, $normalized->getY()); + } + + public function testNormalizeZeroVector(): void + { + $vec = new Vec2([0.0, 0.0]); + $this->expectException(InvalidArgumentException::class); + $vec->normalize(); + } + + public function testDotProduct(): void + { + $vec1 = new Vec2([1.0, 2.0]); + $vec2 = new Vec2([3.0, 4.0]); + $this->assertEquals(11.0, $vec1->dot($vec2)); + } + + public function testAdd(): void + { + $vec1 = new Vec2([1.0, 2.0]); + $vec2 = new Vec2([3.0, 4.0]); + $result = $vec1->add($vec2); + $this->assertEquals(4.0, $result->getX()); + $this->assertEquals(6.0, $result->getY()); + } + + public function testSubtract(): void + { + $vec1 = new Vec2([3.0, 4.0]); + $vec2 = new Vec2([1.0, 2.0]); + $result = $vec1->subtract($vec2); + $this->assertEquals(2.0, $result->getX()); + $this->assertEquals(2.0, $result->getY()); + } + + public function testScale(): void + { + $vec = new Vec2([1.0, 2.0]); + $result = $vec->scale(2.0); + $this->assertEquals(2.0, $result->getX()); + $this->assertEquals(4.0, $result->getY()); + } + + public function testCross(): void + { + $vec1 = new Vec2([1.0, 2.0]); + $vec2 = new Vec2([3.0, 4.0]); + $this->assertEquals(-2.0, $vec1->cross($vec2)); + } + + public function testZero(): void + { + $vec = Vec2::zero(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + } + + public function testUnitX(): void + { + $vec = Vec2::unitX(); + $this->assertEquals(1.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + } + + public function testUnitY(): void + { + $vec = Vec2::unitY(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(1.0, $vec->getY()); + } + + public function testToString(): void + { + $vec = new Vec2([1.0, 2.0]); + $this->assertEquals('(1, 2)', (string)$vec); + } + + public function testEquals(): void + { + $vec1 = new Vec2([1.0, 2.0]); + $vec2 = new Vec2([1.0, 2.0]); + $vec3 = new Vec2([2.0, 1.0]); + + $this->assertTrue($vec1->equals($vec2)); + $this->assertFalse($vec1->equals($vec3)); + } + + public function testDistance(): void + { + $vec1 = new Vec2([0.0, 0.0]); + $vec2 = new Vec2([3.0, 4.0]); + $this->assertEquals(5.0, $vec1->distance($vec2)); + } +} diff --git a/Tests/Composite/Vector/Vec3Test.php b/Tests/Composite/Vector/Vec3Test.php new file mode 100644 index 0000000..04e37e6 --- /dev/null +++ b/Tests/Composite/Vector/Vec3Test.php @@ -0,0 +1,156 @@ +assertEquals(1.0, $vec->getX()); + $this->assertEquals(2.0, $vec->getY()); + $this->assertEquals(3.0, $vec->getZ()); + } + + public function testCreateInvalidComponentCount(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec3([1.0, 2.0]); + } + + public function testCreateWithNonNumericComponents(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec3(['a', 'b', 'c']); + } + + public function testMagnitude(): void + { + $vec = new Vec3([1.0, 2.0, 2.0]); + $this->assertEquals(3.0, $vec->magnitude()); + } + + public function testNormalize(): void + { + $vec = new Vec3([1.0, 2.0, 2.0]); + $normalized = $vec->normalize(); + $this->assertEquals(1.0, $normalized->magnitude()); + $this->assertEquals(1 / 3, $normalized->getX()); + $this->assertEquals(2 / 3, $normalized->getY()); + $this->assertEquals(2 / 3, $normalized->getZ()); + } + + public function testNormalizeZeroVector(): void + { + $vec = new Vec3([0.0, 0.0, 0.0]); + $this->expectException(InvalidArgumentException::class); + $vec->normalize(); + } + + public function testDotProduct(): void + { + $vec1 = new Vec3([1.0, 2.0, 3.0]); + $vec2 = new Vec3([4.0, 5.0, 6.0]); + $this->assertEquals(32.0, $vec1->dot($vec2)); + } + + public function testAdd(): void + { + $vec1 = new Vec3([1.0, 2.0, 3.0]); + $vec2 = new Vec3([4.0, 5.0, 6.0]); + $result = $vec1->add($vec2); + $this->assertEquals(5.0, $result->getX()); + $this->assertEquals(7.0, $result->getY()); + $this->assertEquals(9.0, $result->getZ()); + } + + public function testSubtract(): void + { + $vec1 = new Vec3([4.0, 5.0, 6.0]); + $vec2 = new Vec3([1.0, 2.0, 3.0]); + $result = $vec1->subtract($vec2); + $this->assertEquals(3.0, $result->getX()); + $this->assertEquals(3.0, $result->getY()); + $this->assertEquals(3.0, $result->getZ()); + } + + public function testScale(): void + { + $vec = new Vec3([1.0, 2.0, 3.0]); + $result = $vec->scale(2.0); + $this->assertEquals(2.0, $result->getX()); + $this->assertEquals(4.0, $result->getY()); + $this->assertEquals(6.0, $result->getZ()); + } + + public function testCross(): void + { + $vec1 = new Vec3([1.0, 0.0, 0.0]); + $vec2 = new Vec3([0.0, 1.0, 0.0]); + $result = $vec1->cross($vec2); + $this->assertEquals(0.0, $result->getX()); + $this->assertEquals(0.0, $result->getY()); + $this->assertEquals(1.0, $result->getZ()); + } + + public function testZero(): void + { + $vec = Vec3::zero(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + } + + public function testUnitX(): void + { + $vec = Vec3::unitX(); + $this->assertEquals(1.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + } + + public function testUnitY(): void + { + $vec = Vec3::unitY(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(1.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + } + + public function testUnitZ(): void + { + $vec = Vec3::unitZ(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(1.0, $vec->getZ()); + } + + public function testToString(): void + { + $vec = new Vec3([1.0, 2.0, 3.0]); + $this->assertEquals('(1, 2, 3)', (string)$vec); + } + + public function testEquals(): void + { + $vec1 = new Vec3([1.0, 2.0, 3.0]); + $vec2 = new Vec3([1.0, 2.0, 3.0]); + $vec3 = new Vec3([3.0, 2.0, 1.0]); + + $this->assertTrue($vec1->equals($vec2)); + $this->assertFalse($vec1->equals($vec3)); + } + + public function testDistance(): void + { + $vec1 = new Vec3([0.0, 0.0, 0.0]); + $vec2 = new Vec3([1.0, 2.0, 2.0]); + $this->assertEquals(3.0, $vec1->distance($vec2)); + } +} diff --git a/Tests/Composite/Vector/Vec4Test.php b/Tests/Composite/Vector/Vec4Test.php new file mode 100644 index 0000000..e1e8610 --- /dev/null +++ b/Tests/Composite/Vector/Vec4Test.php @@ -0,0 +1,164 @@ +assertEquals(1.0, $vec->getX()); + $this->assertEquals(2.0, $vec->getY()); + $this->assertEquals(3.0, $vec->getZ()); + $this->assertEquals(4.0, $vec->getW()); + } + + public function testCreateInvalidComponentCount(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec4([1.0, 2.0, 3.0]); + } + + public function testCreateWithNonNumericComponents(): void + { + $this->expectException(InvalidArgumentException::class); + new Vec4(['a', 'b', 'c', 'd']); + } + + public function testMagnitude(): void + { + $vec = new Vec4([1.0, 2.0, 2.0, 2.0]); + $this->assertEquals(sqrt(13), $vec->magnitude()); + } + + public function testNormalize(): void + { + $vec = new Vec4([1.0, 2.0, 2.0, 2.0]); + $normalized = $vec->normalize(); + $this->assertEquals(1.0, $normalized->magnitude()); + $this->assertEquals(1 / sqrt(13), $normalized->getX()); + $this->assertEquals(2 / sqrt(13), $normalized->getY()); + $this->assertEquals(2 / sqrt(13), $normalized->getZ()); + $this->assertEquals(2 / sqrt(13), $normalized->getW()); + } + + public function testNormalizeZeroVector(): void + { + $vec = new Vec4([0.0, 0.0, 0.0, 0.0]); + $this->expectException(InvalidArgumentException::class); + $vec->normalize(); + } + + public function testDotProduct(): void + { + $vec1 = new Vec4([1.0, 2.0, 3.0, 4.0]); + $vec2 = new Vec4([5.0, 6.0, 7.0, 8.0]); + $this->assertEquals(70.0, $vec1->dot($vec2)); + } + + public function testAdd(): void + { + $vec1 = new Vec4([1.0, 2.0, 3.0, 4.0]); + $vec2 = new Vec4([5.0, 6.0, 7.0, 8.0]); + $result = $vec1->add($vec2); + $this->assertEquals(6.0, $result->getX()); + $this->assertEquals(8.0, $result->getY()); + $this->assertEquals(10.0, $result->getZ()); + $this->assertEquals(12.0, $result->getW()); + } + + public function testSubtract(): void + { + $vec1 = new Vec4([5.0, 6.0, 7.0, 8.0]); + $vec2 = new Vec4([1.0, 2.0, 3.0, 4.0]); + $result = $vec1->subtract($vec2); + $this->assertEquals(4.0, $result->getX()); + $this->assertEquals(4.0, $result->getY()); + $this->assertEquals(4.0, $result->getZ()); + $this->assertEquals(4.0, $result->getW()); + } + + public function testScale(): void + { + $vec = new Vec4([1.0, 2.0, 3.0, 4.0]); + $result = $vec->scale(2.0); + $this->assertEquals(2.0, $result->getX()); + $this->assertEquals(4.0, $result->getY()); + $this->assertEquals(6.0, $result->getZ()); + $this->assertEquals(8.0, $result->getW()); + } + + public function testZero(): void + { + $vec = Vec4::zero(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + $this->assertEquals(0.0, $vec->getW()); + } + + public function testUnitX(): void + { + $vec = Vec4::unitX(); + $this->assertEquals(1.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + $this->assertEquals(0.0, $vec->getW()); + } + + public function testUnitY(): void + { + $vec = Vec4::unitY(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(1.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + $this->assertEquals(0.0, $vec->getW()); + } + + public function testUnitZ(): void + { + $vec = Vec4::unitZ(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(1.0, $vec->getZ()); + $this->assertEquals(0.0, $vec->getW()); + } + + public function testUnitW(): void + { + $vec = Vec4::unitW(); + $this->assertEquals(0.0, $vec->getX()); + $this->assertEquals(0.0, $vec->getY()); + $this->assertEquals(0.0, $vec->getZ()); + $this->assertEquals(1.0, $vec->getW()); + } + + public function testToString(): void + { + $vec = new Vec4([1.0, 2.0, 3.0, 4.0]); + $this->assertEquals('(1, 2, 3, 4)', (string)$vec); + } + + public function testEquals(): void + { + $vec1 = new Vec4([1.0, 2.0, 3.0, 4.0]); + $vec2 = new Vec4([1.0, 2.0, 3.0, 4.0]); + $vec3 = new Vec4([4.0, 3.0, 2.0, 1.0]); + + $this->assertTrue($vec1->equals($vec2)); + $this->assertFalse($vec1->equals($vec3)); + } + + public function testDistance(): void + { + $vec1 = new Vec4([0.0, 0.0, 0.0, 0.0]); + $vec2 = new Vec4([1.0, 2.0, 2.0, 2.0]); + $this->assertEquals(sqrt(13), $vec1->distance($vec2)); + } +} diff --git a/Tests/DictionaryTest.php b/Tests/DictionaryTest.php index ff41ac2..61b7575 100644 --- a/Tests/DictionaryTest.php +++ b/Tests/DictionaryTest.php @@ -1,4 +1,5 @@ assertSame(1, $arr->get(0)); + $this->assertSame(4, $arr->get(3)); + } + + public function testSetAndGet(): void + { + $arr = new IntArray([1, 2, 3]); + $arr->set(1, 42); + $this->assertSame(42, $arr->get(1)); + } + + public function testCount(): void + { + $arr = new IntArray([1, 2, 3, 4, 5]); + $this->assertCount(5, $arr); + } + + public function testIteration(): void + { + $arr = new IntArray([10, 20, 30]); + $result = []; + foreach ($arr as $value) { + $result[] = $value; + } + $this->assertSame([10, 20, 30], $result); + } + + public function testToArray(): void + { + $arr = new IntArray([7, 8, 9]); + $this->assertSame([7, 8, 9], $arr->toArray()); + } + + public function testInvalidIndexGet(): void + { + $arr = new IntArray([1, 2, 3]); + $this->expectException(\OutOfRangeException::class); + $arr->get(10); + } + + public function testInvalidIndexSet(): void + { + $arr = new IntArray([1, 2, 3]); + $this->expectException(\OutOfRangeException::class); + $arr->set(10, 5); + } + + public function testInvalidValueType(): void + { + $arr = new IntArray([1, 2, 3]); + $this->expectException(\TypeError::class); + $arr->set(0, 'not an int'); + } + + public function testAppend(): void + { + $arr = new IntArray([1, 2]); + $arr->append(3); + $this->assertSame([1, 2, 3], $arr->toArray()); + } + + public function testAppendInvalidType(): void + { + $arr = new IntArray([1, 2, 3]); + $this->expectException(\TypeError::class); + $arr->append('not an int'); + } +} diff --git a/Tests/Integers/Signed/Int16Test.php b/Tests/Integers/Signed/Int16Test.php index 6aefe01..139c72a 100644 --- a/Tests/Integers/Signed/Int16Test.php +++ b/Tests/Integers/Signed/Int16Test.php @@ -4,11 +4,10 @@ namespace Nejcc\PhpDatatypes\Tests\Integers\Signed; - use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16; use PHPUnit\Framework\TestCase; -class Int16Test extends TestCase +final class Int16Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/Integers/Signed/Int32Test.php b/Tests/Integers/Signed/Int32Test.php index 56f533e..61a4e43 100644 --- a/Tests/Integers/Signed/Int32Test.php +++ b/Tests/Integers/Signed/Int32Test.php @@ -7,7 +7,7 @@ use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int32; use PHPUnit\Framework\TestCase; -class Int32Test extends TestCase +final class Int32Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/Integers/Signed/Int8Test.php b/Tests/Integers/Signed/Int8Test.php index 276ae83..3efd1d1 100644 --- a/Tests/Integers/Signed/Int8Test.php +++ b/Tests/Integers/Signed/Int8Test.php @@ -4,11 +4,10 @@ namespace Nejcc\PhpDatatypes\Tests\Integers\Signed; - use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8; use PHPUnit\Framework\TestCase; -class Int8Test extends TestCase +final class Int8Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/Integers/Unsigned/UInt16Test.php b/Tests/Integers/Unsigned/UInt16Test.php index 5fc9fef..f57c36a 100644 --- a/Tests/Integers/Unsigned/UInt16Test.php +++ b/Tests/Integers/Unsigned/UInt16Test.php @@ -7,7 +7,7 @@ use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt16; use PHPUnit\Framework\TestCase; -class UInt16Test extends TestCase +final class UInt16Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/Integers/Unsigned/UInt32Test.php b/Tests/Integers/Unsigned/UInt32Test.php index 875786a..425ebde 100644 --- a/Tests/Integers/Unsigned/UInt32Test.php +++ b/Tests/Integers/Unsigned/UInt32Test.php @@ -7,7 +7,7 @@ use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32; use PHPUnit\Framework\TestCase; -class UInt32Test extends TestCase +final class UInt32Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/Integers/Unsigned/UInt8Test.php b/Tests/Integers/Unsigned/UInt8Test.php index f0ec601..e191c0c 100644 --- a/Tests/Integers/Unsigned/UInt8Test.php +++ b/Tests/Integers/Unsigned/UInt8Test.php @@ -7,7 +7,7 @@ use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8; use PHPUnit\Framework\TestCase; -class UInt8Test extends TestCase +final class UInt8Test extends TestCase { public function testValidInitialization() { diff --git a/Tests/JsonTest.php b/Tests/JsonTest.php new file mode 100644 index 0000000..bad5063 --- /dev/null +++ b/Tests/JsonTest.php @@ -0,0 +1,88 @@ +assertSame('{"a":1,"b":2}', $json->getJson()); + } + + public function testInvalidJsonThrows(): void + { + $this->expectException(InvalidArgumentException::class); + new Json('{invalid json}'); + } + + public function testToArrayAndToObject(): void + { + $json = new Json('{"a":1,"b":2}'); + $this->assertSame(['a' => 1, 'b' => 2], $json->toArray()); + $obj = $json->toObject(); + $this->assertIsObject($obj); + $this->assertEquals(1, $obj->a); + $this->assertEquals(2, $obj->b); + } + + public function testFromArrayAndFromObject(): void + { + $arr = ['x' => 10, 'y' => 20]; + $json = Json::fromArray($arr); + $this->assertSame($arr, $json->toArray()); + + $obj = (object)['foo' => 'bar']; + $json2 = Json::fromObject($obj); + $this->assertSame(['foo' => 'bar'], $json2->toArray()); + } + + public function testCompressAndDecompress(): void + { + $json = new Json('{"a":1}'); + $encoder = new class () implements EncoderInterface { + public function encode(string $data): string + { + return base64_encode($data); + } + }; + $decoder = new class () implements DecoderInterface { + public function decode(string $data): string + { + return base64_decode($data); + } + }; + $compressed = $json->compress($encoder); + $this->assertSame(base64_encode('{"a":1}'), $compressed); + $decompressed = Json::decompress($decoder, $compressed); + $this->assertSame(['a' => 1], $decompressed->toArray()); + } + + public function testMerge(): void + { + $json1 = new Json('{"a":1,"b":2}'); + $json2 = new Json('{"b":3,"c":4}'); + $merged = $json1->merge($json2); + $this->assertSame(['a' => 1, 'b' => [2, 3], 'c' => 4], $merged->toArray()); + } + + public function testUpdateAndRemove(): void + { + $json = new Json('{"a":1,"b":2}'); + $updated = $json->update('b', 99); + $this->assertSame(['a' => 1, 'b' => 99], $updated->toArray()); + $removed = $updated->remove('a'); + $this->assertSame(['b' => 99], $removed->toArray()); + } + + public function testFromArrayInvalid(): void + { + $this->expectException(JsonException::class); + Json::fromArray(["bad" => fopen('php://memory', 'r')]); + } +} diff --git a/Tests/ListDataTest.php b/Tests/ListDataTest.php index 9f6d178..e39e723 100644 --- a/Tests/ListDataTest.php +++ b/Tests/ListDataTest.php @@ -7,7 +7,8 @@ use Nejcc\PhpDatatypes\Composite\ListData; use OutOfBoundsException; use PHPUnit\Framework\TestCase; -class ListDataTest extends TestCase + +final class ListDataTest extends TestCase { public function testCanInitializeWithElements() { diff --git a/Tests/Scalar/BooleanTest.php b/Tests/Scalar/BooleanTest.php new file mode 100644 index 0000000..f9f0907 --- /dev/null +++ b/Tests/Scalar/BooleanTest.php @@ -0,0 +1,86 @@ +assertTrue($true->getValue()); + $this->assertFalse($false->getValue()); + } + + public function testStringConversion(): void + { + $true = new Boolean(true); + $false = new Boolean(false); + + $this->assertEquals('true', (string)$true); + $this->assertEquals('false', (string)$false); + } + + public function testLogicalOperations(): void + { + $true = new Boolean(true); + $false = new Boolean(false); + + // AND operations + $this->assertTrue($true->and($true)->getValue()); + $this->assertFalse($true->and($false)->getValue()); + $this->assertFalse($false->and($true)->getValue()); + $this->assertFalse($false->and($false)->getValue()); + + // OR operations + $this->assertTrue($true->or($true)->getValue()); + $this->assertTrue($true->or($false)->getValue()); + $this->assertTrue($false->or($true)->getValue()); + $this->assertFalse($false->or($false)->getValue()); + + // XOR operations + $this->assertFalse($true->xor($true)->getValue()); + $this->assertTrue($true->xor($false)->getValue()); + $this->assertTrue($false->xor($true)->getValue()); + $this->assertFalse($false->xor($false)->getValue()); + + // NOT operations + $this->assertFalse($true->not()->getValue()); + $this->assertTrue($false->not()->getValue()); + } + + public function testEquals(): void + { + $true = new Boolean(true); + $false = new Boolean(false); + $anotherTrue = new Boolean(true); + $anotherFalse = new Boolean(false); + + $this->assertTrue($true->equals($anotherTrue)); + $this->assertTrue($false->equals($anotherFalse)); + $this->assertFalse($true->equals($false)); + $this->assertFalse($false->equals($true)); + } + + public function testFromString(): void + { + $this->assertTrue(Boolean::fromString('true')->getValue()); + $this->assertTrue(Boolean::fromString('TRUE')->getValue()); + $this->assertTrue(Boolean::fromString('True')->getValue()); + $this->assertFalse(Boolean::fromString('false')->getValue()); + $this->assertFalse(Boolean::fromString('FALSE')->getValue()); + $this->assertFalse(Boolean::fromString('False')->getValue()); + } + + public function testFromStringInvalidValue(): void + { + $this->expectException(\InvalidArgumentException::class); + Boolean::fromString('invalid'); + } +} diff --git a/Tests/Scalar/ByteTest.php b/Tests/Scalar/ByteTest.php new file mode 100644 index 0000000..c385f1d --- /dev/null +++ b/Tests/Scalar/ByteTest.php @@ -0,0 +1,165 @@ +assertEquals(0, $min->getValue()); + + // Test maximum value + $max = new Byte(255); + $this->assertEquals(255, $max->getValue()); + + // Test a value in the middle of the range + $middle = new Byte(128); + $this->assertEquals(128, $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Byte(256); // MAX_VALUE + 1 + } + + public function testNegativeValue(): void + { + $this->expectException(OutOfRangeException::class); + new Byte(-1); + } + + public function testAddition(): void + { + $a = new Byte(200); + $b = new Byte(50); + $sum = $a->add($b); + $this->assertEquals(250, $sum->getValue()); + } + + public function testAdditionWithWrap(): void + { + $a = new Byte(200); + $b = new Byte(100); + $sum = $a->add($b); + $this->assertEquals(44, $sum->getValue()); // (200 + 100) % 256 = 44 + } + + public function testSubtraction(): void + { + $a = new Byte(100); + $b = new Byte(50); + $diff = $a->subtract($b); + $this->assertEquals(50, $diff->getValue()); + } + + public function testSubtractionWithWrap(): void + { + $a = new Byte(50); + $b = new Byte(100); + $diff = $a->subtract($b); + $this->assertEquals(206, $diff->getValue()); // (50 - 100 + 256) % 256 = 206 + } + + public function testMultiplication(): void + { + $a = new Byte(10); + $b = new Byte(20); + $product = $a->multiply($b); + $this->assertEquals(200, $product->getValue()); + } + + public function testMultiplicationWithWrap(): void + { + $a = new Byte(20); + $b = new Byte(20); + $product = $a->multiply($b); + $this->assertEquals(144, $product->getValue()); // (20 * 20) % 256 = 144 + } + + public function testDivision(): void + { + $a = new Byte(100); + $b = new Byte(2); + $quotient = $a->divide($b); + $this->assertEquals(50, $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Byte(100); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Byte(0)); + } + + public function testBitwiseOperations(): void + { + $a = new Byte(0b10101010); + $b = new Byte(0b11110000); + + // AND + $and = $a->and($b); + $this->assertEquals(0b10100000, $and->getValue()); + + // OR + $or = $a->or($b); + $this->assertEquals(0b11111010, $or->getValue()); + + // XOR + $xor = $a->xor($b); + $this->assertEquals(0b01011010, $xor->getValue()); + + // NOT + $not = $a->not(); + $this->assertEquals(0b01010101, $not->getValue()); + } + + public function testShiftOperations(): void + { + $byte = new Byte(0b10101010); + + // Left shift + $leftShift = $byte->leftShift(2); + $this->assertEquals(0b10101000, $leftShift->getValue()); + + // Right shift + $rightShift = $byte->rightShift(2); + $this->assertEquals(0b00101010, $rightShift->getValue()); + } + + public function testStringConversion(): void + { + $byte = new Byte(170); // 0b10101010 + $this->assertEquals('170', (string)$byte); + } + + public function testBinaryConversion(): void + { + $byte = new Byte(170); // 0b10101010 + $this->assertEquals('10101010', $byte->toBinary()); + } + + public function testHexadecimalConversion(): void + { + $byte = new Byte(170); // 0xAA + $this->assertEquals('AA', $byte->toHex()); + } + + public function testEquals(): void + { + $a = new Byte(100); + $b = new Byte(100); + $c = new Byte(200); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } +} diff --git a/Tests/Scalar/CharTest.php b/Tests/Scalar/CharTest.php new file mode 100644 index 0000000..0a39ce1 --- /dev/null +++ b/Tests/Scalar/CharTest.php @@ -0,0 +1,141 @@ +assertEquals('A', $charA->getValue()); + + $charZ = new Char('Z'); + $this->assertEquals('Z', $charZ->getValue()); + + // Test lowercase letters + $charA = new Char('a'); + $this->assertEquals('a', $charA->getValue()); + + // Test numbers + $char0 = new Char('0'); + $this->assertEquals('0', $char0->getValue()); + + // Test special characters + $charSpace = new Char(' '); + $this->assertEquals(' ', $charSpace->getValue()); + + $charDot = new Char('.'); + $this->assertEquals('.', $charDot->getValue()); + } + + public function testInvalidCharacters(): void + { + $this->expectException(InvalidArgumentException::class); + new Char('AB'); // More than one character + } + + public function testEmptyString(): void + { + $this->expectException(InvalidArgumentException::class); + new Char(''); // Empty string + } + + public function testStringConversion(): void + { + $char = new Char('X'); + $this->assertEquals('X', (string)$char); + } + + public function testEquals(): void + { + $charA = new Char('A'); + $charB = new Char('B'); + $anotherCharA = new Char('A'); + + $this->assertTrue($charA->equals($anotherCharA)); + $this->assertFalse($charA->equals($charB)); + } + + public function testIsLetter(): void + { + $charA = new Char('A'); + $charZ = new Char('Z'); + $char0 = new Char('0'); + $charSpace = new Char(' '); + + $this->assertTrue($charA->isLetter()); + $this->assertTrue($charZ->isLetter()); + $this->assertFalse($char0->isLetter()); + $this->assertFalse($charSpace->isLetter()); + } + + public function testIsDigit(): void + { + $char0 = new Char('0'); + $char9 = new Char('9'); + $charA = new Char('A'); + $charSpace = new Char(' '); + + $this->assertTrue($char0->isDigit()); + $this->assertTrue($char9->isDigit()); + $this->assertFalse($charA->isDigit()); + $this->assertFalse($charSpace->isDigit()); + } + + public function testIsWhitespace(): void + { + $charSpace = new Char(' '); + $charTab = new Char("\t"); + $charNewline = new Char("\n"); + $charA = new Char('A'); + + $this->assertTrue($charSpace->isWhitespace()); + $this->assertTrue($charTab->isWhitespace()); + $this->assertTrue($charNewline->isWhitespace()); + $this->assertFalse($charA->isWhitespace()); + } + + public function testToUpperCase(): void + { + $charA = new Char('a'); + $charZ = new Char('z'); + $char0 = new Char('0'); + $charSpace = new Char(' '); + + $this->assertEquals('A', $charA->toUpperCase()->getValue()); + $this->assertEquals('Z', $charZ->toUpperCase()->getValue()); + $this->assertEquals('0', $char0->toUpperCase()->getValue()); + $this->assertEquals(' ', $charSpace->toUpperCase()->getValue()); + } + + public function testToLowerCase(): void + { + $charA = new Char('A'); + $charZ = new Char('Z'); + $char0 = new Char('0'); + $charSpace = new Char(' '); + + $this->assertEquals('a', $charA->toLowerCase()->getValue()); + $this->assertEquals('z', $charZ->toLowerCase()->getValue()); + $this->assertEquals('0', $char0->toLowerCase()->getValue()); + $this->assertEquals(' ', $charSpace->toLowerCase()->getValue()); + } + + public function testGetNumericValue(): void + { + $char0 = new Char('0'); + $char9 = new Char('9'); + $charA = new Char('A'); + + $this->assertEquals(0, $char0->getNumericValue()); + $this->assertEquals(9, $char9->getNumericValue()); + $this->assertEquals(-1, $charA->getNumericValue()); + } +} diff --git a/Tests/Scalar/FloatingPoints/Float32Test.php b/Tests/Scalar/FloatingPoints/Float32Test.php new file mode 100644 index 0000000..3a3fd43 --- /dev/null +++ b/Tests/Scalar/FloatingPoints/Float32Test.php @@ -0,0 +1,144 @@ +assertEquals(-3.4028235E38, $min->getValue()); + + // Test maximum value + $max = new Float32(3.4028235E38); + $this->assertEquals(3.4028235E38, $max->getValue()); + + // Test zero + $zero = new Float32(0.0); + $this->assertEquals(0.0, $zero->getValue()); + + // Test small positive value + $small = new Float32(1.17549435E-38); + $this->assertEquals(1.17549435E-38, $small->getValue()); + + // Test small negative value + $smallNeg = new Float32(-1.17549435E-38); + $this->assertEquals(-1.17549435E-38, $smallNeg->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Float32(3.4028236E38); // MAX_VALUE + epsilon + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Float32(-3.4028236E38); // MIN_VALUE - epsilon + } + + public function testAddition(): void + { + $a = new Float32(1.5); + $b = new Float32(2.5); + $sum = $a->add($b); + $this->assertEquals(4.0, $sum->getValue()); + } + + public function testSubtraction(): void + { + $a = new Float32(5.0); + $b = new Float32(2.5); + $diff = $a->subtract($b); + $this->assertEquals(2.5, $diff->getValue()); + } + + public function testMultiplication(): void + { + $a = new Float32(2.5); + $b = new Float32(2.0); + $product = $a->multiply($b); + $this->assertEquals(5.0, $product->getValue()); + } + + public function testDivision(): void + { + $a = new Float32(5.0); + $b = new Float32(2.0); + $quotient = $a->divide($b); + $this->assertEquals(2.5, $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Float32(5.0); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Float32(0.0)); + } + + public function testEquals(): void + { + $a = new Float32(1.5); + $b = new Float32(1.5); + $c = new Float32(2.5); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Float32(2.5); + $b = new Float32(1.5); + $c = new Float32(2.5); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isGreaterThan($c)); + } + + public function testIsLessThan(): void + { + $a = new Float32(1.5); + $b = new Float32(2.5); + $c = new Float32(1.5); + + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($a->isLessThan($c)); + } + + public function testStringConversion(): void + { + $float = new Float32(1.5); + $this->assertEquals('1.5', (string)$float); + } + + public function testPrecision(): void + { + // Test that precision is maintained within Float32 limits + $value = 1.23456789; + $float = new Float32($value); + $this->assertEquals($value, $float->getValue(), '', 1E-7); // Allow for small floating-point differences + } + + public function testSpecialValues(): void + { + // Test NaN + $nan = new Float32(NAN); + $this->assertTrue(is_nan($nan->getValue())); + + // INF and -INF are now disallowed, so expect OutOfRangeException + $this->expectException(\OutOfRangeException::class); + new Float32(INF); + + $this->expectException(\OutOfRangeException::class); + new Float32(-INF); + } +} diff --git a/Tests/Scalar/FloatingPoints/Float64Test.php b/Tests/Scalar/FloatingPoints/Float64Test.php new file mode 100644 index 0000000..aaa64a2 --- /dev/null +++ b/Tests/Scalar/FloatingPoints/Float64Test.php @@ -0,0 +1,144 @@ +assertEquals(-1.7976931348623157E308, $min->getValue()); + + // Test maximum value + $max = new Float64(1.7976931348623157E308); + $this->assertEquals(1.7976931348623157E308, $max->getValue()); + + // Test zero + $zero = new Float64(0.0); + $this->assertEquals(0.0, $zero->getValue()); + + // Test small positive value + $small = new Float64(2.2250738585072014E-308); + $this->assertEquals(2.2250738585072014E-308, $small->getValue()); + + // Test small negative value + $smallNeg = new Float64(-2.2250738585072014E-308); + $this->assertEquals(-2.2250738585072014E-308, $smallNeg->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Float64(1.7976931348623158E308 * 2); // MAX_VALUE * 2 + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Float64(-1.7976931348623158E308 * 2); // MIN_VALUE * 2 + } + + public function testAddition(): void + { + $a = new Float64(1.5); + $b = new Float64(2.5); + $sum = $a->add($b); + $this->assertEquals(4.0, $sum->getValue()); + } + + public function testSubtraction(): void + { + $a = new Float64(5.0); + $b = new Float64(2.5); + $diff = $a->subtract($b); + $this->assertEquals(2.5, $diff->getValue()); + } + + public function testMultiplication(): void + { + $a = new Float64(2.5); + $b = new Float64(2.0); + $product = $a->multiply($b); + $this->assertEquals(5.0, $product->getValue()); + } + + public function testDivision(): void + { + $a = new Float64(5.0); + $b = new Float64(2.0); + $quotient = $a->divide($b); + $this->assertEquals(2.5, $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Float64(5.0); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Float64(0.0)); + } + + public function testEquals(): void + { + $a = new Float64(1.5); + $b = new Float64(1.5); + $c = new Float64(2.5); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Float64(2.5); + $b = new Float64(1.5); + $c = new Float64(2.5); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isGreaterThan($c)); + } + + public function testIsLessThan(): void + { + $a = new Float64(1.5); + $b = new Float64(2.5); + $c = new Float64(1.5); + + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($a->isLessThan($c)); + } + + public function testStringConversion(): void + { + $float = new Float64(1.5); + $this->assertEquals('1.5', (string)$float); + } + + public function testPrecision(): void + { + // Test that precision is maintained within Float64 limits + $value = 1.2345678901234567; + $float = new Float64($value); + $this->assertEquals($value, $float->getValue(), '', 1E-15); // Allow for small floating-point differences + } + + public function testSpecialValues(): void + { + // Test NaN + $nan = new Float64(NAN); + $this->assertTrue(is_nan($nan->getValue())); + + // INF and -INF are now disallowed, so expect OutOfRangeException + $this->expectException(\OutOfRangeException::class); + new Float64(INF); + + $this->expectException(\OutOfRangeException::class); + new Float64(-INF); + } +} diff --git a/Tests/Scalar/Integers/Signed/Int128Test.php b/Tests/Scalar/Integers/Signed/Int128Test.php new file mode 100644 index 0000000..fbbb366 --- /dev/null +++ b/Tests/Scalar/Integers/Signed/Int128Test.php @@ -0,0 +1,166 @@ +assertSame('-170141183460469231731687303715884105728', $min->getValue()); + $this->assertSame('170141183460469231731687303715884105727', $max->getValue()); + $this->assertSame('0', $zero->getValue()); + $this->assertSame('123456789012345678901234567890123456', $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(\OutOfRangeException::class); + new Int128('170141183460469231731687303715884105728'); // MAX_VALUE + 1 + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(\OutOfRangeException::class); + new Int128('-170141183460469231731687303715884105729'); // MIN_VALUE - 1 + } + + public function testAddition(): void + { + $a = new Int128('170141183460469231731687303715884105720'); + $b = new Int128('7'); + $c = $a->add($b); + $this->assertSame('170141183460469231731687303715884105727', $c->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new Int128('170141183460469231731687303715884105727'); + $this->expectException(\OverflowException::class); + $max->add(new Int128('1')); + } + + public function testAdditionUnderflow(): void + { + $min = new Int128('-170141183460469231731687303715884105728'); + $this->expectException(\UnderflowException::class); + $min->add(new Int128('-1')); + } + + public function testSubtraction(): void + { + $a = new Int128('170141183460469231731687303715884105720'); + $b = new Int128('7'); + $c = $a->subtract($b); + $this->assertSame('170141183460469231731687303715884105713', $c->getValue()); + } + + public function testSubtractionOverflow(): void + { + $max = new Int128('170141183460469231731687303715884105727'); + $this->expectException(\OverflowException::class); + $max->subtract(new Int128('-1')); + } + + public function testSubtractionUnderflow(): void + { + $min = new Int128('-170141183460469231731687303715884105728'); + $this->expectException(\UnderflowException::class); + $min->subtract(new Int128('1')); + } + + public function testMultiplication(): void + { + $a = new Int128('100'); + $b = new Int128('50'); + $c = $a->multiply($b); + $this->assertSame('5000', $c->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new Int128('170141183460469231731687303715884105727'); + $this->expectException(\OverflowException::class); + $max->multiply(new Int128('2')); + } + + public function testMultiplicationUnderflow(): void + { + $min = new Int128('-170141183460469231731687303715884105728'); + $this->expectException(\UnderflowException::class); + $min->multiply(new Int128('2')); + } + + public function testDivision(): void + { + $a = new Int128('10000'); + $b = new Int128('2'); + $c = $a->divide($b); + $this->assertSame('5000', $c->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Int128('100'); + $b = new Int128('0'); + $this->expectException(\DivisionByZeroError::class); + $a->divide($b); + } + + public function testDivisionNonIntegerResult(): void + { + $a = new Int128('5'); + $b = new Int128('2'); + $this->expectException(\UnexpectedValueException::class); + $a->divide($b); + } + + public function testEquals(): void + { + $a = new Int128('100'); + $b = new Int128('100'); + $c = new Int128('50'); + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Int128('200'); + $b = new Int128('100'); + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($b->isGreaterThan($a)); + } + + public function testIsLessThan(): void + { + $a = new Int128('100'); + $b = new Int128('200'); + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($b->isLessThan($a)); + } + + public function testStringConversion(): void + { + $a = new Int128('123456789012345678901234567890123456'); + $this->assertSame('123456789012345678901234567890123456', (string)$a); + } + + public function testZeroOperations(): void + { + $zero = new Int128('0'); + $a = new Int128('100'); + $this->assertSame('100', $a->add($zero)->getValue()); + $this->assertSame('100', $a->subtract($zero)->getValue()); + $this->assertSame('0', $a->multiply($zero)->getValue()); + $this->assertSame('0', $zero->add($zero)->getValue()); + } +} diff --git a/Tests/Scalar/Integers/Signed/Int16Test.php b/Tests/Scalar/Integers/Signed/Int16Test.php new file mode 100644 index 0000000..219b3b2 --- /dev/null +++ b/Tests/Scalar/Integers/Signed/Int16Test.php @@ -0,0 +1,13 @@ +assertTrue(true); + } +} diff --git a/Tests/Scalar/Integers/Signed/Int32Test.php b/Tests/Scalar/Integers/Signed/Int32Test.php new file mode 100644 index 0000000..09e3d5b --- /dev/null +++ b/Tests/Scalar/Integers/Signed/Int32Test.php @@ -0,0 +1,192 @@ +assertEquals(-2147483648, $min->getValue()); + + // Test maximum value + $max = new Int32(2147483647); + $this->assertEquals(2147483647, $max->getValue()); + + // Test zero + $zero = new Int32(0); + $this->assertEquals(0, $zero->getValue()); + + // Test a value in the middle of the range + $middle = new Int32(1073741824); + $this->assertEquals(1073741824, $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int32(2147483648); // MAX_VALUE + 1 + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int32(-2147483649); // MIN_VALUE - 1 + } + + public function testAddition(): void + { + $a = new Int32(1000000000); + $b = new Int32(200000000); + $sum = $a->add($b); + $this->assertEquals(1200000000, $sum->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new Int32(2147483647); + $this->expectException(OverflowException::class); + $max->add(new Int32(1)); + } + + public function testAdditionUnderflow(): void + { + $min = new Int32(-2147483648); + $this->expectException(UnderflowException::class); + $min->add(new Int32(-1)); + } + + public function testSubtraction(): void + { + $a = new Int32(1000000000); + $b = new Int32(200000000); + $diff = $a->subtract($b); + $this->assertEquals(800000000, $diff->getValue()); + } + + public function testSubtractionOverflow(): void + { + $max = new Int32(2147483647); + $this->expectException(OverflowException::class); + $max->subtract(new Int32(-1)); + } + + public function testSubtractionUnderflow(): void + { + $min = new Int32(-2147483648); + $this->expectException(UnderflowException::class); + $min->subtract(new Int32(1)); + } + + public function testMultiplication(): void + { + $a = new Int32(100000); + $b = new Int32(20000); + $product = $a->multiply($b); + $this->assertEquals(2000000000, $product->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new Int32(2147483647); + $this->expectException(OverflowException::class); + $max->multiply(new Int32(2)); + } + + public function testMultiplicationUnderflow(): void + { + $min = new Int32(-2147483648); + $this->expectException(UnderflowException::class); + $min->multiply(new Int32(2)); + } + + public function testDivision(): void + { + $a = new Int32(1000000000); + $b = new Int32(2); + $quotient = $a->divide($b); + $this->assertEquals(500000000, $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Int32(1000000000); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Int32(0)); + } + + public function testDivisionNonIntegerResult(): void + { + $a = new Int32(5); + $this->expectException(\UnexpectedValueException::class); + $a->divide(new Int32(2)); + } + + public function testEquals(): void + { + $a = new Int32(1000000000); + $b = new Int32(1000000000); + $c = new Int32(500000000); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Int32(1000000000); + $b = new Int32(500000000); + $c = new Int32(1000000000); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isGreaterThan($c)); + } + + public function testIsLessThan(): void + { + $a = new Int32(500000000); + $b = new Int32(1000000000); + $c = new Int32(500000000); + + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($a->isLessThan($c)); + } + + public function testStringConversion(): void + { + $int = new Int32(1000000000); + $this->assertEquals('1000000000', (string)$int); + } + + public function testZeroOperations(): void + { + $zero = new Int32(0); + $one = new Int32(1); + $negOne = new Int32(-1); + + // Addition with zero + $sum = $zero->add($one); + $this->assertEquals(1, $sum->getValue()); + + // Subtraction with zero + $diff = $zero->subtract($one); + $this->assertEquals(-1, $diff->getValue()); + + // Multiplication with zero + $product = $zero->multiply($one); + $this->assertEquals(0, $product->getValue()); + + // Division of zero + $quotient = $zero->divide($one); + $this->assertEquals(0, $quotient->getValue()); + } +} diff --git a/Tests/Scalar/Integers/Signed/Int64Test.php b/Tests/Scalar/Integers/Signed/Int64Test.php new file mode 100644 index 0000000..9898814 --- /dev/null +++ b/Tests/Scalar/Integers/Signed/Int64Test.php @@ -0,0 +1,192 @@ +assertEquals('-9223372036854775808', $min->getValue()); + + // Test maximum value + $max = new Int64('9223372036854775807'); + $this->assertEquals('9223372036854775807', $max->getValue()); + + // Test zero + $zero = new Int64('0'); + $this->assertEquals('0', $zero->getValue()); + + // Test a value in the middle of the range + $middle = new Int64('4611686018427387904'); + $this->assertEquals('4611686018427387904', $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int64('9223372036854775808'); // MAX_VALUE + 1 + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int64('-9223372036854775809'); // MIN_VALUE - 1 + } + + public function testAddition(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('200000000000000000'); + $sum = $a->add($b); + $this->assertEquals('1200000000000000000', $sum->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new Int64('9223372036854775807'); + $this->expectException(OverflowException::class); + $max->add(new Int64('1')); + } + + public function testAdditionUnderflow(): void + { + $min = new Int64('-9223372036854775808'); + $this->expectException(UnderflowException::class); + $min->add(new Int64('-1')); + } + + public function testSubtraction(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('200000000000000000'); + $diff = $a->subtract($b); + $this->assertEquals('800000000000000000', $diff->getValue()); + } + + public function testSubtractionOverflow(): void + { + $max = new Int64('9223372036854775807'); + $this->expectException(OverflowException::class); + $max->subtract(new Int64('-1')); + } + + public function testSubtractionUnderflow(): void + { + $min = new Int64('-9223372036854775808'); + $this->expectException(UnderflowException::class); + $min->subtract(new Int64('1')); + } + + public function testMultiplication(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('2'); + $product = $a->multiply($b); + $this->assertEquals('2000000000000000000', $product->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new Int64('9223372036854775807'); + $this->expectException(OverflowException::class); + $max->multiply(new Int64('2')); + } + + public function testMultiplicationUnderflow(): void + { + $min = new Int64('-9223372036854775808'); + $this->expectException(UnderflowException::class); + $min->multiply(new Int64('2')); + } + + public function testDivision(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('2'); + $quotient = $a->divide($b); + $this->assertEquals('500000000000000000', $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Int64('1000000000000000000'); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Int64('0')); + } + + public function testDivisionNonIntegerResult(): void + { + $a = new Int64('5'); + $this->expectException(\UnexpectedValueException::class); + $a->divide(new Int64('2')); + } + + public function testEquals(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('1000000000000000000'); + $c = new Int64('500000000000000000'); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Int64('1000000000000000000'); + $b = new Int64('500000000000000000'); + $c = new Int64('1000000000000000000'); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isGreaterThan($c)); + } + + public function testIsLessThan(): void + { + $a = new Int64('500000000000000000'); + $b = new Int64('1000000000000000000'); + $c = new Int64('500000000000000000'); + + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($a->isLessThan($c)); + } + + public function testStringConversion(): void + { + $int = new Int64('1000000000000000000'); + $this->assertEquals('1000000000000000000', (string)$int); + } + + public function testZeroOperations(): void + { + $zero = new Int64('0'); + $one = new Int64('1'); + $negOne = new Int64('-1'); + + // Addition with zero + $sum = $zero->add($one); + $this->assertEquals('1', $sum->getValue()); + + // Subtraction with zero + $diff = $zero->subtract($one); + $this->assertEquals('-1', $diff->getValue()); + + // Multiplication with zero + $product = $zero->multiply($one); + $this->assertEquals('0', $product->getValue()); + + // Division of zero + $quotient = $zero->divide($one); + $this->assertEquals('0', $quotient->getValue()); + } +} diff --git a/Tests/Scalar/Integers/Signed/Int8Test.php b/Tests/Scalar/Integers/Signed/Int8Test.php new file mode 100644 index 0000000..ef2a27f --- /dev/null +++ b/Tests/Scalar/Integers/Signed/Int8Test.php @@ -0,0 +1,192 @@ +assertEquals(-128, $min->getValue()); + + // Test maximum value + $max = new Int8(127); + $this->assertEquals(127, $max->getValue()); + + // Test zero + $zero = new Int8(0); + $this->assertEquals(0, $zero->getValue()); + + // Test a value in the middle of the range + $middle = new Int8(64); + $this->assertEquals(64, $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int8(128); // MAX_VALUE + 1 + } + + public function testNegativeInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new Int8(-129); // MIN_VALUE - 1 + } + + public function testAddition(): void + { + $a = new Int8(100); + $b = new Int8(20); + $sum = $a->add($b); + $this->assertEquals(120, $sum->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new Int8(127); + $this->expectException(OverflowException::class); + $max->add(new Int8(1)); + } + + public function testAdditionUnderflow(): void + { + $min = new Int8(-128); + $this->expectException(UnderflowException::class); + $min->add(new Int8(-1)); + } + + public function testSubtraction(): void + { + $a = new Int8(100); + $b = new Int8(20); + $diff = $a->subtract($b); + $this->assertEquals(80, $diff->getValue()); + } + + public function testSubtractionOverflow(): void + { + $max = new Int8(127); + $this->expectException(OverflowException::class); + $max->subtract(new Int8(-1)); + } + + public function testSubtractionUnderflow(): void + { + $min = new Int8(-128); + $this->expectException(UnderflowException::class); + $min->subtract(new Int8(1)); + } + + public function testMultiplication(): void + { + $a = new Int8(10); + $b = new Int8(5); + $product = $a->multiply($b); + $this->assertEquals(50, $product->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new Int8(127); + $this->expectException(OverflowException::class); + $max->multiply(new Int8(2)); + } + + public function testMultiplicationUnderflow(): void + { + $min = new Int8(-128); + $this->expectException(UnderflowException::class); + $min->multiply(new Int8(2)); + } + + public function testDivision(): void + { + $a = new Int8(100); + $b = new Int8(2); + $quotient = $a->divide($b); + $this->assertEquals(50, $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new Int8(100); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new Int8(0)); + } + + public function testDivisionNonIntegerResult(): void + { + $a = new Int8(5); + $this->expectException(\UnexpectedValueException::class); + $a->divide(new Int8(2)); + } + + public function testEquals(): void + { + $a = new Int8(100); + $b = new Int8(100); + $c = new Int8(50); + + $this->assertTrue($a->equals($b)); + $this->assertFalse($a->equals($c)); + } + + public function testIsGreaterThan(): void + { + $a = new Int8(100); + $b = new Int8(50); + $c = new Int8(100); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isGreaterThan($c)); + } + + public function testIsLessThan(): void + { + $a = new Int8(50); + $b = new Int8(100); + $c = new Int8(50); + + $this->assertTrue($a->isLessThan($b)); + $this->assertFalse($a->isLessThan($c)); + } + + public function testStringConversion(): void + { + $int = new Int8(100); + $this->assertEquals('100', (string)$int); + } + + public function testZeroOperations(): void + { + $zero = new Int8(0); + $one = new Int8(1); + $negOne = new Int8(-1); + + // Addition with zero + $sum = $zero->add($one); + $this->assertEquals(1, $sum->getValue()); + + // Subtraction with zero + $diff = $zero->subtract($one); + $this->assertEquals(-1, $diff->getValue()); + + // Multiplication with zero + $product = $zero->multiply($one); + $this->assertEquals(0, $product->getValue()); + + // Division of zero + $quotient = $zero->divide($one); + $this->assertEquals(0, $quotient->getValue()); + } +} diff --git a/Tests/Scalar/Integers/Unsigned/UInt128Test.php b/Tests/Scalar/Integers/Unsigned/UInt128Test.php new file mode 100644 index 0000000..02f573e --- /dev/null +++ b/Tests/Scalar/Integers/Unsigned/UInt128Test.php @@ -0,0 +1,138 @@ +assertEquals('0', $min->getValue()); + + // Test maximum value + $max = new UInt128('340282366920938463463374607431768211455'); + $this->assertEquals('340282366920938463463374607431768211455', $max->getValue()); + + // Test a value in the middle of the range + $middle = new UInt128('170141183460469231731687303715884105727'); + $this->assertEquals('170141183460469231731687303715884105727', $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new UInt128('340282366920938463463374607431768211456'); // MAX_VALUE + 1 + } + + public function testNegativeValue(): void + { + $this->expectException(OutOfRangeException::class); + new UInt128('-1'); + } + + public function testAddition(): void + { + $a = new UInt128('340282366920938463463374607431768211450'); + $b = new UInt128('5'); + $sum = $a->add($b); + $this->assertEquals('340282366920938463463374607431768211455', $sum->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new UInt128('340282366920938463463374607431768211455'); + $this->expectException(OverflowException::class); + $max->add(new UInt128('1')); + } + + public function testSubtraction(): void + { + $a = new UInt128('100'); + $b = new UInt128('50'); + $diff = $a->subtract($b); + $this->assertEquals('50', $diff->getValue()); + } + + public function testSubtractionUnderflow(): void + { + $min = new UInt128('0'); + $this->expectException(UnderflowException::class); + $min->subtract(new UInt128('1')); + } + + public function testMultiplication(): void + { + $a = new UInt128('170141183460469231731687303715884105727'); + $b = new UInt128('2'); + $product = $a->multiply($b); + $this->assertEquals('340282366920938463463374607431768211454', $product->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new UInt128('340282366920938463463374607431768211455'); + $this->expectException(OverflowException::class); + $max->multiply(new UInt128('2')); + } + + public function testDivision(): void + { + $a = new UInt128('100'); + $b = new UInt128('2'); + $quotient = $a->divide($b); + $this->assertEquals('50', $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new UInt128('100'); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new UInt128('0')); + } + + public function testComparison(): void + { + $a = new UInt128('100'); + $b = new UInt128('50'); + $c = new UInt128('100'); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isLessThan($b)); + $this->assertTrue($a->equals($c)); + $this->assertFalse($a->equals($b)); + } + + public function testStringConversion(): void + { + $value = '340282366920938463463374607431768211455'; + $uint128 = new UInt128($value); + $this->assertEquals($value, (string) $uint128); + } + + public function testZeroOperations(): void + { + $zero = new UInt128('0'); + $one = new UInt128('1'); + + // Addition with zero + $sum = $zero->add($one); + $this->assertEquals('1', $sum->getValue()); + + // Multiplication with zero + $product = $zero->multiply($one); + $this->assertEquals('0', $product->getValue()); + + // Division of zero + $quotient = $zero->divide($one); + $this->assertEquals('0', $quotient->getValue()); + } +} diff --git a/Tests/Scalar/Integers/Unsigned/UInt64Test.php b/Tests/Scalar/Integers/Unsigned/UInt64Test.php new file mode 100644 index 0000000..1dad02b --- /dev/null +++ b/Tests/Scalar/Integers/Unsigned/UInt64Test.php @@ -0,0 +1,138 @@ +assertEquals('0', $min->getValue()); + + // Test maximum value + $max = new UInt64('18446744073709551615'); + $this->assertEquals('18446744073709551615', $max->getValue()); + + // Test a value in the middle of the range + $middle = new UInt64('9223372036854775807'); + $this->assertEquals('9223372036854775807', $middle->getValue()); + } + + public function testInvalidRange(): void + { + $this->expectException(OutOfRangeException::class); + new UInt64('18446744073709551616'); // MAX_VALUE + 1 + } + + public function testNegativeValue(): void + { + $this->expectException(OutOfRangeException::class); + new UInt64('-1'); + } + + public function testAddition(): void + { + $a = new UInt64('18446744073709551610'); + $b = new UInt64('5'); + $sum = $a->add($b); + $this->assertEquals('18446744073709551615', $sum->getValue()); + } + + public function testAdditionOverflow(): void + { + $max = new UInt64('18446744073709551615'); + $this->expectException(OverflowException::class); + $max->add(new UInt64('1')); + } + + public function testSubtraction(): void + { + $a = new UInt64('100'); + $b = new UInt64('50'); + $diff = $a->subtract($b); + $this->assertEquals('50', $diff->getValue()); + } + + public function testSubtractionUnderflow(): void + { + $min = new UInt64('0'); + $this->expectException(UnderflowException::class); + $min->subtract(new UInt64('1')); + } + + public function testMultiplication(): void + { + $a = new UInt64('9223372036854775807'); + $b = new UInt64('2'); + $product = $a->multiply($b); + $this->assertEquals('18446744073709551614', $product->getValue()); + } + + public function testMultiplicationOverflow(): void + { + $max = new UInt64('18446744073709551615'); + $this->expectException(OverflowException::class); + $max->multiply(new UInt64('2')); + } + + public function testDivision(): void + { + $a = new UInt64('100'); + $b = new UInt64('2'); + $quotient = $a->divide($b); + $this->assertEquals('50', $quotient->getValue()); + } + + public function testDivisionByZero(): void + { + $a = new UInt64('100'); + $this->expectException(\DivisionByZeroError::class); + $a->divide(new UInt64('0')); + } + + public function testComparison(): void + { + $a = new UInt64('100'); + $b = new UInt64('50'); + $c = new UInt64('100'); + + $this->assertTrue($a->isGreaterThan($b)); + $this->assertFalse($a->isLessThan($b)); + $this->assertTrue($a->equals($c)); + $this->assertFalse($a->equals($b)); + } + + public function testStringConversion(): void + { + $value = '18446744073709551615'; + $uint64 = new UInt64($value); + $this->assertEquals($value, (string) $uint64); + } + + public function testZeroOperations(): void + { + $zero = new UInt64('0'); + $one = new UInt64('1'); + + // Addition with zero + $sum = $zero->add($one); + $this->assertEquals('1', $sum->getValue()); + + // Multiplication with zero + $product = $zero->multiply($one); + $this->assertEquals('0', $product->getValue()); + + // Division of zero + $quotient = $zero->divide($one); + $this->assertEquals('0', $quotient->getValue()); + } +} diff --git a/Tests/StringArrayTest.php b/Tests/StringArrayTest.php index b275786..4384c98 100644 --- a/Tests/StringArrayTest.php +++ b/Tests/StringArrayTest.php @@ -1,4 +1,5 @@ 'string', - 'age' => '?int', - 'balance' => 'float', + 'id' => ['type' => 'int', 'nullable' => true], + 'name' => ['type' => 'string', 'nullable' => true], ]); - - // Test setting and getting field values using set/get methods - $struct->set('name', 'Nejc'); - $struct->set('age', null); // Nullable type - $struct->set('balance', 100.50); - - // Assertions - $this->assertEquals('Nejc', $struct->get('name')); - $this->assertNull($struct->get('age')); - $this->assertEquals(100.50, $struct->get('balance')); + $fields = $struct->getFields(); + $this->assertArrayHasKey('id', $fields); + $this->assertArrayHasKey('name', $fields); + $this->assertSame('int', $fields['id']['type']); + $this->assertSame('string', $fields['name']['type']); + $this->assertNull($fields['id']['value']); + $this->assertNull($fields['name']['value']); } - public function testMagicMethods() + public function testSetAndGet(): void { - // Example 1 with magic methods $struct = new Struct([ - 'name' => 'string', - 'age' => '?int', - 'balance' => 'float', + 'id' => ['type' => 'int', 'nullable' => true], + 'name' => ['type' => 'string', 'nullable' => true], ]); - - // Test setting and getting field values using magic methods - $struct->name = 'John'; - $struct->age = null; - $struct->balance = 200.75; - - // Assertions - $this->assertEquals('John', $struct->name); - $this->assertNull($struct->age); - $this->assertEquals(200.75, $struct->balance); + $struct->set('id', 42); + $struct->set('name', 'Alice'); + $this->assertSame(42, $struct->get('id')); + $this->assertSame('Alice', $struct->get('name')); } - public function testStructHelperFunction() + public function testSetWrongTypeThrows(): void { - // Example 2: using the `struct()` helper function (assuming it is defined) - $struct = struct([ - 'name' => 'string', - 'age' => '?int', - 'balance' => 'float', + $struct = new Struct([ + 'id' => ['type' => 'int', 'nullable' => true], ]); + $this->expectException(InvalidArgumentException::class); + $struct->set('id', 'not an int'); + } - // Test setting and getting field values using set/get methods - $struct->set('name', 'Test'); - $struct->set('age', null); - $struct->set('balance', 100.50); + public function testSetNullableField(): void + { + $struct = new Struct([ + 'desc' => ['type' => 'string', 'nullable' => true], + ]); + $struct->set('desc', null); + $this->assertNull($struct->get('desc')); + $struct->set('desc', 'hello'); + $this->assertSame('hello', $struct->get('desc')); + } - // Assertions - $this->assertEquals('Test', $struct->get('name')); - $this->assertNull($struct->get('age')); - $this->assertEquals(100.50, $struct->get('balance')); + public function testSetNonNullableFieldNullThrows(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Field 'id' is required and has no value"); + new Struct([ + 'id' => ['type' => 'int', 'nullable' => false], + ]); } - public function testInvalidFieldThrowsException() + public function testSetSubclass(): void { $struct = new Struct([ - 'name' => 'string', + 'obj' => ['type' => 'stdClass', 'nullable' => true], ]); + $obj = new class () extends \stdClass {}; + $struct->set('obj', $obj); + $this->assertSame($obj, $struct->get('obj')); + } + public function testGetNonexistentFieldThrows(): void + { + $struct = new Struct([ + 'id' => ['type' => 'int', 'nullable' => true], + ]); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Field 'age' does not exist in the struct."); - - $struct->set('age', 25); // This should throw an exception + $struct->get('missing'); } - public function testInvalidTypeThrowsException() + public function testSetNonexistentFieldThrows(): void { $struct = new Struct([ - 'name' => 'string', + 'id' => ['type' => 'int', 'nullable' => true], ]); + $this->expectException(InvalidArgumentException::class); + $struct->set('missing', 123); + } + public function testDuplicateFieldThrows(): void + { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Field 'name' expects type 'string', but got 'int'."); + // Simulate duplicate by calling addField directly via reflection + $struct = new Struct(['id' => ['type' => 'int', 'nullable' => true]]); + $ref = new \ReflectionClass($struct); + $method = $ref->getMethod('addField'); + $method->setAccessible(true); + $method->invoke($struct, 'id', 'int'); + } - $struct->set('name', 123); // Invalid type + public function testMagicGetSet(): void + { + $struct = new Struct([ + 'foo' => ['type' => 'int', 'nullable' => true], + ]); + $struct->foo = 123; + $this->assertSame(123, $struct->foo); } } diff --git a/Tests/UnionTest.php b/Tests/UnionTest.php index abcbdbf..6df2941 100644 --- a/Tests/UnionTest.php +++ b/Tests/UnionTest.php @@ -1,4 +1,5 @@ $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'IntArray Creation' + ]; + } + + public function benchmarkNativeArrayCreation(): array + { + $data = range(1, self::ARRAY_SIZE); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $array = $data; + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Native Array Creation' + ]; + } + + public function benchmarkIntArrayOperations(): array + { + $data = range(1, self::ARRAY_SIZE); + $array = new IntArray($data); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $array->toArray(); + $array->getValue(); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'IntArray Operations' + ]; + } + + public function benchmarkNativeArrayOperations(): array + { + $data = range(1, self::ARRAY_SIZE); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $copy = $data; + $count = count($data); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Native Array Operations' + ]; + } + + public function benchmarkDictionaryOperations(): array + { + $data = []; + for ($i = 0; $i < self::ARRAY_SIZE; $i++) { + $data["key_$i"] = "value_$i"; + } + $dict = new Dictionary($data); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $dict->toArray(); + $dict->size(); + $dict->getKeys(); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Dictionary Operations' + ]; + } + + public function benchmarkNativeAssociativeArrayOperations(): array + { + $data = []; + for ($i = 0; $i < self::ARRAY_SIZE; $i++) { + $data["key_$i"] = "value_$i"; + } + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $copy = $data; + $count = count($data); + $keys = array_keys($data); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Native Associative Array Operations' + ]; + } + + public function benchmarkIntArrayFromTrusted(): array + { + $data = range(1, self::ARRAY_SIZE); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $array = IntArray::fromTrusted($data); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'IntArray::fromTrusted' + ]; + } + + public function runAllBenchmarks(): array + { + return [ + 'int_array_creation' => $this->benchmarkIntArrayCreation(), + 'int_array_from_trusted' => $this->benchmarkIntArrayFromTrusted(), + 'native_array_creation' => $this->benchmarkNativeArrayCreation(), + 'int_array_operations' => $this->benchmarkIntArrayOperations(), + 'native_array_operations' => $this->benchmarkNativeArrayOperations(), + 'dictionary_operations' => $this->benchmarkDictionaryOperations(), + 'native_assoc_array_operations' => $this->benchmarkNativeAssociativeArrayOperations(), + ]; + } + + public function printResults(array $results): void + { + echo "=== Array Benchmark Results ===\n\n"; + + foreach ($results as $name => $result) { + echo sprintf( + "%s:\n Time: %.6f seconds\n Memory: %d bytes\n Iterations: %d\n Time per iteration: %.9f seconds\n\n", + $result['type'], + $result['time'], + $result['memory'], + $result['iterations'], + $result['time'] / $result['iterations'] + ); + } + + // Compare IntArray vs Native + $intArrayCreation = $results['int_array_creation']; + $nativeArrayCreation = $results['native_array_creation']; + $intArrayOps = $results['int_array_operations']; + $nativeArrayOps = $results['native_array_operations']; + + echo "=== Performance Comparison ===\n"; + echo sprintf( + "IntArray Creation vs Native: %.2fx slower\n", + $intArrayCreation['time'] / $nativeArrayCreation['time'] + ); + echo sprintf( + "IntArray Operations vs Native: %.2fx slower\n", + $intArrayOps['time'] / $nativeArrayOps['time'] + ); + echo sprintf( + "IntArray Memory overhead: %d bytes per operation\n", + ($intArrayCreation['memory'] - $nativeArrayCreation['memory']) / $intArrayCreation['iterations'] + ); + } +} diff --git a/benchmarks/IntegerBenchmark.php b/benchmarks/IntegerBenchmark.php new file mode 100644 index 0000000..e861db1 --- /dev/null +++ b/benchmarks/IntegerBenchmark.php @@ -0,0 +1,201 @@ + $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Int8 Creation' + ]; + } + + public function benchmarkNativeIntCreation(): array + { + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $int = 42; + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Native Int Creation' + ]; + } + + public function benchmarkInt8Arithmetic(): array + { + $int1 = new Int8(5); + $int2 = new Int8(3); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $result = $int1->add($int2); + $result = $int1->subtract($int2); + $result = $int1->multiply($int2); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Int8 Arithmetic' + ]; + } + + public function benchmarkNativeIntArithmetic(): array + { + $int1 = 5; + $int2 = 3; + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $result = $int1 + $int2; + $result = $int1 - $int2; + $result = $int1 * $int2; + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Native Int Arithmetic' + ]; + } + + public function benchmarkBigIntegerOperations(): array + { + $int1 = new Int64('9223372036854775800'); + $int2 = new Int64('7'); + + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS / 100; $i++) { // Fewer iterations for big ints + $result = $int1->add($int2); + $result = $int1->subtract($int2); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS / 100, + 'type' => 'Int64 Arithmetic' + ]; + } + + public function benchmarkInt8Of(): array + { + $start = microtime(true); + $memoryStart = memory_get_usage(); + + for ($i = 0; $i < self::ITERATIONS; $i++) { + $int = Int8::of(42); + } + + $end = microtime(true); + $memoryEnd = memory_get_usage(); + + return [ + 'time' => $end - $start, + 'memory' => $memoryEnd - $memoryStart, + 'iterations' => self::ITERATIONS, + 'type' => 'Int8::of (cached)' + ]; + } + + public function runAllBenchmarks(): array + { + return [ + 'int8_creation' => $this->benchmarkInt8Creation(), + 'int8_of' => $this->benchmarkInt8Of(), + 'native_int_creation' => $this->benchmarkNativeIntCreation(), + 'int8_arithmetic' => $this->benchmarkInt8Arithmetic(), + 'native_int_arithmetic' => $this->benchmarkNativeIntArithmetic(), + 'big_int_arithmetic' => $this->benchmarkBigIntegerOperations(), + ]; + } + + public function printResults(array $results): void + { + echo "=== Integer Benchmark Results ===\n\n"; + + foreach ($results as $name => $result) { + echo sprintf( + "%s:\n Time: %.6f seconds\n Memory: %d bytes\n Iterations: %d\n Time per iteration: %.9f seconds\n\n", + $result['type'], + $result['time'], + $result['memory'], + $result['iterations'], + $result['time'] / $result['iterations'] + ); + } + + // Compare Int8 vs Native + $int8Creation = $results['int8_creation']; + $nativeCreation = $results['native_int_creation']; + $int8Arithmetic = $results['int8_arithmetic']; + $nativeArithmetic = $results['native_int_arithmetic']; + + echo "=== Performance Comparison ===\n"; + echo sprintf( + "Int8 Creation vs Native: %.2fx slower\n", + $int8Creation['time'] / $nativeCreation['time'] + ); + echo sprintf( + "Int8 Arithmetic vs Native: %.2fx slower\n", + $int8Arithmetic['time'] / $nativeArithmetic['time'] + ); + echo sprintf( + "Int8 Memory overhead: %d bytes per operation\n", + ($int8Creation['memory'] - $nativeCreation['memory']) / $int8Creation['iterations'] + ); + } +} diff --git a/benchmarks/run_benchmarks.php b/benchmarks/run_benchmarks.php new file mode 100644 index 0000000..e4b115c --- /dev/null +++ b/benchmarks/run_benchmarks.php @@ -0,0 +1,31 @@ +runAllBenchmarks(); +$integerBenchmark->printResults($integerResults); + +echo "\n" . str_repeat("=", 50) . "\n\n"; + +// Run array benchmarks +echo "Running Array Benchmarks...\n"; +$arrayBenchmark = new ArrayBenchmark(); +$arrayResults = $arrayBenchmark->runAllBenchmarks(); +$arrayBenchmark->printResults($arrayResults); + +echo "\n" . str_repeat("=", 50) . "\n"; +echo "Benchmark completed!\n"; diff --git a/build/logs/junit.xml b/build/logs/junit.xml index 225927a..d3254a0 100644 --- a/build/logs/junit.xml +++ b/build/logs/junit.xml @@ -1,247 +1,688 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index e59f33c..3648f94 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,16 @@ } ], "require": { - "php": "^8.2", + "php": "^8.4", "ext-bcmath": "*", "ext-ctype": "*", "ext-zlib": "*" }, "require-dev": { - "phpunit/phpunit": "^11.4.2" + "laravel/pint": "^1.22", + "phpunit/phpunit": "^11.4.2", + "phpstan/phpstan": "^1.10", + "infection/infection": "^0.27" }, "autoload": { "psr-4": { @@ -34,13 +37,19 @@ }, "autoload-dev": { "psr-4": { - "Nejcc\\PhpDatatypes\\Tests\\": "tests" + "Nejcc\\PhpDatatypes\\Tests\\": "tests", + "Nejcc\\PhpDatatypes\\Benchmarks\\": "benchmarks" } }, "scripts": { "test": "vendor/bin/phpunit", "test-box": "vendor/bin/phpunit --testdox", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage" + "test-coverage": "vendor/bin/phpunit --coverage-html coverage", + "phpstan": "vendor/bin/phpstan analyse", + "phpstan-baseline": "vendor/bin/phpstan analyse --generate-baseline", + "benchmark": "php benchmarks/run_benchmarks.php", + "infection": "vendor/bin/infection", + "infection-baseline": "vendor/bin/infection --generate-baseline" }, "config": { "sort-packages": true diff --git a/composer.lock b/composer.lock index 54fbc5f..e59339d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,21 +4,87 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1f565fe082028eca22f4d68bd2ec44d0", + "content-hash": "08cc4e4faa22a4f7901d5251d099ec60", "packages": [], "packages-dev": [ + { + "name": "laravel/pint", + "version": "v1.25.1", + "source": { + "type": "git", + "url": "https://github.com/laravel/pint.git", + "reference": "5016e263f95d97670d71b9a987bd8996ade6d8d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pint/zipball/5016e263f95d97670d71b9a987bd8996ade6d8d9", + "reference": "5016e263f95d97670d71b9a987bd8996ade6d8d9", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^8.2.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.87.2", + "illuminate/view": "^11.46.0", + "larastan/larastan": "^3.7.1", + "laravel-zero/framework": "^11.45.0", + "mockery/mockery": "^1.6.12", + "nunomaduro/termwind": "^2.3.1", + "pestphp/pest": "^2.36.0" + }, + "bin": [ + "builds/pint" + ], + "type": "project", + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Seeders\\": "database/seeders/", + "Database\\Factories\\": "database/factories/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "An opinionated code formatter for PHP.", + "homepage": "https://laravel.com", + "keywords": [ + "format", + "formatter", + "lint", + "linter", + "php" + ], + "support": { + "issues": "https://github.com/laravel/pint/issues", + "source": "https://github.com/laravel/pint" + }, + "time": "2025-09-19T02:57:12+00:00" + }, { "name": "myclabs/deep-copy", - "version": "1.12.0", + "version": "1.13.4", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a", + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a", "shasum": "" }, "require": { @@ -57,7 +123,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" + "source": "https://github.com/myclabs/DeepCopy/tree/1.13.4" }, "funding": [ { @@ -65,20 +131,20 @@ "type": "tidelift" } ], - "time": "2024-06-12T14:39:25+00:00" + "time": "2025-08-01T08:46:24+00:00" }, { "name": "nikic/php-parser", - "version": "v5.3.1", + "version": "v5.6.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" + "reference": "f103601b29efebd7ff4a1ca7b3eeea9e3336a2a2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", - "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/f103601b29efebd7ff4a1ca7b3eeea9e3336a2a2", + "reference": "f103601b29efebd7ff4a1ca7b3eeea9e3336a2a2", "shasum": "" }, "require": { @@ -97,7 +163,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "5.0-dev" + "dev-master": "5.x-dev" } }, "autoload": { @@ -121,9 +187,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.6.1" }, - "time": "2024-10-08T18:51:32+00:00" + "time": "2025-08-13T20:13:15+00:00" }, { "name": "phar-io/manifest", @@ -245,23 +311,23 @@ }, { "name": "phpunit/php-code-coverage", - "version": "11.0.7", + "version": "11.0.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "f7f08030e8811582cc459871d28d6f5a1a4d35ca" + "reference": "4f7722aa9a7b76aa775e2d9d4e95d1ea16eeeef4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f7f08030e8811582cc459871d28d6f5a1a4d35ca", - "reference": "f7f08030e8811582cc459871d28d6f5a1a4d35ca", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/4f7722aa9a7b76aa775e2d9d4e95d1ea16eeeef4", + "reference": "4f7722aa9a7b76aa775e2d9d4e95d1ea16eeeef4", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^5.3.1", + "nikic/php-parser": "^5.4.0", "php": ">=8.2", "phpunit/php-file-iterator": "^5.1.0", "phpunit/php-text-template": "^4.0.1", @@ -273,7 +339,7 @@ "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^11.4.1" + "phpunit/phpunit": "^11.5.2" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -311,15 +377,27 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/11.0.7" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/11.0.11" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpunit/php-code-coverage", + "type": "tidelift" } ], - "time": "2024-10-09T06:21:38+00:00" + "time": "2025-08-27T14:37:49+00:00" }, { "name": "phpunit/php-file-iterator", @@ -568,16 +646,16 @@ }, { "name": "phpunit/phpunit", - "version": "11.4.3", + "version": "11.5.42", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "e8e8ed1854de5d36c088ec1833beae40d2dedd76" + "reference": "1c6cb5dfe412af3d0dfd414cfd110e3b9cfdbc3c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e8e8ed1854de5d36c088ec1833beae40d2dedd76", - "reference": "e8e8ed1854de5d36c088ec1833beae40d2dedd76", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1c6cb5dfe412af3d0dfd414cfd110e3b9cfdbc3c", + "reference": "1c6cb5dfe412af3d0dfd414cfd110e3b9cfdbc3c", "shasum": "" }, "require": { @@ -587,25 +665,26 @@ "ext-mbstring": "*", "ext-xml": "*", "ext-xmlwriter": "*", - "myclabs/deep-copy": "^1.12.0", + "myclabs/deep-copy": "^1.13.4", "phar-io/manifest": "^2.0.4", "phar-io/version": "^3.2.1", "php": ">=8.2", - "phpunit/php-code-coverage": "^11.0.7", + "phpunit/php-code-coverage": "^11.0.11", "phpunit/php-file-iterator": "^5.1.0", "phpunit/php-invoker": "^5.0.1", "phpunit/php-text-template": "^4.0.1", "phpunit/php-timer": "^7.0.1", "sebastian/cli-parser": "^3.0.2", - "sebastian/code-unit": "^3.0.1", - "sebastian/comparator": "^6.1.1", + "sebastian/code-unit": "^3.0.3", + "sebastian/comparator": "^6.3.2", "sebastian/diff": "^6.0.2", - "sebastian/environment": "^7.2.0", - "sebastian/exporter": "^6.1.3", + "sebastian/environment": "^7.2.1", + "sebastian/exporter": "^6.3.2", "sebastian/global-state": "^7.0.2", "sebastian/object-enumerator": "^6.0.1", - "sebastian/type": "^5.1.0", - "sebastian/version": "^5.0.2" + "sebastian/type": "^5.1.3", + "sebastian/version": "^5.0.2", + "staabm/side-effects-detector": "^1.0.5" }, "suggest": { "ext-soap": "To be able to generate mocks based on WSDL files" @@ -616,7 +695,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "11.4-dev" + "dev-main": "11.5-dev" } }, "autoload": { @@ -648,7 +727,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/11.4.3" + "source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.42" }, "funding": [ { @@ -659,12 +738,20 @@ "url": "https://github.com/sebastianbergmann", "type": "github" }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, { "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", "type": "tidelift" } ], - "time": "2024-10-28T13:07:50+00:00" + "time": "2025-09-28T12:09:13+00:00" }, { "name": "sebastian/cli-parser", @@ -725,23 +812,23 @@ }, { "name": "sebastian/code-unit", - "version": "3.0.1", + "version": "3.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/code-unit.git", - "reference": "6bb7d09d6623567178cf54126afa9c2310114268" + "reference": "54391c61e4af8078e5b276ab082b6d3c54c9ad64" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit/zipball/6bb7d09d6623567178cf54126afa9c2310114268", - "reference": "6bb7d09d6623567178cf54126afa9c2310114268", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit/zipball/54391c61e4af8078e5b276ab082b6d3c54c9ad64", + "reference": "54391c61e4af8078e5b276ab082b6d3c54c9ad64", "shasum": "" }, "require": { "php": ">=8.2" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.5" }, "type": "library", "extra": { @@ -770,7 +857,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/code-unit/issues", "security": "https://github.com/sebastianbergmann/code-unit/security/policy", - "source": "https://github.com/sebastianbergmann/code-unit/tree/3.0.1" + "source": "https://github.com/sebastianbergmann/code-unit/tree/3.0.3" }, "funding": [ { @@ -778,7 +865,7 @@ "type": "github" } ], - "time": "2024-07-03T04:44:28+00:00" + "time": "2025-03-19T07:56:08+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -838,16 +925,16 @@ }, { "name": "sebastian/comparator", - "version": "6.1.1", + "version": "6.3.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "5ef523a49ae7a302b87b2102b72b1eda8918d686" + "reference": "85c77556683e6eee4323e4c5468641ca0237e2e8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/5ef523a49ae7a302b87b2102b72b1eda8918d686", - "reference": "5ef523a49ae7a302b87b2102b72b1eda8918d686", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/85c77556683e6eee4323e4c5468641ca0237e2e8", + "reference": "85c77556683e6eee4323e4c5468641ca0237e2e8", "shasum": "" }, "require": { @@ -858,12 +945,15 @@ "sebastian/exporter": "^6.0" }, "require-dev": { - "phpunit/phpunit": "^11.3" + "phpunit/phpunit": "^11.4" + }, + "suggest": { + "ext-bcmath": "For comparing BcMath\\Number objects" }, "type": "library", "extra": { "branch-alias": { - "dev-main": "6.1-dev" + "dev-main": "6.3-dev" } }, "autoload": { @@ -903,15 +993,27 @@ "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", "security": "https://github.com/sebastianbergmann/comparator/security/policy", - "source": "https://github.com/sebastianbergmann/comparator/tree/6.1.1" + "source": "https://github.com/sebastianbergmann/comparator/tree/6.3.2" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/comparator", + "type": "tidelift" } ], - "time": "2024-10-18T15:00:48+00:00" + "time": "2025-08-10T08:07:46+00:00" }, { "name": "sebastian/complexity", @@ -1040,23 +1142,23 @@ }, { "name": "sebastian/environment", - "version": "7.2.0", + "version": "7.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "855f3ae0ab316bbafe1ba4e16e9f3c078d24a0c5" + "reference": "a5c75038693ad2e8d4b6c15ba2403532647830c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/855f3ae0ab316bbafe1ba4e16e9f3c078d24a0c5", - "reference": "855f3ae0ab316bbafe1ba4e16e9f3c078d24a0c5", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/a5c75038693ad2e8d4b6c15ba2403532647830c4", + "reference": "a5c75038693ad2e8d4b6c15ba2403532647830c4", "shasum": "" }, "require": { "php": ">=8.2" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.3" }, "suggest": { "ext-posix": "*" @@ -1092,28 +1194,40 @@ "support": { "issues": "https://github.com/sebastianbergmann/environment/issues", "security": "https://github.com/sebastianbergmann/environment/security/policy", - "source": "https://github.com/sebastianbergmann/environment/tree/7.2.0" + "source": "https://github.com/sebastianbergmann/environment/tree/7.2.1" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/environment", + "type": "tidelift" } ], - "time": "2024-07-03T04:54:44+00:00" + "time": "2025-05-21T11:55:47+00:00" }, { "name": "sebastian/exporter", - "version": "6.1.3", + "version": "6.3.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "c414673eee9a8f9d51bbf8d61fc9e3ef1e85b20e" + "reference": "70a298763b40b213ec087c51c739efcaa90bcd74" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/c414673eee9a8f9d51bbf8d61fc9e3ef1e85b20e", - "reference": "c414673eee9a8f9d51bbf8d61fc9e3ef1e85b20e", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/70a298763b40b213ec087c51c739efcaa90bcd74", + "reference": "70a298763b40b213ec087c51c739efcaa90bcd74", "shasum": "" }, "require": { @@ -1122,12 +1236,12 @@ "sebastian/recursion-context": "^6.0" }, "require-dev": { - "phpunit/phpunit": "^11.2" + "phpunit/phpunit": "^11.3" }, "type": "library", "extra": { "branch-alias": { - "dev-main": "6.1-dev" + "dev-main": "6.3-dev" } }, "autoload": { @@ -1170,15 +1284,27 @@ "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", "security": "https://github.com/sebastianbergmann/exporter/security/policy", - "source": "https://github.com/sebastianbergmann/exporter/tree/6.1.3" + "source": "https://github.com/sebastianbergmann/exporter/tree/6.3.2" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/exporter", + "type": "tidelift" } ], - "time": "2024-07-03T04:56:19+00:00" + "time": "2025-09-24T06:12:51+00:00" }, { "name": "sebastian/global-state", @@ -1416,23 +1542,23 @@ }, { "name": "sebastian/recursion-context", - "version": "6.0.2", + "version": "6.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "694d156164372abbd149a4b85ccda2e4670c0e16" + "reference": "f6458abbf32a6c8174f8f26261475dc133b3d9dc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/694d156164372abbd149a4b85ccda2e4670c0e16", - "reference": "694d156164372abbd149a4b85ccda2e4670c0e16", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/f6458abbf32a6c8174f8f26261475dc133b3d9dc", + "reference": "f6458abbf32a6c8174f8f26261475dc133b3d9dc", "shasum": "" }, "require": { "php": ">=8.2" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.3" }, "type": "library", "extra": { @@ -1468,28 +1594,40 @@ "support": { "issues": "https://github.com/sebastianbergmann/recursion-context/issues", "security": "https://github.com/sebastianbergmann/recursion-context/security/policy", - "source": "https://github.com/sebastianbergmann/recursion-context/tree/6.0.2" + "source": "https://github.com/sebastianbergmann/recursion-context/tree/6.0.3" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/recursion-context", + "type": "tidelift" } ], - "time": "2024-07-03T05:10:34+00:00" + "time": "2025-08-13T04:42:22+00:00" }, { "name": "sebastian/type", - "version": "5.1.0", + "version": "5.1.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/type.git", - "reference": "461b9c5da241511a2a0e8f240814fb23ce5c0aac" + "reference": "f77d2d4e78738c98d9a68d2596fe5e8fa380f449" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/461b9c5da241511a2a0e8f240814fb23ce5c0aac", - "reference": "461b9c5da241511a2a0e8f240814fb23ce5c0aac", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/f77d2d4e78738c98d9a68d2596fe5e8fa380f449", + "reference": "f77d2d4e78738c98d9a68d2596fe5e8fa380f449", "shasum": "" }, "require": { @@ -1525,15 +1663,27 @@ "support": { "issues": "https://github.com/sebastianbergmann/type/issues", "security": "https://github.com/sebastianbergmann/type/security/policy", - "source": "https://github.com/sebastianbergmann/type/tree/5.1.0" + "source": "https://github.com/sebastianbergmann/type/tree/5.1.3" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/type", + "type": "tidelift" } ], - "time": "2024-09-17T13:12:04+00:00" + "time": "2025-08-09T06:55:48+00:00" }, { "name": "sebastian/version", @@ -1589,6 +1739,58 @@ ], "time": "2024-10-09T05:16:32+00:00" }, + { + "name": "staabm/side-effects-detector", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/staabm/side-effects-detector.git", + "reference": "d8334211a140ce329c13726d4a715adbddd0a163" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/staabm/side-effects-detector/zipball/d8334211a140ce329c13726d4a715adbddd0a163", + "reference": "d8334211a140ce329c13726d4a715adbddd0a163", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "phpstan/extension-installer": "^1.4.3", + "phpstan/phpstan": "^1.12.6", + "phpunit/phpunit": "^9.6.21", + "symfony/var-dumper": "^5.4.43", + "tomasvotruba/type-coverage": "1.0.0", + "tomasvotruba/unused-public": "1.0.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "lib/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A static analysis tool to detect side effects in PHP code", + "keywords": [ + "static analysis" + ], + "support": { + "issues": "https://github.com/staabm/side-effects-detector/issues", + "source": "https://github.com/staabm/side-effects-detector/tree/1.0.5" + }, + "funding": [ + { + "url": "https://github.com/staabm", + "type": "github" + } + ], + "time": "2024-10-20T05:08:20+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.3", @@ -1642,7 +1844,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -1651,6 +1853,6 @@ "ext-ctype": "*", "ext-zlib": "*" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..c08a675 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,494 @@ +# PHP Datatypes Examples + +This directory contains example scripts demonstrating the usage of various data types provided by the PHP Datatypes library. + +## Running the Examples + +To run any example, use PHP from the command line: + +```bash +php integer_operations.php +php float_operations.php +php array_operations.php +php char_operations.php +php boolean_operations.php +php string_operations.php +``` + +## Available Examples + +### 1. Integer Operations (`integer_operations.php`) + +This example demonstrates various operations with integer types: + +- Basic arithmetic operations (addition, subtraction, multiplication, division) +- Range validation +- Overflow handling +- Comparison operations +- Working with different integer types (Int8, Int16, UInt8) +- Division and modulo operations + +The example includes error handling to demonstrate how the library handles various edge cases and invalid operations. + +### 2. Float Operations (`float_operations.php`) + +This example demonstrates various operations with floating-point types: + +- Basic arithmetic operations with Float32 and Float64 +- Precision comparison between Float32 and Float64 +- Handling of special values (Infinity, NaN, very small numbers) +- Comparison operations +- Rounding operations (round, ceil, floor) +- Mathematical functions (sin, cos, tan, sqrt) + +The example includes error handling and demonstrates the precision differences between Float32 and Float64 types. + +### 3. Array Operations (`array_operations.php`) + +This example demonstrates working with arrays of typed values: + +- Working with arrays of Int8 values (sum, max) +- Array operations with Float32 values (average, filtering) +- Mixed type arrays and type conversion +- Array filtering and mapping operations +- Array reduction operations (product, sum of squares) + +The example shows how to use PHP's array functions with typed values while maintaining type safety. + +### 4. Char Operations (`char_operations.php`) + +This example demonstrates working with single characters: + +- Basic character operations (creation, string conversion) +- Character type checking (letters, digits, case) +- ASCII operations (conversion to/from ASCII codes) +- Character comparison +- Error handling for invalid inputs +- Character transformation chains + +The example shows how to work with individual characters in a type-safe way. + +### 5. Boolean Operations (`boolean_operations.php`) + +This example demonstrates working with boolean values: + +- Basic boolean operations (creation, string conversion) +- Boolean logic operations (AND, OR, NOT) +- Boolean comparison +- Type conversion (from/to int, string, float) +- Error handling for invalid inputs +- Boolean chain operations + +The example shows how to work with boolean values in a type-safe way. + +### 6. String Operations (`string_operations.php`) + +This example demonstrates working with strings: + +- Basic string operations (creation, concatenation, length) +- String case operations (uppercase, lowercase, title case) +- String search and replace operations +- String trimming and padding +- String splitting and joining +- String comparison and validation +- String substring and character access +- String formatting with placeholders + +The example shows how to work with strings in a type-safe way. + +## Example Output + +### Integer Operations Output + +When you run `integer_operations.php`, you'll see output similar to this: + +``` +Example 1: Basic Int8 Operations +============================== +Number 1: 42 +Number 2: 10 +Sum: 52 +Difference: 32 +Product: 420 +Quotient: 4 + +Example 2: Range Validation +======================== +Range validation works: Value must be between -128 and 127. + +Example 3: Overflow Handling +========================= +Overflow protection works: Result is out of bounds. + +Example 4: Comparison Operations +============================= +Is 50 greater than 30? Yes +Is 50 less than 30? No +Is 50 equal to 30? No + +Example 5: Working with Different Integer Types +=========================================== +Int8 value: 100 +Int16 value: 1000 +UInt8 value: 200 + +Example 6: Division and Modulo Operations +===================================== +Division validation works: Division result is not an integer. +50 divided by 5 = 10 +50 modulo 5 = 0 +``` + +### Float Operations Output + +When you run `float_operations.php`, you'll see output similar to this: + +``` +Example 1: Basic Float Operations +============================== +Float 1: 3.14159 +Float 2: 2.0 +Sum: 5.14159 +Difference: 1.14159 +Product: 6.28318 +Quotient: 1.570795 + +Example 2: Precision Comparison +=========================== +Float32 value: 1.2345678 +Float64 value: 1.23456789 +Note the difference in precision between Float32 and Float64 + +Example 3: Special Values +===================== +Infinity: INF +NaN: NAN +Very small number: 1.0E-45 + +Example 4: Comparison Operations +============================= +Is 3.14 greater than 2.71? Yes +Is 3.14 less than 2.71? No +Is 3.14 equal to 2.71? No + +Example 5: Rounding Operations +========================== +Original number: 3.14159 +Rounded to 2 decimal places: 3.14 +Ceiling: 4 +Floor: 3 + +Example 6: Mathematical Functions +============================= +Original number: 0.5 +Sine: 0.4794255386042 +Cosine: 0.87758256189037 +Tangent: 0.54630248984379 +Square root: 0.70710678118655 +``` + +### Array Operations Output + +When you run `array_operations.php`, you'll see output similar to this: + +``` +Example 1: Working with Arrays of Int8 +================================== +Sum of numbers: 150 +Maximum value: 50 + +Example 2: Array Operations with Float32 +==================================== +Average temperature: 23.92°C +Temperatures above average: +- 24.8°C +- 25.1°C + +Example 3: Mixed Type Arrays +========================= +Original integers: 1, 2, 3 +Converted to floats: 1.0, 2.0, 3.0 +Sums of corresponding values: 2.5, 4.5, 6.5 + +Example 4: Array Filtering and Mapping +================================== +Positive numbers: 5, 10, 15, 20 +Doubled numbers: -10, 0, 10, 20, 30, 40 + +Example 5: Array Reduction +======================== +Product of all values: 59.0625 +Sum of squares: 39.5 +``` + +### Char Operations Output + +When you run `char_operations.php`, you'll see output similar to this: + +``` +Example 1: Basic Char Operations +============================ +Char 1: A +Char 2: b +Char 1 as string: A +Char 1 to lowercase: a +Char 2 to uppercase: B + +Example 2: Character Type Checking +============================== +Is 'A' a letter? Yes +Is 'A' uppercase? Yes +Is 'A' lowercase? No + +Is '5' a digit? Yes +Is '5' a letter? No + +Is '@' a letter? No +Is '@' a digit? No + +Example 3: ASCII Operations +======================== +ASCII code of 'A': 65 +Char from ASCII 65: A +ASCII 32 (space): ' ' +ASCII 10 (newline): ' +' + +Example 4: Character Comparison +=========================== +Is 'A' equal to 'A'? Yes +Is 'A' equal to 'B'? No +ASCII of 'A': 65 +ASCII of 'B': 66 + +Example 5: Error Handling +====================== +Error creating Char with multiple characters: Char must be a single character. +Error creating Char from invalid ASCII: ASCII value must be between 0 and 255. + +Example 6: Character Transformation Chain +==================================== +Original: a +To uppercase: A +Back to lowercase: a +ASCII code: 97 +Back to char: a +``` + +### Boolean Operations Output + +When you run `boolean_operations.php`, you'll see output similar to this: + +``` +Example 1: Basic Boolean Operations +============================== +True value: true +False value: false +True as string: true +False as string: false + +Example 2: Boolean Logic Operations +================================ +True AND True: true +True AND False: false +False AND False: false + +True OR True: true +True OR False: true +False OR False: false + +NOT True: false +NOT False: true + +Example 3: Boolean Comparison +========================== +Is true equal to true? Yes +Is true equal to false? No +Is false equal to false? Yes + +Example 4: Boolean Type Conversion +============================== +From integer 1: true +From string 'true': true +From float 1.0: true + +To integer: 1 +To string: true +To float: 1.0 + +Example 5: Error Handling +====================== +Error creating Boolean from invalid string: Invalid boolean string value. +Error creating Boolean from invalid integer: Invalid boolean integer value. + +Example 6: Boolean Chain Operations +================================ +Chain 1 (true AND false OR true NOT): false +Chain 2 (false OR true AND true NOT): false +``` + +### String Operations Output + +When you run `string_operations.php`, you'll see output similar to this: + +``` +Example 1: Basic String Operations +============================== +String 1: Hello +String 2: World +Concatenated: HelloWorld +Length of String 1: 5 + +Example 2: String Case Operations +============================== +Original: Hello World +Uppercase: HELLO WORLD +Lowercase: hello world +Title Case: Hello World + +Example 3: String Search and Replace +================================ +Contains 'World'? Yes +Starts with 'Hello'? Yes +Ends with 'PHP'? Yes +Replaced 'Hello' with 'Hi': Hi World, Hi PHP + +Example 4: String Trimming and Padding +================================== +Original: ' Hello World ' +Trimmed: 'Hello World' +Left Trimmed: 'Hello World ' +Right Trimmed: ' Hello World' +Left Padded: '***Hello World' +Right Padded: 'Hello World***' + +Example 5: String Splitting and Joining +=================================== +Split by comma: +- Hello +- World +- PHP + +Joined with space: Hello World PHP + +Example 6: String Comparison and Validation +====================================== +Is 'Hello' equal to 'Hello'? Yes +Is 'Hello' equal to 'World'? No + +Is 'Hello123' alphanumeric? Yes +Is 'Hello123' alphabetic? No +Is '123' numeric? Yes + +Example 7: String Substring and Character Access +========================================== +Original: Hello World +Substring(0, 5): Hello +Substring(6): World + +Character at index 0: H +Character at index 6: W + +Example 8: String Formatting +======================== +Formatted: Hello, John! Welcome to PHP. +Formatted with names: Hello, John! Your age is 25. +``` + +## Understanding the Examples + +Each example is designed to demonstrate specific features and behaviors of the library: + +### Integer Examples +1. **Basic Operations**: Shows how to perform basic arithmetic with type-safe integers +2. **Range Validation**: Demonstrates how the library prevents values outside the valid range +3. **Overflow Handling**: Shows how the library prevents integer overflow +4. **Comparison Operations**: Demonstrates various comparison methods +5. **Different Types**: Shows how to work with different integer types +6. **Division Operations**: Demonstrates proper handling of division and modulo operations + +### Float Examples +1. **Basic Operations**: Shows arithmetic operations with floating-point numbers +2. **Precision Comparison**: Demonstrates the difference between Float32 and Float64 precision +3. **Special Values**: Shows handling of special floating-point values +4. **Comparison Operations**: Demonstrates floating-point comparisons +5. **Rounding Operations**: Shows various rounding methods +6. **Mathematical Functions**: Demonstrates trigonometric and other mathematical functions + +### Array Examples +1. **Int8 Arrays**: Shows working with arrays of Int8 values +2. **Float32 Arrays**: Demonstrates array operations with Float32 values +3. **Mixed Types**: Shows working with different types in arrays +4. **Filtering and Mapping**: Demonstrates array manipulation with typed values +5. **Reduction**: Shows how to use array reduction with typed values + +### Char Examples +1. **Basic Operations**: Shows basic character operations and string conversion +2. **Type Checking**: Demonstrates character classification methods +3. **ASCII Operations**: Shows conversion between characters and ASCII codes +4. **Comparison**: Demonstrates character comparison methods +5. **Error Handling**: Shows handling of invalid inputs +6. **Transformation**: Demonstrates chaining of character transformations + +### Boolean Examples +1. **Basic Operations**: Shows basic boolean operations and string conversion +2. **Logic Operations**: Demonstrates boolean logic operations (AND, OR, NOT) +3. **Comparison**: Shows boolean comparison methods +4. **Type Conversion**: Demonstrates conversion between boolean and other types +5. **Error Handling**: Shows handling of invalid inputs +6. **Chain Operations**: Demonstrates chaining of boolean operations + +### String Examples +1. **Basic Operations**: Shows basic string operations and concatenation +2. **Case Operations**: Demonstrates string case manipulation methods +3. **Search and Replace**: Shows string search and replacement operations +4. **Trimming and Padding**: Demonstrates string trimming and padding methods +5. **Splitting and Joining**: Shows string splitting and joining operations +6. **Comparison and Validation**: Demonstrates string comparison and validation methods +7. **Substring and Character Access**: Shows substring and character access operations +8. **Formatting**: Demonstrates string formatting with placeholders + +## Error Handling + +The examples include proper error handling to demonstrate how the library handles various error conditions: + +### Integer Errors +- `OutOfRangeException`: Thrown when a value is outside the valid range +- `OverflowException`: Thrown when an operation would result in a value too large +- `UnderflowException`: Thrown when an operation would result in a value too small +- `DivisionByZeroError`: Thrown when attempting to divide by zero +- `UnexpectedValueException`: Thrown when division results in a non-integer value + +### Float Errors +- `OutOfRangeException`: Thrown when a value is outside the valid range +- `DivisionByZeroError`: Thrown when attempting to divide by zero +- `InvalidArgumentException`: Thrown when invalid arguments are provided to methods + +### Array Errors +- `TypeError`: Thrown when array operations involve incompatible types +- `OutOfRangeException`: Thrown when array operations result in values outside valid ranges +- `OverflowException`: Thrown when array operations result in values too large +- `UnderflowException`: Thrown when array operations result in values too small + +### Char Errors +- `InvalidArgumentException`: Thrown when creating a Char with multiple characters +- `InvalidArgumentException`: Thrown when creating a Char from invalid ASCII values + +### Boolean Errors +- `InvalidArgumentException`: Thrown when creating a Boolean from invalid string +- `InvalidArgumentException`: Thrown when creating a Boolean from invalid integer +- `InvalidArgumentException`: Thrown when creating a Boolean from invalid float + +### String Errors +- `InvalidArgumentException`: Thrown when creating a String with invalid input +- `OutOfRangeException`: Thrown when accessing invalid string indices +- `InvalidArgumentException`: Thrown when providing invalid arguments to string methods + +## Contributing + +Feel free to add more examples to demonstrate other features of the library. When adding new examples: + +1. Create a new PHP file with a descriptive name +2. Include proper error handling +3. Add comments explaining the purpose of the example +4. Update this README to include information about your new example \ No newline at end of file diff --git a/examples/array_operations.php b/examples/array_operations.php new file mode 100644 index 0000000..1a99988 --- /dev/null +++ b/examples/array_operations.php @@ -0,0 +1,193 @@ +add($number); + } + + echo "Sum of numbers: " . $sum->getValue() . "\n"; + + // Find maximum value + $max = $numbers[0]; + foreach ($numbers as $number) { + if ($number->greaterThan($max)) { + $max = $number; + } + } + + echo "Maximum value: " . $max->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: Array Operations with Float32 + */ +echo "\nExample 2: Array Operations with Float32\n"; +echo "====================================\n"; + +try { + // Create an array of Float32 values + $temperatures = [ + new Float32(23.5), + new Float32(24.8), + new Float32(22.3), + new Float32(25.1), + new Float32(23.9) + ]; + + // Calculate average temperature + $sum = new Float32(0.0); + foreach ($temperatures as $temp) { + $sum = $sum->add($temp); + } + $average = $sum->divide(new Float32(count($temperatures))); + + echo "Average temperature: " . $average->getValue() . "°C\n"; + + // Find temperatures above average + echo "Temperatures above average:\n"; + foreach ($temperatures as $temp) { + if ($temp->greaterThan($average)) { + echo "- " . $temp->getValue() . "°C\n"; + } + } +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: Mixed Type Arrays + */ +echo "\nExample 3: Mixed Type Arrays\n"; +echo "=========================\n"; + +try { + // Create arrays of different types + $integers = [ + new Int8(1), + new Int8(2), + new Int8(3) + ]; + + $floats = [ + new Float32(1.5), + new Float32(2.5), + new Float32(3.5) + ]; + + // Convert integers to floats + $convertedFloats = array_map( + fn (Int8 $int) => new Float32($int->getValue()), + $integers + ); + + echo "Original integers: " . implode(', ', array_map(fn (Int8 $int) => $int->getValue(), $integers)) . "\n"; + echo "Converted to floats: " . implode(', ', array_map(fn (Float32 $float) => $float->getValue(), $convertedFloats)) . "\n"; + + // Add corresponding values + $sums = []; + for ($i = 0; $i < count($integers); $i++) { + $sums[] = $floats[$i]->add(new Float32($integers[$i]->getValue())); + } + + echo "Sums of corresponding values: " . implode(', ', array_map(fn (Float32 $float) => $float->getValue(), $sums)) . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: Array Filtering and Mapping + */ +echo "\nExample 4: Array Filtering and Mapping\n"; +echo "==================================\n"; + +try { + // Create an array of Int8 values + $numbers = [ + new Int8(-5), + new Int8(0), + new Int8(5), + new Int8(10), + new Int8(15), + new Int8(20) + ]; + + // Filter positive numbers + $positiveNumbers = array_filter( + $numbers, + fn (Int8 $num) => $num->greaterThan(new Int8(0)) + ); + + echo "Positive numbers: " . implode(', ', array_map(fn (Int8 $num) => $num->getValue(), $positiveNumbers)) . "\n"; + + // Double each number + $doubledNumbers = array_map( + fn (Int8 $num) => $num->multiply(new Int8(2)), + $numbers + ); + + echo "Doubled numbers: " . implode(', ', array_map(fn (Int8 $num) => $num->getValue(), $doubledNumbers)) . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 5: Array Reduction + */ +echo "\nExample 5: Array Reduction\n"; +echo "========================\n"; + +try { + // Create an array of Float32 values + $values = [ + new Float32(1.5), + new Float32(2.5), + new Float32(3.5), + new Float32(4.5) + ]; + + // Calculate product of all values + $product = array_reduce( + $values, + fn (Float32 $carry, Float32 $item) => $carry->multiply($item), + new Float32(1.0) + ); + + echo "Product of all values: " . $product->getValue() . "\n"; + + // Calculate sum of squares + $sumOfSquares = array_reduce( + $values, + fn (Float32 $carry, Float32 $item) => $carry->add($item->multiply($item)), + new Float32(0.0) + ); + + echo "Sum of squares: " . $sumOfSquares->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/boolean_operations.php b/examples/boolean_operations.php new file mode 100644 index 0000000..4257689 --- /dev/null +++ b/examples/boolean_operations.php @@ -0,0 +1,138 @@ +getValue() ? "true" : "false") . "\n"; + echo "False value: " . ($false->getValue() ? "true" : "false") . "\n"; + + // Convert to string + echo "True as string: " . (string)$true . "\n"; + echo "False as string: " . (string)$false . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: Boolean Logic Operations + */ +echo "\nExample 2: Boolean Logic Operations\n"; +echo "================================\n"; + +try { + $true = new Boolean(true); + $false = new Boolean(false); + + // AND operation + echo "True AND True: " . ($true->and($true)->getValue() ? "true" : "false") . "\n"; + echo "True AND False: " . ($true->and($false)->getValue() ? "true" : "false") . "\n"; + echo "False AND False: " . ($false->and($false)->getValue() ? "true" : "false") . "\n"; + + // OR operation + echo "\nTrue OR True: " . ($true->or($true)->getValue() ? "true" : "false") . "\n"; + echo "True OR False: " . ($true->or($false)->getValue() ? "true" : "false") . "\n"; + echo "False OR False: " . ($false->or($false)->getValue() ? "true" : "false") . "\n"; + + // NOT operation + echo "\nNOT True: " . ($true->not()->getValue() ? "true" : "false") . "\n"; + echo "NOT False: " . ($false->not()->getValue() ? "true" : "false") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: Boolean Comparison + */ +echo "\nExample 3: Boolean Comparison\n"; +echo "==========================\n"; + +try { + $true1 = new Boolean(true); + $true2 = new Boolean(true); + $false = new Boolean(false); + + echo "Is true equal to true? " . ($true1->equals($true2) ? "Yes" : "No") . "\n"; + echo "Is true equal to false? " . ($true1->equals($false) ? "Yes" : "No") . "\n"; + echo "Is false equal to false? " . ($false->equals($false) ? "Yes" : "No") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: Boolean Type Conversion + */ +echo "\nExample 4: Boolean Type Conversion\n"; +echo "==============================\n"; + +try { + // Convert from different types + $fromInt = Boolean::fromInt(1); + $fromString = Boolean::fromString("true"); + $fromFloat = Boolean::fromFloat(1.0); + + echo "From integer 1: " . ($fromInt->getValue() ? "true" : "false") . "\n"; + echo "From string 'true': " . ($fromString->getValue() ? "true" : "false") . "\n"; + echo "From float 1.0: " . ($fromFloat->getValue() ? "true" : "false") . "\n"; + + // Convert to different types + $bool = new Boolean(true); + echo "\nTo integer: " . $bool->toInt() . "\n"; + echo "To string: " . $bool->toString() . "\n"; + echo "To float: " . $bool->toFloat() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 5: Error Handling + */ +echo "\nExample 5: Error Handling\n"; +echo "======================\n"; + +try { + // Try to create a Boolean from invalid string + $invalidBool = Boolean::fromString("invalid"); +} catch (\InvalidArgumentException $e) { + echo "Error creating Boolean from invalid string: " . $e->getMessage() . "\n"; +} + +try { + // Try to create a Boolean from invalid integer + $invalidInt = Boolean::fromInt(2); +} catch (\InvalidArgumentException $e) { + echo "Error creating Boolean from invalid integer: " . $e->getMessage() . "\n"; +} + +/** + * Example 6: Boolean Chain Operations + */ +echo "\nExample 6: Boolean Chain Operations\n"; +echo "================================\n"; + +try { + $true = new Boolean(true); + $false = new Boolean(false); + + // Chain multiple operations + $result1 = $true->and($false)->or($true)->not(); + echo "Chain 1 (true AND false OR true NOT): " . ($result1->getValue() ? "true" : "false") . "\n"; + + $result2 = $false->or($true)->and($true)->not(); + echo "Chain 2 (false OR true AND true NOT): " . ($result2->getValue() ? "true" : "false") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/char_operations.php b/examples/char_operations.php new file mode 100644 index 0000000..49b64a0 --- /dev/null +++ b/examples/char_operations.php @@ -0,0 +1,141 @@ +getValue() . "\n"; + echo "Char 2: " . $char2->getValue() . "\n"; + + // Convert to string + echo "Char 1 as string: " . (string)$char1 . "\n"; + + // Case conversion + echo "Char 1 to lowercase: " . $char1->toLowerCase()->getValue() . "\n"; + echo "Char 2 to uppercase: " . $char2->toUpperCase()->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: Character Type Checking + */ +echo "\nExample 2: Character Type Checking\n"; +echo "==============================\n"; + +try { + $letter = new Char('A'); + $digit = new Char('5'); + $symbol = new Char('@'); + + echo "Is 'A' a letter? " . ($letter->isLetter() ? "Yes" : "No") . "\n"; + echo "Is 'A' uppercase? " . ($letter->isUpperCase() ? "Yes" : "No") . "\n"; + echo "Is 'A' lowercase? " . ($letter->isLowerCase() ? "Yes" : "No") . "\n"; + + echo "\nIs '5' a digit? " . ($digit->isDigit() ? "Yes" : "No") . "\n"; + echo "Is '5' a letter? " . ($digit->isLetter() ? "Yes" : "No") . "\n"; + + echo "\nIs '@' a letter? " . ($symbol->isLetter() ? "Yes" : "No") . "\n"; + echo "Is '@' a digit? " . ($symbol->isDigit() ? "Yes" : "No") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: ASCII Operations + */ +echo "\nExample 3: ASCII Operations\n"; +echo "========================\n"; + +try { + $char = new Char('A'); + + // Get ASCII code + $ascii = $char->toAscii(); + echo "ASCII code of 'A': " . $ascii . "\n"; + + // Create Char from ASCII + $newChar = Char::fromAscii($ascii); + echo "Char from ASCII " . $ascii . ": " . $newChar->getValue() . "\n"; + + // Try some other ASCII values + $space = Char::fromAscii(32); + echo "ASCII 32 (space): '" . $space->getValue() . "'\n"; + + $newline = Char::fromAscii(10); + echo "ASCII 10 (newline): '" . $newline->getValue() . "'\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: Character Comparison + */ +echo "\nExample 4: Character Comparison\n"; +echo "===========================\n"; + +try { + $char1 = new Char('A'); + $char2 = new Char('A'); + $char3 = new Char('B'); + + echo "Is 'A' equal to 'A'? " . ($char1->equals($char2) ? "Yes" : "No") . "\n"; + echo "Is 'A' equal to 'B'? " . ($char1->equals($char3) ? "Yes" : "No") . "\n"; + + // Compare ASCII values + echo "ASCII of 'A': " . $char1->toAscii() . "\n"; + echo "ASCII of 'B': " . $char3->toAscii() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 5: Error Handling + */ +echo "\nExample 5: Error Handling\n"; +echo "======================\n"; + +try { + // Try to create a Char with multiple characters + $invalidChar = new Char('AB'); +} catch (\InvalidArgumentException $e) { + echo "Error creating Char with multiple characters: " . $e->getMessage() . "\n"; +} + +try { + // Try to create a Char from invalid ASCII + $invalidAscii = Char::fromAscii(300); +} catch (\InvalidArgumentException $e) { + echo "Error creating Char from invalid ASCII: " . $e->getMessage() . "\n"; +} + +/** + * Example 6: Character Transformation Chain + */ +echo "\nExample 6: Character Transformation Chain\n"; +echo "=====================================\n"; + +try { + $char = new Char('a'); + + echo "Original: " . $char->getValue() . "\n"; + echo "To uppercase: " . $char->toUpperCase()->getValue() . "\n"; + echo "Back to lowercase: " . $char->toUpperCase()->toLowerCase()->getValue() . "\n"; + echo "ASCII code: " . $char->toAscii() . "\n"; + echo "Back to char: " . Char::fromAscii($char->toAscii())->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/comprehensive_example.php b/examples/comprehensive_example.php new file mode 100644 index 0000000..0c2e98f --- /dev/null +++ b/examples/comprehensive_example.php @@ -0,0 +1,173 @@ +getValue() . "\n"; +echo "Int32: " . $int32->getValue() . "\n"; +echo "UInt8: " . $uint8->getValue() . "\n"; +echo "Float32: " . $float32->getValue() . "\n\n"; + +// 2. Arithmetic Operations +echo "2. Arithmetic Operations:\n"; +echo "------------------------\n"; + +$result = $int8->add(new Int8(10)); +echo "Int8(42) + Int8(10) = " . $result->getValue() . "\n"; + +$result = $int32->multiply(new Int32(2)); +echo "Int32(1000) * Int32(2) = " . $result->getValue() . "\n\n"; + +// 3. Option Type +echo "3. Option Type:\n"; +echo "--------------\n"; + +$someValue = Option::some("Hello World"); +$noneValue = Option::none(); + +echo "Some value: " . $someValue . "\n"; +echo "None value: " . $noneValue . "\n"; + +$processed = $someValue + ->map(fn($value) => strtoupper($value)) + ->unwrapOr("DEFAULT"); + +echo "Processed: " . $processed . "\n\n"; + +// 4. Result Type +echo "4. Result Type:\n"; +echo "--------------\n"; + +$successResult = Result::ok("Operation successful"); +$errorResult = Result::err("Something went wrong"); + +echo "Success: " . $successResult . "\n"; +echo "Error: " . $errorResult . "\n"; + +$safeResult = Result::try(function () { + return new Int8(50); +}); + +if ($safeResult->isOk()) { + echo "Safe operation result: " . $safeResult->unwrap()->getValue() . "\n"; +} else { + echo "Safe operation failed: " . $safeResult->unwrapErr() . "\n"; +} + +echo "\n"; + +// 5. Dictionary +echo "5. Dictionary:\n"; +echo "-------------\n"; + +$dict = new Dictionary([ + 'name' => 'John Doe', + 'age' => 30, + 'email' => 'john@example.com' +]); + +echo "Dictionary size: " . $dict->size() . "\n"; +echo "Name: " . $dict->get('name') . "\n"; +echo "Keys: " . implode(', ', $dict->getKeys()) . "\n\n"; + +// 6. Struct +echo "6. Struct:\n"; +echo "----------\n"; + +$userStruct = new Struct([ + 'id' => ['type' => 'int', 'nullable' => false], + 'name' => ['type' => 'string', 'nullable' => false], + 'email' => ['type' => 'string', 'nullable' => true], +], [ + 'id' => 1, + 'name' => 'Jane Doe', + 'email' => 'jane@example.com' +]); + +echo "User ID: " . $userStruct->get('id') . "\n"; +echo "User Name: " . $userStruct->get('name') . "\n"; +echo "User Email: " . $userStruct->get('email') . "\n\n"; + +// 7. Union Type +echo "7. Union Type:\n"; +echo "-------------\n"; + +$union = new UnionType([ + 'string' => 'string', + 'int' => 'int', + 'float' => 'float' +]); + +$union->setValue('string', 'Hello Union'); +echo "Union active type: " . $union->getActiveType() . "\n"; +echo "Union value: " . $union->getValue() . "\n"; + +$union->setValue('int', 42); +echo "Union active type: " . $union->getActiveType() . "\n"; +echo "Union value: " . $union->getValue() . "\n\n"; + +// 8. Helper Functions +echo "8. Helper Functions:\n"; +echo "-------------------\n"; + +$int8Helper = int8(75); +$uint8Helper = uint8(150); +$float32Helper = float32(2.71828); +$someHelper = some("Helper function"); +$okHelper = ok("Success"); + +echo "Int8 helper: " . $int8Helper->getValue() . "\n"; +echo "UInt8 helper: " . $uint8Helper->getValue() . "\n"; +echo "Float32 helper: " . $float32Helper->getValue() . "\n"; +echo "Some helper: " . $someHelper . "\n"; +echo "Ok helper: " . $okHelper . "\n\n"; + +// 9. Serialization +echo "9. Serialization:\n"; +echo "----------------\n"; + +$json = $userStruct->toJson(); +echo "Struct JSON: " . $json . "\n"; + +$xml = $userStruct->toXml(); +echo "Struct XML: " . substr($xml, 0, 100) . "...\n\n"; + +// 10. Error Handling +echo "10. Error Handling:\n"; +echo "------------------\n"; + +try { + $invalidInt8 = new Int8(1000); // This will throw OutOfRangeException +} catch (\OutOfRangeException $e) { + echo "Caught OutOfRangeException: " . $e->getMessage() . "\n"; +} + +try { + $overflow = $int8->add(new Int8(100)); // This will throw OverflowException +} catch (\OverflowException $e) { + echo "Caught OverflowException: " . $e->getMessage() . "\n"; +} + +echo "\n=== Example Complete ===\n"; diff --git a/examples/dictionary.php b/examples/dictionary.php index 3d1cf49..fcb1c72 100644 --- a/examples/dictionary.php +++ b/examples/dictionary.php @@ -1,5 +1,7 @@ getSuggestion(); // Output: "Verify the URL or resource identifier." \ No newline at end of file +echo $status->getSuggestion(); // Output: "Verify the URL or resource identifier." diff --git a/examples/float_operations.php b/examples/float_operations.php new file mode 100644 index 0000000..8c67784 --- /dev/null +++ b/examples/float_operations.php @@ -0,0 +1,126 @@ +add($float2); + $difference = $float1->subtract($float2); + $product = $float1->multiply($float2); + $quotient = $float1->divide($float2); + + echo "Float 1: " . $float1->getValue() . "\n"; + echo "Float 2: " . $float2->getValue() . "\n"; + echo "Sum: " . $sum->getValue() . "\n"; + echo "Difference: " . $difference->getValue() . "\n"; + echo "Product: " . $product->getValue() . "\n"; + echo "Quotient: " . $quotient->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: Precision Comparison + */ +echo "\nExample 2: Precision Comparison\n"; +echo "===========================\n"; + +try { + $float32 = new Float32(1.23456789); + $float64 = new Float64(1.23456789); + + echo "Float32 value: " . $float32->getValue() . "\n"; + echo "Float64 value: " . $float64->getValue() . "\n"; + echo "Note the difference in precision between Float32 and Float64\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: Special Values + */ +echo "\nExample 3: Special Values\n"; +echo "=====================\n"; + +try { + // Infinity + $infinity = new Float64(INF); + echo "Infinity: " . $infinity->getValue() . "\n"; + + // NaN + $nan = new Float64(NAN); + echo "NaN: " . $nan->getValue() . "\n"; + + // Very small number + $small = new Float64(1.0E-45); + echo "Very small number: " . $small->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: Comparison Operations + */ +echo "\nExample 4: Comparison Operations\n"; +echo "=============================\n"; + +try { + $num1 = new Float32(3.14); + $num2 = new Float32(2.71); + + echo "Is 3.14 greater than 2.71? " . ($num1->greaterThan($num2) ? "Yes" : "No") . "\n"; + echo "Is 3.14 less than 2.71? " . ($num1->lessThan($num2) ? "Yes" : "No") . "\n"; + echo "Is 3.14 equal to 2.71? " . ($num1->equals($num2) ? "Yes" : "No") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 5: Rounding Operations + */ +echo "\nExample 5: Rounding Operations\n"; +echo "==========================\n"; + +try { + $number = new Float32(3.14159); + + echo "Original number: " . $number->getValue() . "\n"; + echo "Rounded to 2 decimal places: " . $number->round(2)->getValue() . "\n"; + echo "Ceiling: " . $number->ceil()->getValue() . "\n"; + echo "Floor: " . $number->floor()->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 6: Mathematical Functions + */ +echo "\nExample 6: Mathematical Functions\n"; +echo "=============================\n"; + +try { + $number = new Float32(0.5); + + echo "Original number: " . $number->getValue() . "\n"; + echo "Sine: " . $number->sin()->getValue() . "\n"; + echo "Cosine: " . $number->cos()->getValue() . "\n"; + echo "Tangent: " . $number->tan()->getValue() . "\n"; + echo "Square root: " . $number->sqrt()->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/int128_operations.php b/examples/int128_operations.php new file mode 100644 index 0000000..0e98368 --- /dev/null +++ b/examples/int128_operations.php @@ -0,0 +1,103 @@ +getValue() . "\n"; + + // Addition + $sum = $number->add(new Int128('7')); + echo "Addition: " . $sum->getValue() . "\n"; + + // Subtraction + $diff = $number->subtract(new Int128('100')); + echo "Subtraction: " . $diff->getValue() . "\n"; + + // Multiplication + $product = $number->multiply(new Int128('2')); + echo "Multiplication: " . $product->getValue() . "\n"; + + // Division + $quotient = $number->divide(new Int128('2')); + echo "Division: " . $quotient->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 1: " . $e->getMessage() . "\n"; +} + +// Example 2: Range Validation +echo "\nExample 2: Range Validation\n"; +try { + // Valid range + $valid = new Int128('170141183460469231731687303715884105727'); + echo "Valid maximum: " . $valid->getValue() . "\n"; + + // Invalid range (should throw OutOfRangeException) + $invalid = new Int128('170141183460469231731687303715884105728'); + echo "This line should not be reached\n"; +} catch (OutOfRangeException $e) { + echo "Caught OutOfRangeException: " . $e->getMessage() . "\n"; +} + +// Example 3: Arithmetic Operations with Negative Numbers +echo "\nExample 3: Arithmetic with Negative Numbers\n"; +try { + $negative = new Int128('-170141183460469231731687303715884105720'); + echo "Negative number: " . $negative->getValue() . "\n"; + + // Addition with negative + $sum = $negative->add(new Int128('100')); + echo "Addition with negative: " . $sum->getValue() . "\n"; + + // Subtraction with negative + $diff = $negative->subtract(new Int128('100')); + echo "Subtraction with negative: " . $diff->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 3: " . $e->getMessage() . "\n"; +} + +// Example 4: Comparison Operations +echo "\nExample 4: Comparison Operations\n"; +try { + $a = new Int128('170141183460469231731687303715884105720'); + $b = new Int128('170141183460469231731687303715884105620'); + + echo "A: " . $a->getValue() . "\n"; + echo "B: " . $b->getValue() . "\n"; + + echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n"; + echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n"; + echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n"; +} catch (Exception $e) { + echo "Error in Example 4: " . $e->getMessage() . "\n"; +} + +// Example 5: Overflow/Underflow Handling +echo "\nExample 5: Overflow/Underflow Handling\n"; +try { + $max = new Int128('170141183460469231731687303715884105727'); + echo "Maximum value: " . $max->getValue() . "\n"; + + // This should cause an overflow + $overflow = $max->add(new Int128('1')); + echo "This line should not be reached\n"; +} catch (OverflowException $e) { + echo "Caught OverflowException: " . $e->getMessage() . "\n"; +} + +try { + $min = new Int128('-170141183460469231731687303715884105728'); + echo "Minimum value: " . $min->getValue() . "\n"; + + // This should cause an underflow + $underflow = $min->subtract(new Int128('1')); + echo "This line should not be reached\n"; +} catch (UnderflowException $e) { + echo "Caught UnderflowException: " . $e->getMessage() . "\n"; +} diff --git a/examples/int64_operations.php b/examples/int64_operations.php new file mode 100644 index 0000000..0623055 --- /dev/null +++ b/examples/int64_operations.php @@ -0,0 +1,103 @@ +getValue() . "\n"; + + // Addition + $sum = $number->add(new Int64('7')); + echo "Addition: " . $sum->getValue() . "\n"; + + // Subtraction + $diff = $number->subtract(new Int64('100')); + echo "Subtraction: " . $diff->getValue() . "\n"; + + // Multiplication + $product = $number->multiply(new Int64('2')); + echo "Multiplication: " . $product->getValue() . "\n"; + + // Division + $quotient = $number->divide(new Int64('2')); + echo "Division: " . $quotient->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 1: " . $e->getMessage() . "\n"; +} + +// Example 2: Range Validation +echo "\nExample 2: Range Validation\n"; +try { + // Valid range + $valid = new Int64('9223372036854775807'); + echo "Valid maximum: " . $valid->getValue() . "\n"; + + // Invalid range (should throw OutOfRangeException) + $invalid = new Int64('9223372036854775808'); + echo "This line should not be reached\n"; +} catch (OutOfRangeException $e) { + echo "Caught OutOfRangeException: " . $e->getMessage() . "\n"; +} + +// Example 3: Arithmetic Operations with Negative Numbers +echo "\nExample 3: Arithmetic with Negative Numbers\n"; +try { + $negative = new Int64('-9223372036854775800'); + echo "Negative number: " . $negative->getValue() . "\n"; + + // Addition with negative + $sum = $negative->add(new Int64('100')); + echo "Addition with negative: " . $sum->getValue() . "\n"; + + // Subtraction with negative + $diff = $negative->subtract(new Int64('100')); + echo "Subtraction with negative: " . $diff->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 3: " . $e->getMessage() . "\n"; +} + +// Example 4: Comparison Operations +echo "\nExample 4: Comparison Operations\n"; +try { + $a = new Int64('9223372036854775800'); + $b = new Int64('9223372036854775700'); + + echo "A: " . $a->getValue() . "\n"; + echo "B: " . $b->getValue() . "\n"; + + echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n"; + echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n"; + echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n"; +} catch (Exception $e) { + echo "Error in Example 4: " . $e->getMessage() . "\n"; +} + +// Example 5: Overflow/Underflow Handling +echo "\nExample 5: Overflow/Underflow Handling\n"; +try { + $max = new Int64('9223372036854775807'); + echo "Maximum value: " . $max->getValue() . "\n"; + + // This should cause an overflow + $overflow = $max->add(new Int64('1')); + echo "This line should not be reached\n"; +} catch (OverflowException $e) { + echo "Caught OverflowException: " . $e->getMessage() . "\n"; +} + +try { + $min = new Int64('-9223372036854775808'); + echo "Minimum value: " . $min->getValue() . "\n"; + + // This should cause an underflow + $underflow = $min->subtract(new Int64('1')); + echo "This line should not be reached\n"; +} catch (UnderflowException $e) { + echo "Caught UnderflowException: " . $e->getMessage() . "\n"; +} diff --git a/examples/integer_operations.php b/examples/integer_operations.php new file mode 100644 index 0000000..75e8a00 --- /dev/null +++ b/examples/integer_operations.php @@ -0,0 +1,122 @@ +add($number2); + $difference = $number1->subtract($number2); + $product = $number1->multiply($number2); + $quotient = $number1->divide($number2); + + echo "Number 1: " . $number1->getValue() . "\n"; + echo "Number 2: " . $number2->getValue() . "\n"; + echo "Sum: " . $sum->getValue() . "\n"; + echo "Difference: " . $difference->getValue() . "\n"; + echo "Product: " . $product->getValue() . "\n"; + echo "Quotient: " . $quotient->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: Range Validation + */ +echo "\nExample 2: Range Validation\n"; +echo "========================\n"; + +try { + // This will throw OutOfRangeException + $invalidNumber = new Int8(200); +} catch (\OutOfRangeException $e) { + echo "Range validation works: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: Overflow Handling + */ +echo "\nExample 3: Overflow Handling\n"; +echo "=========================\n"; + +try { + $maxInt8 = new Int8(Int8::MAX_VALUE); + $overflow = $maxInt8->add(new Int8(1)); +} catch (\OverflowException $e) { + echo "Overflow protection works: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: Comparison Operations + */ +echo "\nExample 4: Comparison Operations\n"; +echo "=============================\n"; + +$num1 = new Int8(50); +$num2 = new Int8(30); + +echo "Is 50 greater than 30? " . ($num1->greaterThan($num2) ? "Yes" : "No") . "\n"; +echo "Is 50 less than 30? " . ($num1->lessThan($num2) ? "Yes" : "No") . "\n"; +echo "Is 50 equal to 30? " . ($num1->equals($num2) ? "Yes" : "No") . "\n"; + +/** + * Example 5: Working with Different Integer Types + */ +echo "\nExample 5: Working with Different Integer Types\n"; +echo "===========================================\n"; + +try { + $int8 = new Int8(100); + $int16 = new Int16(1000); + $uint8 = new UInt8(200); + + echo "Int8 value: " . $int8->getValue() . "\n"; + echo "Int16 value: " . $int16->getValue() . "\n"; + echo "UInt8 value: " . $uint8->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 6: Division and Modulo Operations + */ +echo "\nExample 6: Division and Modulo Operations\n"; +echo "=====================================\n"; + +try { + $dividend = new Int8(50); + $divisor = new Int8(3); + + // This will throw UnexpectedValueException because 50/3 is not an integer + $result = $dividend->divide($divisor); +} catch (\UnexpectedValueException $e) { + echo "Division validation works: " . $e->getMessage() . "\n"; +} + +try { + $dividend = new Int8(50); + $divisor = new Int8(5); + + $quotient = $dividend->divide($divisor); + $remainder = $dividend->mod($divisor); + + echo "50 divided by 5 = " . $quotient->getValue() . "\n"; + echo "50 modulo 5 = " . $remainder->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/json.php b/examples/json.php index b867136..0c4986a 100644 --- a/examples/json.php +++ b/examples/json.php @@ -31,14 +31,14 @@ 'output' => "Json1: " . $json1->getJson() . "\nJson2: " . $json2->getJson(), ]; -// // 2. Compare Json instances -// $areEqual = $json1->compareWith($json2) ? 'Yes' : 'No'; -// $examples[] = [ -// 'title' => 'Compare Json Instances', -// 'description' => 'We compare json1 and json2 to check if they are identical.', -// 'code' => "\$areEqual = \$json1->compareWith(\$json2) ? 'Yes' : 'No';", -// 'output' => "Are Json1 and Json2 identical? " . $areEqual, -// ]; + // // 2. Compare Json instances + // $areEqual = $json1->compareWith($json2) ? 'Yes' : 'No'; + // $examples[] = [ + // 'title' => 'Compare Json Instances', + // 'description' => 'We compare json1 and json2 to check if they are identical.', + // 'code' => "\$areEqual = \$json1->compareWith(\$json2) ? 'Yes' : 'No';", + // 'output' => "Are Json1 and Json2 identical? " . $areEqual, + // ]; // 3. Serialize Json to Array $array1 = $json1->toArray(); @@ -58,7 +58,7 @@ 'output' => "Json from Array: " . $jsonFromArray->getJson(), ]; -// // 5. Compress Json1 using HuffmanEncoding + // // 5. Compress Json1 using HuffmanEncoding $huffmanEncoder = new HuffmanEncoding(); $compressed = $json1->compress($huffmanEncoder); $examples[] = [ @@ -68,7 +68,7 @@ 'output' => "Compressed Json1 (hex): " . bin2hex($compressed), ]; -// // 6. Decompress the previously compressed data + // // 6. Decompress the previously compressed data $decompressedJson = $json1->decompress($huffmanEncoder, $compressed); $examples[] = [ 'title' => 'Decompress the Compressed Data', @@ -85,7 +85,7 @@ 'code' => "\$isMatch = (\$json1->toArray() === \$decompressedJson->toArray()) ? 'Yes' : 'No';", 'output' => "Does decompressed Json match original Json1? " . $isMatch, ]; -// + // // 8. Update Json1 by adding a new user $updatedJson1 = $json1->update('users', array_merge($json1->toArray()['users'], [['id' => 5, 'name' => 'Eve']])); $examples[] = [ @@ -94,7 +94,7 @@ 'code' => "\$updatedJson1 = \$json1->update('users', array_merge(\$json1->toArray()['users'], [['id' => 5, 'name' => 'Eve']]));", 'output' => "Updated Json1: " . $updatedJson1->getJson(), ]; -// + // // 9. Remove a user from updated Json1 $modifiedJson1 = $updatedJson1->remove('users', 2); // Assuming remove method removes by 'id' or index $examples[] = [ diff --git a/examples/listdata.php b/examples/listdata.php index 9bdbd34..c533ae3 100644 --- a/examples/listdata.php +++ b/examples/listdata.php @@ -1,5 +1,7 @@ getValue() . "\n"; + echo "String 2: " . $str2->getValue() . "\n"; + + // String concatenation + $concatenated = $str1->concat($str2); + echo "Concatenated: " . $concatenated->getValue() . "\n"; + + // String length + echo "Length of String 1: " . $str1->length() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 2: String Case Operations + */ +echo "\nExample 2: String Case Operations\n"; +echo "==============================\n"; + +try { + $str = new TypedString("Hello World"); + + echo "Original: " . $str->getValue() . "\n"; + echo "Uppercase: " . $str->toUpperCase()->getValue() . "\n"; + echo "Lowercase: " . $str->toLowerCase()->getValue() . "\n"; + echo "Title Case: " . $str->toTitleCase()->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 3: String Search and Replace + */ +echo "\nExample 3: String Search and Replace\n"; +echo "================================\n"; + +try { + $str = new TypedString("Hello World, Hello PHP"); + + // Search operations + echo "Contains 'World'? " . ($str->contains(new TypedString("World")) ? "Yes" : "No") . "\n"; + echo "Starts with 'Hello'? " . ($str->startsWith(new TypedString("Hello")) ? "Yes" : "No") . "\n"; + echo "Ends with 'PHP'? " . ($str->endsWith(new TypedString("PHP")) ? "Yes" : "No") . "\n"; + + // Replace operations + $replaced = $str->replace(new TypedString("Hello"), new TypedString("Hi")); + echo "Replaced 'Hello' with 'Hi': " . $replaced->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 4: String Trimming and Padding + */ +echo "\nExample 4: String Trimming and Padding\n"; +echo "==================================\n"; + +try { + $str = new TypedString(" Hello World "); + + echo "Original: '" . $str->getValue() . "'\n"; + echo "Trimmed: '" . $str->trim()->getValue() . "'\n"; + echo "Left Trimmed: '" . $str->trimLeft()->getValue() . "'\n"; + echo "Right Trimmed: '" . $str->trimRight()->getValue() . "'\n"; + + // Padding + $padded = $str->trim()->padLeft(15, new TypedString("*")); + echo "Left Padded: '" . $padded->getValue() . "'\n"; + + $padded = $str->trim()->padRight(15, new TypedString("*")); + echo "Right Padded: '" . $padded->getValue() . "'\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 5: String Splitting and Joining + */ +echo "\nExample 5: String Splitting and Joining\n"; +echo "===================================\n"; + +try { + $str = new TypedString("Hello,World,PHP"); + + // Split string + $parts = $str->split(new TypedString(",")); + echo "Split by comma:\n"; + foreach ($parts as $part) { + echo "- " . $part->getValue() . "\n"; + } + + // Join strings + $joined = TypedString::join(new TypedString(" "), [ + new TypedString("Hello"), + new TypedString("World"), + new TypedString("PHP") + ]); + echo "\nJoined with space: " . $joined->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 6: String Comparison and Validation + */ +echo "\nExample 6: String Comparison and Validation\n"; +echo "======================================\n"; + +try { + $str1 = new TypedString("Hello"); + $str2 = new TypedString("Hello"); + $str3 = new TypedString("World"); + + // Comparison + echo "Is 'Hello' equal to 'Hello'? " . ($str1->equals($str2) ? "Yes" : "No") . "\n"; + echo "Is 'Hello' equal to 'World'? " . ($str1->equals($str3) ? "Yes" : "No") . "\n"; + + // Validation + echo "\nIs 'Hello123' alphanumeric? " . (TypedString::isAlphanumeric(new TypedString("Hello123")) ? "Yes" : "No") . "\n"; + echo "Is 'Hello123' alphabetic? " . (TypedString::isAlphabetic(new TypedString("Hello123")) ? "Yes" : "No") . "\n"; + echo "Is '123' numeric? " . (TypedString::isNumeric(new TypedString("123")) ? "Yes" : "No") . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 7: String Substring and Character Access + */ +echo "\nExample 7: String Substring and Character Access\n"; +echo "==========================================\n"; + +try { + $str = new TypedString("Hello World"); + + // Substring + echo "Original: " . $str->getValue() . "\n"; + echo "Substring(0, 5): " . $str->substring(0, 5)->getValue() . "\n"; + echo "Substring(6): " . $str->substring(6)->getValue() . "\n"; + + // Character access + echo "\nCharacter at index 0: " . $str->charAt(0)->getValue() . "\n"; + echo "Character at index 6: " . $str->charAt(6)->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} + +/** + * Example 8: String Formatting + */ +echo "\nExample 8: String Formatting\n"; +echo "========================\n"; + +try { + // Format with placeholders + $formatted = TypedString::format( + new TypedString("Hello, {0}! Welcome to {1}."), + [new TypedString("John"), new TypedString("PHP")] + ); + echo "Formatted: " . $formatted->getValue() . "\n"; + + // Format with named placeholders + $formatted = TypedString::formatNamed( + new TypedString("Hello, {name}! Your age is {age}."), + [ + "name" => new TypedString("John"), + "age" => new TypedString("25") + ] + ); + echo "Formatted with names: " . $formatted->getValue() . "\n"; +} catch (\Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} diff --git a/examples/struct.php b/examples/struct.php index 264f3a1..667a353 100644 --- a/examples/struct.php +++ b/examples/struct.php @@ -1,5 +1,7 @@ getValue() . "\n"; + + // Addition + $sum = $number->add(new UInt128('5')); + echo "Addition: " . $sum->getValue() . "\n"; + + // Subtraction + $diff = $number->subtract(new UInt128('100')); + echo "Subtraction: " . $diff->getValue() . "\n"; + + // Multiplication + $product = $number->multiply(new UInt128('2')); + echo "Multiplication: " . $product->getValue() . "\n"; + + // Division + $quotient = $number->divide(new UInt128('2')); + echo "Division: " . $quotient->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 1: " . $e->getMessage() . "\n"; +} + +// Example 2: Range Validation +echo "\nExample 2: Range Validation\n"; +try { + // Valid range + $valid = new UInt128('340282366920938463463374607431768211455'); + echo "Valid maximum: " . $valid->getValue() . "\n"; + + // Invalid range (should throw OutOfRangeException) + $invalid = new UInt128('340282366920938463463374607431768211456'); + echo "This line should not be reached\n"; +} catch (OutOfRangeException $e) { + echo "Caught OutOfRangeException: " . $e->getMessage() . "\n"; +} + +// Example 3: Zero and Small Numbers +echo "\nExample 3: Zero and Small Numbers\n"; +try { + $zero = new UInt128('0'); + echo "Zero: " . $zero->getValue() . "\n"; + + // Addition with zero + $sum = $zero->add(new UInt128('100')); + echo "Addition with zero: " . $sum->getValue() . "\n"; + + // Subtraction from small number + $small = new UInt128('100'); + $diff = $small->subtract(new UInt128('50')); + echo "Subtraction from small number: " . $diff->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 3: " . $e->getMessage() . "\n"; +} + +// Example 4: Comparison Operations +echo "\nExample 4: Comparison Operations\n"; +try { + $a = new UInt128('340282366920938463463374607431768211450'); + $b = new UInt128('340282366920938463463374607431768211400'); + + echo "A: " . $a->getValue() . "\n"; + echo "B: " . $b->getValue() . "\n"; + + echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n"; + echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n"; + echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n"; +} catch (Exception $e) { + echo "Error in Example 4: " . $e->getMessage() . "\n"; +} + +// Example 5: Overflow/Underflow Handling +echo "\nExample 5: Overflow/Underflow Handling\n"; +try { + $max = new UInt128('340282366920938463463374607431768211455'); + echo "Maximum value: " . $max->getValue() . "\n"; + + // This should cause an overflow + $overflow = $max->add(new UInt128('1')); + echo "This line should not be reached\n"; +} catch (OverflowException $e) { + echo "Caught OverflowException: " . $e->getMessage() . "\n"; +} + +try { + $min = new UInt128('0'); + echo "Minimum value: " . $min->getValue() . "\n"; + + // This should cause an underflow + $underflow = $min->subtract(new UInt128('1')); + echo "This line should not be reached\n"; +} catch (UnderflowException $e) { + echo "Caught UnderflowException: " . $e->getMessage() . "\n"; +} diff --git a/examples/uint64_operations.php b/examples/uint64_operations.php new file mode 100644 index 0000000..6eb58e1 --- /dev/null +++ b/examples/uint64_operations.php @@ -0,0 +1,104 @@ +getValue() . "\n"; + + // Addition + $sum = $number->add(new UInt64('5')); + echo "Addition: " . $sum->getValue() . "\n"; + + // Subtraction + $diff = $number->subtract(new UInt64('100')); + echo "Subtraction: " . $diff->getValue() . "\n"; + + // Multiplication + $product = $number->multiply(new UInt64('2')); + echo "Multiplication: " . $product->getValue() . "\n"; + + // Division + $quotient = $number->divide(new UInt64('2')); + echo "Division: " . $quotient->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 1: " . $e->getMessage() . "\n"; +} + +// Example 2: Range Validation +echo "\nExample 2: Range Validation\n"; +try { + // Valid range + $valid = new UInt64('18446744073709551615'); + echo "Valid maximum: " . $valid->getValue() . "\n"; + + // Invalid range (should throw OutOfRangeException) + $invalid = new UInt64('18446744073709551616'); + echo "This line should not be reached\n"; +} catch (OutOfRangeException $e) { + echo "Caught OutOfRangeException: " . $e->getMessage() . "\n"; +} + +// Example 3: Zero and Small Numbers +echo "\nExample 3: Zero and Small Numbers\n"; +try { + $zero = new UInt64('0'); + echo "Zero: " . $zero->getValue() . "\n"; + + // Addition with zero + $sum = $zero->add(new UInt64('100')); + echo "Addition with zero: " . $sum->getValue() . "\n"; + + // Subtraction from small number + $small = new UInt64('100'); + $diff = $small->subtract(new UInt64('50')); + echo "Subtraction from small number: " . $diff->getValue() . "\n"; +} catch (Exception $e) { + echo "Error in Example 3: " . $e->getMessage() . "\n"; +} + +// Example 4: Comparison Operations +echo "\nExample 4: Comparison Operations\n"; +try { + $a = new UInt64('18446744073709551610'); + $b = new UInt64('18446744073709551600'); + + echo "A: " . $a->getValue() . "\n"; + echo "B: " . $b->getValue() . "\n"; + + echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n"; + echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n"; + echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n"; +} catch (Exception $e) { + echo "Error in Example 4: " . $e->getMessage() . "\n"; +} + +// Example 5: Overflow/Underflow Handling +echo "\nExample 5: Overflow/Underflow Handling\n"; +try { + $max = new UInt64('18446744073709551615'); + echo "Maximum value: " . $max->getValue() . "\n"; + + // This should cause an overflow + $overflow = $max->add(new UInt64('1')); + echo "This line should not be reached\n"; +} catch (OverflowException $e) { + echo "Caught OverflowException: " . $e->getMessage() . "\n"; +} + +try { + $min = new UInt64('0'); + echo "Minimum value: " . $min->getValue() . "\n"; + + // This should cause an underflow + $underflow = $min->subtract(new UInt64('1')); + echo "This line should not be reached\n"; +} catch (UnderflowException $e) { + echo "Caught UnderflowException: " . $e->getMessage() . "\n"; +} diff --git a/index.php b/index.php index 8cd5077..8cbce9c 100644 --- a/index.php +++ b/index.php @@ -48,6 +48,33 @@ public function __construct() $this->initStruct(); } + /** + * Retrieve all example data as an array. + * This method returns the initialized scalar, composite, and structured data. + * Can be used to display or process the data in various parts of the system. + * + * @return array + */ + public function getExamples(): array + { + return [ + 'years' => $this->years, + 'account_number' => $this->account_number, + 'account_balance' => $this->account_balance, + 'investment_amount' => $this->investment_amount, + 'grade' => $this->grade, + 'age' => $this->age, + 'names' => $this->names, + 'scores' => $this->scores, + 'weights' => $this->weights, + 'data' => $this->data, + 'listData' => $this->listData, + 'dictionary' => $this->dictionary, + 'struct' => $this->struct, + 'struct_all' => $this->struct->getFields(), // All fields in the struct + ]; + } + /** * Initialize scalar data types. * Scalar types represent individual values like numbers, bytes, and characters. @@ -181,33 +208,6 @@ private function initStruct(): void $this->struct->set('age', null); $this->struct->set('balance', 250.75); } - - /** - * Retrieve all example data as an array. - * This method returns the initialized scalar, composite, and structured data. - * Can be used to display or process the data in various parts of the system. - * - * @return array - */ - public function getExamples(): array - { - return [ - 'years' => $this->years, - 'account_number' => $this->account_number, - 'account_balance' => $this->account_balance, - 'investment_amount' => $this->investment_amount, - 'grade' => $this->grade, - 'age' => $this->age, - 'names' => $this->names, - 'scores' => $this->scores, - 'weights' => $this->weights, - 'data' => $this->data, - 'listData' => $this->listData, - 'dictionary' => $this->dictionary, - 'struct' => $this->struct, - 'struct_all' => $this->struct->getFields(), // All fields in the struct - ]; - } } // Instantiate the class and invoke the examples @@ -215,4 +215,3 @@ public function getExamples(): array // Display the example data var_dump($example->getExamples()); - diff --git a/infection.json b/infection.json new file mode 100644 index 0000000..c9dab97 --- /dev/null +++ b/infection.json @@ -0,0 +1,42 @@ +{ + "timeout": 10, + "source": { + "directories": [ + "src" + ] + }, + "logs": { + "text": "build/infection.log", + "summary": "build/infection-summary.log", + "debug": "build/infection-debug.log" + }, + "mutators": { + "@default": true, + "@equal": true, + "@identical": true, + "@conditional_boundary": true, + "@conditional_negation": true, + "@function_signature": true, + "@number": true, + "@operator": true, + "@regex": true, + "@return_value": true, + "@sort": true, + "@loop": true, + "@cast": true, + "@array": true, + "@boolean": true, + "@string": true + }, + "testFramework": "phpunit", + "phpUnit": { + "configDir": "." + }, + "ignoreMutations": [ + "src/helpers.php" + ], + "minMsi": 80, + "minCoveredMsi": 80, + "threads": 4, + "tmpDir": "build/infection" +} diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..886ac10 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + level: 9 + paths: + - src + - Tests + excludePaths: + - vendor + ignoreErrors: + # Ignore errors in test files for now + - '#Call to an undefined method.*#' + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + reportUnmatchedIgnoredErrors: false + tmpDir: build/phpstan diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..8fe6b84 --- /dev/null +++ b/pint.json @@ -0,0 +1,33 @@ +{ + "preset": "psr12", + "rules": { + "no_unused_imports": true, + "ordered_imports": { + "sort_algorithm": "alpha" + }, + "declare_strict_types": true, + "no_superfluous_phpdoc_tags": false, + "phpdoc_align": { + "align": "left" + }, + "phpdoc_to_comment": false, + "phpdoc_indent": true, + "phpdoc_single_line_var_spacing": true, + "phpdoc_var_without_name": false, + "phpdoc_scalar": true, + "phpdoc_types": true, + "phpdoc_trim": true, + "phpdoc_trim_consecutive_blank_line_separation": true, + "phpdoc_separation": true, + "final_class": true, + "final_internal_class": true, + "ordered_class_elements": true, + "ordered_interfaces": true, + "ordered_traits": true, + "self_static_accessor": true, + "strict_comparison": true, + "visibility_required": { + "elements": ["property", "method", "const"] + } + } +} \ No newline at end of file diff --git a/src/Abstract/AbstractBigInteger.php b/src/Abstract/AbstractBigInteger.php index 9ee7c77..4d24bd0 100644 --- a/src/Abstract/AbstractBigInteger.php +++ b/src/Abstract/AbstractBigInteger.php @@ -6,8 +6,8 @@ use Nejcc\PhpDatatypes\Interfaces\BigIntegerInterface; use Nejcc\PhpDatatypes\Interfaces\NativeIntegerInterface; -use Nejcc\PhpDatatypes\Traits\ArithmeticOperationsTrait; -use Nejcc\PhpDatatypes\Traits\IntegerComparisonTrait; +use Nejcc\PhpDatatypes\Traits\BigArithmeticOperationsTrait; +use Nejcc\PhpDatatypes\Traits\BigIntegerComparisonTrait; /** * Abstract class for big integer types using arbitrary-precision arithmetic. @@ -16,42 +16,34 @@ */ abstract class AbstractBigInteger implements BigIntegerInterface { - use ArithmeticOperationsTrait; - use IntegerComparisonTrait; + use BigArithmeticOperationsTrait; + use BigIntegerComparisonTrait; + + public const MIN_VALUE = null; + public const MAX_VALUE = null; /** * @var string */ protected readonly string $value; - public const MIN_VALUE = null; - public const MAX_VALUE = null; - /** * @param int|string $value + * @param bool $trusted Internal use only. When true, skips MIN/MAX validation. + * Used by arithmetic ops that already pre-check the result. */ - public function __construct(int|string $value) + public function __construct(int|string $value, bool $trusted = false) { + if ($trusted) { + $this->value = (string)$value; + return; + } $this->setValue($value); } - /** - * @param int|string $value - * @return void - */ - protected function setValue(int|string $value): void + public function __toString(): string { - $valueStr = (string)$value; - - if (bccomp($valueStr, (string)static::MIN_VALUE) < 0 || bccomp($valueStr, (string)static::MAX_VALUE) > 0) { - throw new \OutOfRangeException(sprintf( - 'Value must be between %s and %s.', - static::MIN_VALUE, - static::MAX_VALUE - )); - } - - $this->value = $valueStr; + return $this->value; } /** @@ -64,96 +56,31 @@ public function getValue(): string /** * @param NativeIntegerInterface|BigIntegerInterface $other + * * @return int */ - public function compare(NativeIntegerInterface|BigIntegerInterface $other): int + final public function compare(NativeIntegerInterface|BigIntegerInterface $other): int { return bccomp($this->value, (string)$other->getValue()); } - - /** - * @param BigIntegerInterface|NativeIntegerInterface $other - * @param callable $operation - * @param string $operationName - * @return $this - */ - protected function performOperation( - BigIntegerInterface|NativeIntegerInterface $other, - callable $operation, - string $operationName - ): static { - $result = $operation($this->value, (string)$other->getValue()); - - if (bccomp($result, (string)static::MIN_VALUE) < 0 || bccomp($result, (string)static::MAX_VALUE) > 0) { - $exceptionClass = bccomp($result, (string)static::MAX_VALUE) > 0 ? \OverflowException::class : \UnderflowException::class; - throw new $exceptionClass('Result is out of bounds.'); - } - - return new static($result); - } - - /** - * @param string $a - * @param string $b - * @return string - */ - protected function addValues(string $a, string $b): string - { - return bcadd($a, $b, 0); - } - - /** - * @param string $a - * @param string $b - * @return string - */ - protected function subtractValues(string $a, string $b): string - { - return bcsub($a, $b, 0); - } - - /** - * @param string $a - * @param string $b - * @return string - */ - protected function multiplyValues(string $a, string $b): string - { - return bcmul($a, $b, 0); - } - /** - * @param string $a - * @param string $b - * @return string + * @param int|string $value + * + * @return void */ - protected function divideValues(string $a, string $b): string + protected function setValue(int|string $value): void { - if ($b === '0') { - throw new \DivisionByZeroError('Division by zero.'); - } - - $result = bcdiv($a, $b, 0); - - if (str_contains($result, '.')) { - throw new \UnexpectedValueException('Division result is not an integer.'); - } - - return $result; - } + $valueStr = (string)$value; - /** - * @param string $a - * @param string $b - * @return string - */ - protected function modValues(string $a, string $b): string - { - if ($b === '0') { - throw new \DivisionByZeroError('Division by zero.'); + if (bccomp($valueStr, (string)static::MIN_VALUE) < 0 || bccomp($valueStr, (string)static::MAX_VALUE) > 0) { + throw new \OutOfRangeException(sprintf( + 'Value must be between %s and %s.', + static::MIN_VALUE, + static::MAX_VALUE + )); } - return bcmod($a, $b); + $this->value = $valueStr; } } diff --git a/src/Abstract/AbstractChar.php b/src/Abstract/AbstractChar.php new file mode 100644 index 0000000..10a62c3 --- /dev/null +++ b/src/Abstract/AbstractChar.php @@ -0,0 +1,97 @@ +value = $value; + } + + public function __toString(): string + { + return $this->value; + } + + final public function getValue(): string + { + return $this->value; + } + + final public function toUpperCase(): static + { + return new static(strtoupper($this->value)); + } + + final public function toLowerCase(): static + { + return new static(strtolower($this->value)); + } + + final public function isLetter(): bool + { + return ctype_alpha($this->value); + } + + final public function isDigit(): bool + { + return ctype_digit($this->value); + } + + final public function isUpperCase(): bool + { + return ctype_upper($this->value); + } + + final public function isLowerCase(): bool + { + return ctype_lower($this->value); + } + + final public function isWhitespace(): bool + { + return ctype_space($this->value); + } + + final public function getNumericValue(): int + { + return $this->isDigit() ? (int)$this->value : -1; + } + + final public function equals(self $char): bool + { + return $this->value === $char->getValue(); + } + + final public function toAscii(): int + { + return ord($this->value); + } + + final public static function fromAscii(int $ascii): static + { + if ($ascii < 0 || $ascii > 255) { + throw new \InvalidArgumentException('ASCII value must be between 0 and 255.'); + } + return new static(chr($ascii)); + } +} diff --git a/src/Abstract/AbstractFloat.php b/src/Abstract/AbstractFloat.php index 07da5b9..362e5e7 100644 --- a/src/Abstract/AbstractFloat.php +++ b/src/Abstract/AbstractFloat.php @@ -8,30 +8,144 @@ abstract class AbstractFloat { + public const MIN_VALUE = null; + public const MAX_VALUE = null; /** * @var float */ protected readonly float $value; - public const MIN_VALUE = null; - public const MAX_VALUE = null; - /** * @param float $value + * @param bool $trusted Internal use only. When true, skips MIN/MAX and INF validation. + * Used by arithmetic ops that already pre-check the result. */ - public function __construct(float $value) + public function __construct(float $value, bool $trusted = false) { + if ($trusted) { + $this->value = $value; + return; + } $this->setValue($value); } + public function __toString(): string + { + return (string)$this->value; + } + + /** + * @return float + */ + final public function getValue(): float + { + return $this->value; + } + + #[\NoDiscard('add() returns a new immutable Float; the original is unchanged so discarding the result is always a bug')] + final public function add(self $other): static + { + $result = $this->value + $other->value; + if (is_infinite($result)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + if ($result > static::MAX_VALUE || $result < static::MIN_VALUE) { + throw new OutOfRangeException(sprintf( + 'Value %f is out of range for this float type. Allowed range: [%f, %f]', + $result, + static::MIN_VALUE, + static::MAX_VALUE + )); + } + return new static($result, true); + } + + #[\NoDiscard('subtract() returns a new immutable Float; the original is unchanged so discarding the result is always a bug')] + final public function subtract(self $other): static + { + $result = $this->value - $other->value; + if (is_infinite($result)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + if ($result > static::MAX_VALUE || $result < static::MIN_VALUE) { + throw new OutOfRangeException(sprintf( + 'Value %f is out of range for this float type. Allowed range: [%f, %f]', + $result, + static::MIN_VALUE, + static::MAX_VALUE + )); + } + return new static($result, true); + } + + #[\NoDiscard('multiply() returns a new immutable Float; the original is unchanged so discarding the result is always a bug')] + final public function multiply(self $other): static + { + $result = $this->value * $other->value; + if (is_infinite($result)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + if ($result > static::MAX_VALUE || $result < static::MIN_VALUE) { + throw new OutOfRangeException(sprintf( + 'Value %f is out of range for this float type. Allowed range: [%f, %f]', + $result, + static::MIN_VALUE, + static::MAX_VALUE + )); + } + return new static($result, true); + } + + #[\NoDiscard('divide() returns a new immutable Float; the original is unchanged so discarding the result is always a bug')] + final public function divide(self $other): static + { + if ($other->value === 0.0) { + throw new \DivisionByZeroError('Division by zero.'); + } + $result = $this->value / $other->value; + if (is_infinite($result)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + if ($result > static::MAX_VALUE || $result < static::MIN_VALUE) { + throw new OutOfRangeException(sprintf( + 'Value %f is out of range for this float type. Allowed range: [%f, %f]', + $result, + static::MIN_VALUE, + static::MAX_VALUE + )); + } + return new static($result, true); + } + + final public function equals(self $other): bool + { + return $this->value === $other->value; + } + + final public function isGreaterThan(self $other): bool + { + return $this->value > $other->value; + } + + final public function isLessThan(self $other): bool + { + return $this->value < $other->value; + } + /** * @param float $value + * * @return void */ protected function setValue(float $value): void { + // Disallow INF and -INF + if (is_infinite($value)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + // Check if value is out of range - if ($value < static::MIN_VALUE || $value > static::MAX_VALUE) { + if ($value > static::MAX_VALUE || $value < static::MIN_VALUE) { throw new OutOfRangeException(sprintf( 'Value %f is out of range for this float type. Allowed range: [%f, %f]', $value, @@ -42,12 +156,4 @@ protected function setValue(float $value): void $this->value = $value; } - - /** - * @return float - */ - public function getValue(): float - { - return $this->value; - } } diff --git a/src/Abstract/AbstractNativeInteger.php b/src/Abstract/AbstractNativeInteger.php index 7aaa529..6bb808f 100644 --- a/src/Abstract/AbstractNativeInteger.php +++ b/src/Abstract/AbstractNativeInteger.php @@ -4,9 +4,10 @@ namespace Nejcc\PhpDatatypes\Abstract; +use Nejcc\PhpDatatypes\Attributes\Range; use Nejcc\PhpDatatypes\Interfaces\NativeIntegerInterface; -use Nejcc\PhpDatatypes\Traits\ArithmeticOperationsTrait; -use Nejcc\PhpDatatypes\Traits\IntegerComparisonTrait; +use Nejcc\PhpDatatypes\Traits\NativeArithmeticOperationsTrait; +use Nejcc\PhpDatatypes\Traits\NativeIntegerComparisonTrait; /** * Abstract class for native integer types. @@ -15,136 +16,67 @@ */ abstract class AbstractNativeInteger implements NativeIntegerInterface { - use ArithmeticOperationsTrait; - use IntegerComparisonTrait; - - protected readonly int $value; + use NativeArithmeticOperationsTrait; + use NativeIntegerComparisonTrait; public const MIN_VALUE = null; public const MAX_VALUE = null; + protected readonly int $value; + /** * @param int $value + * @param bool $trusted Internal use only. When true, skips MIN/MAX validation. + * Callers must guarantee the value is already within range + * (used by arithmetic ops that pre-check the result). */ - public function __construct(int $value) + public function __construct(int $value, bool $trusted = false) { + if ($trusted) { + $this->value = $value; + return; + } $this->setValue($value); } - /** - * @param int $value - * @return void - */ - protected function setValue(int $value): void + public function __toString(): string { - if ($value < static::MIN_VALUE || $value > static::MAX_VALUE) { - throw new \OutOfRangeException(sprintf( - 'Value must be between %d and %d.', - static::MIN_VALUE, - static::MAX_VALUE - )); - } - - $this->value = $value; + return (string)$this->value; } /** * @return int */ - public function getValue(): int + final public function getValue(): int { return $this->value; } /** * @param NativeIntegerInterface $other + * * @return int */ - public function compare(NativeIntegerInterface $other): int + final public function compare(NativeIntegerInterface $other): int { return $this->value <=> $other->getValue(); } /** - * @param NativeIntegerInterface $other - * @param callable $operation - * @param string $operationName - * @return $this - */ - protected function performOperation( - NativeIntegerInterface $other, - callable $operation, - string $operationName - ): static { - $result = $operation($this->value, $other->getValue()); - - if ($result < static::MIN_VALUE || $result > static::MAX_VALUE) { - $exceptionClass = $result > static::MAX_VALUE ? \OverflowException::class : \UnderflowException::class; - throw new $exceptionClass('Result is out of bounds.'); - } - - return new static($result); - } - - /** - * @param int $a - * @param int $b - * @return int - */ - protected function addValues(int $a, int $b): int - { - return $a + $b; - } - - /** - * @param int $a - * @param int $b - * @return int - */ - protected function subtractValues(int $a, int $b): int - { - return $a - $b; - } - - /** - * @param int $a - * @param int $b - * @return int - */ - protected function multiplyValues(int $a, int $b): int - { - return $a * $b; - } - - /** - * @param int $a - * @param int $b - * @return int - */ - protected function divideValues(int $a, int $b): int - { - if ($b === 0) { - throw new \DivisionByZeroError('Division by zero.'); - } - - if ($a % $b !== 0) { - throw new \UnexpectedValueException('Division result is not an integer.'); - } - - return intdiv($a, $b); - } - - /** - * @param int $a - * @param int $b - * @return int + * @param int $value + * + * @return void */ - protected function modValues(int $a, int $b): int + protected function setValue(int $value): void { - if ($b === 0) { - throw new \DivisionByZeroError('Division by zero.'); + if ($value < static::MIN_VALUE || $value > static::MAX_VALUE) { + throw new \OutOfRangeException(sprintf( + 'Value must be between %d and %d.', + static::MIN_VALUE, + static::MAX_VALUE + )); } - return $a % $b; + $this->value = $value; } } diff --git a/src/Abstract/AbstractString.php b/src/Abstract/AbstractString.php index fec3217..e090e08 100644 --- a/src/Abstract/AbstractString.php +++ b/src/Abstract/AbstractString.php @@ -25,24 +25,12 @@ public function __construct(string $value) $this->setValue($value); } - /** - * Set the string value. - * - * @param string $value - * @return void - */ - protected function setValue(string $value): void - { - // Perform validations if necessary (e.g., length checks) - $this->value = $value; - } - /** * Get the string value. * * @return string */ - public function getValue(): string + final public function getValue(): string { return $this->value; } @@ -51,9 +39,10 @@ public function getValue(): string * Compare two strings. * * @param StringInterface $other + * * @return int */ - public function compare(StringInterface $other): int + final public function compare(StringInterface $other): int { return strcmp($this->value, $other->getValue()); } @@ -62,9 +51,10 @@ public function compare(StringInterface $other): int * Append another string to this one. * * @param StringInterface $other + * * @return static */ - public function append(StringInterface $other): static + final public function append(StringInterface $other): static { return new static($this->value . $other->getValue()); } @@ -74,9 +64,10 @@ public function append(StringInterface $other): static * * @param int $start * @param int|null $length + * * @return static */ - public function substring(int $start, ?int $length = null): static + final public function substring(int $start, ?int $length = null): static { return new static(substr($this->value, $start, $length)); } @@ -85,9 +76,10 @@ public function substring(int $start, ?int $length = null): static * Check if this string contains another string. * * @param StringInterface $needle + * * @return bool */ - public function contains(StringInterface $needle): bool + final public function contains(StringInterface $needle): bool { return str_contains($this->value, $needle->getValue()); } @@ -97,8 +89,21 @@ public function contains(StringInterface $needle): bool * * @return int */ - public function length(): int + final public function length(): int { return strlen($this->value); } + + /** + * Set the string value. + * + * @param string $value + * + * @return void + */ + protected function setValue(string $value): void + { + // Perform validations if necessary (e.g., length checks) + $this->value = $value; + } } diff --git a/src/Abstract/AbstractVector.php b/src/Abstract/AbstractVector.php new file mode 100644 index 0000000..f995107 --- /dev/null +++ b/src/Abstract/AbstractVector.php @@ -0,0 +1,151 @@ +validateComponents($components); + $this->components = $components; + } + + public function __toString(): string + { + return '(' . implode(', ', $this->components) . ')'; + } + + public function getComponents(): array + { + return $this->components; + } + + public function magnitude(): float + { + return sqrt(array_sum(array_map(fn ($component) => $component ** 2, $this->components))); + } + + public function normalize(): self + { + $magnitude = $this->magnitude(); + if ($magnitude === 0.0) { + throw new InvalidArgumentException("Cannot normalize a zero vector"); + } + + $normalized = array_map(fn ($component) => $component / $magnitude, $this->components); + return new static($normalized); + } + + public function dot(self $other): float + { + if (get_class($this) !== get_class($other)) { + throw new InvalidArgumentException("Cannot calculate dot product of vectors with different dimensions"); + } + + return array_sum(array_map( + fn ($a, $b) => $a * $b, + $this->components, + $other->components + )); + } + + public function add(self $other): self + { + if (get_class($this) !== get_class($other)) { + throw new InvalidArgumentException("Cannot add vectors with different dimensions"); + } + + $result = array_map( + fn ($a, $b) => $a + $b, + $this->components, + $other->components + ); + + return new static($result); + } + + public function subtract(self $other): self + { + if (get_class($this) !== get_class($other)) { + throw new InvalidArgumentException("Cannot subtract vectors with different dimensions"); + } + + $result = array_map( + fn ($a, $b) => $a - $b, + $this->components, + $other->components + ); + + return new static($result); + } + + public function scale(float $scalar): self + { + $result = array_map( + fn ($component) => $component * $scalar, + $this->components + ); + + return new static($result); + } + + public function getComponent(int $index): float + { + if (!isset($this->components[$index])) { + throw new InvalidArgumentException("Invalid component index"); + } + return $this->components[$index]; + } + + public function equals(DataTypeInterface $other): bool + { + if (!$other instanceof self) { + return false; + } + + return $this->components === $other->components; + } + + public function distance(self $other): float + { + if (get_class($this) !== get_class($other)) { + throw new InvalidArgumentException("Cannot calculate distance between vectors with different dimensions"); + } + + $squaredDiff = array_map( + fn ($a, $b) => ($a - $b) ** 2, + $this->components, + $other->components + ); + + return sqrt(array_sum($squaredDiff)); + } + + abstract protected function validateComponents(array $components): void; + + protected function validateNumericComponents(array $components): void + { + foreach ($components as $component) { + if (!is_numeric($component)) { + throw new InvalidArgumentException("All components must be numeric"); + } + } + } + + protected function validateComponentCount(array $components, int $expectedCount): void + { + if (count($components) !== $expectedCount) { + throw new InvalidArgumentException(sprintf( + "Vector must have exactly %d components", + $expectedCount + )); + } + } +} diff --git a/src/Abstract/ArrayAbstraction.php b/src/Abstract/ArrayAbstraction.php new file mode 100644 index 0000000..8aca996 --- /dev/null +++ b/src/Abstract/ArrayAbstraction.php @@ -0,0 +1,104 @@ +value = $value; + } + + public function getValue(): array + { + return $this->value; + } + + public function count(): int + { + return count($this->value); + } + + public function getIterator(): \ArrayIterator + { + return new \ArrayIterator($this->value); + } + + public function toArray(): array + { + return $this->value; + } + + /** + * Return the first element, or null if the collection is empty. + * + * Uses PHP 8.5's array_first() — O(1), works with any array key shape. + */ + #[\NoDiscard('first() returns the first element or null; discarding the result is always a bug')] + public function first(): mixed + { + return array_first($this->value); + } + + /** + * Return the last element, or null if the collection is empty. + * + * Uses PHP 8.5's array_last() — O(1), works with any array key shape. + */ + #[\NoDiscard('last() returns the last element or null; discarding the result is always a bug')] + public function last(): mixed + { + return array_last($this->value); + } + + /** + * Whether the collection is empty. + */ + #[\NoDiscard('isEmpty() returns whether the collection has elements; discarding the result is always a bug')] + public function isEmpty(): bool + { + return $this->value === []; + } + + // Add this for use by FloatArray and similar subclasses + protected function validateFloats(array $array): void + { + foreach ($array as $item) { + if (!is_float($item)) { + throw new \Nejcc\PhpDatatypes\Exceptions\InvalidFloatException('All elements must be floats. Invalid value: ' . json_encode($item)); + } + } + } + + protected function validateStrings(array $array): void + { + foreach ($array as $item) { + if (!is_string($item)) { + throw new \Nejcc\PhpDatatypes\Exceptions\InvalidStringException('All elements must be strings. Invalid value: ' . json_encode($item)); + } + } + } + + protected function validateBytes(array $array): void + { + foreach ($array as $item) { + if (!is_int($item) || $item < 0 || $item > 255) { + throw new \Nejcc\PhpDatatypes\Exceptions\InvalidByteException('All elements must be valid bytes (0-255). Invalid value: ' . $item); + } + } + } + + protected function validateJson(string $json): void + { + try { + json_decode($json, true, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \InvalidArgumentException('Invalid JSON provided: ' . $e->getMessage()); + } + } + +} diff --git a/src/Abstract/BaseStruct.php b/src/Abstract/BaseStruct.php index c15df7b..68cc62e 100644 --- a/src/Abstract/BaseStruct.php +++ b/src/Abstract/BaseStruct.php @@ -1,5 +1,7 @@ value = $value; + } + + /** + * Converts the Boolean to a string representation. + * + * @return string "true" or "false" + */ + public function __toString(): string + { + return $this->value ? 'true' : 'false'; + } + + /** + * Gets the underlying boolean value. + * + * @return bool The boolean value + */ + final public function getValue(): bool + { + return $this->value; + } + + /** + * Performs a logical AND operation with another Boolean. + * + * @param self $other The other Boolean to AND with + * + * @return static A new instance with the result + */ + final public function and(self $other): static + { + return new static($this->value && $other->getValue()); + } + + /** + * Performs a logical OR operation with another Boolean. + * + * @param self $other The other Boolean to OR with + * + * @return static A new instance with the result + */ + final public function or(self $other): static + { + return new static($this->value || $other->getValue()); + } + + /** + * Performs a logical XOR operation with another Boolean. + * + * @param self $other The other Boolean to XOR with + * + * @return static A new instance with the result + */ + final public function xor(self $other): static + { + return new static($this->value xor $other->getValue()); + } + + /** + * Performs a logical NOT operation. + * + * @return static A new instance with the negated value + */ + final public function not(): static + { + return new static(!$this->value); + } + + /** + * Checks if this Boolean equals another Boolean. + * + * @param self $other The other Boolean to compare with + * + * @return bool True if the values are equal, false otherwise + */ + final public function equals(self $other): bool + { + return $this->value === $other->getValue(); + } + + /** + * Creates a Boolean instance from a string. + * + * @param string $value The string to convert ("true", "false", "1", "0") + * + * @return static A new instance + * + * @throws \InvalidArgumentException If the string is not a valid boolean representation + */ + final public static function fromString(string $value): static + { + $value = strtolower($value); + if ($value === 'true' || $value === '1') { + return new static(true); + } + if ($value === 'false' || $value === '0') { + return new static(false); + } + throw new \InvalidArgumentException('Invalid boolean string representation'); + } + + /** + * Creates a Boolean instance from an integer. + * + * @param int $value The integer to convert (0 or 1) + * + * @return static A new instance + * + * @throws \InvalidArgumentException If the integer is not 0 or 1 + */ + final public static function fromInteger(int $value): static + { + if ($value === 1) { + return new static(true); + } + if ($value === 0) { + return new static(false); + } + throw new \InvalidArgumentException('Integer must be 0 or 1'); + } +} diff --git a/src/Abstract/ByteAbstraction.php b/src/Abstract/ByteAbstraction.php new file mode 100644 index 0000000..66a6b12 --- /dev/null +++ b/src/Abstract/ByteAbstraction.php @@ -0,0 +1,166 @@ + self::MAX_VALUE) { + throw new \OutOfRangeException('Byte value must be between 0 and 255.'); + } + $this->value = $value; + } + + public function __toString(): string + { + return (string)$this->value; + } + + final public function getValue(): int + { + return $this->value; + } + + final public function add(self|int $other): static + { + $otherValue = $other instanceof self ? $other->value : $other; + return new static($this->wrap($this->value + $otherValue)); + } + + final public function subtract(self|int $other): static + { + $otherValue = $other instanceof self ? $other->value : $other; + return new static($this->wrap($this->value - $otherValue)); + } + + final public function multiply(self|int $other): static + { + $otherValue = $other instanceof self ? $other->value : $other; + return new static($this->wrap($this->value * $otherValue)); + } + + final public function divide(self|int $other): static + { + $otherValue = $other instanceof self ? $other->value : $other; + if ($otherValue === 0) { + throw new \DivisionByZeroError('Division by zero.'); + } + return new static($this->wrap(intdiv($this->value, $otherValue))); + } + + final public function and(self $other): static + { + return new static($this->value & $other->value); + } + + final public function or(self $other): static + { + return new static($this->value | $other->value); + } + + final public function xor(self $other): static + { + return new static($this->value ^ $other->value); + } + + final public function not(): static + { + return new static(~$this->value & 0xFF); + } + + final public function leftShift(int $positions): static + { + return new static(($this->value << $positions) & 0xFF); + } + + final public function rightShift(int $positions): static + { + return new static($this->value >> $positions); + } + + final public function shiftLeft(int $positions): static + { + return $this->leftShift($positions); + } + + final public function shiftRight(int $positions): static + { + return $this->rightShift($positions); + } + + final public function equals(self $other): bool + { + return $this->value === $other->value; + } + + final public function isGreaterThan(self $other): bool + { + return $this->value > $other->value; + } + + final public function isLessThan(self $other): bool + { + return $this->value < $other->value; + } + + final public function toBinary(): string + { + return sprintf('%08b', $this->value); + } + + final public function toHex(): string + { + return sprintf('%02X', $this->value); + } + + final public static function fromBinary(string $binary): static + { + return new static(bindec($binary)); + } + + final public static function fromHex(string $hex): static + { + return new static(hexdec($hex)); + } + + /** + * Wrap a value to 0-255 (used for arithmetic). + * + * @param int $value + * + * @return int + */ + protected function wrap(int $value): int + { + return ($value + 256) % 256; + } + + protected function setValue(float $value): void + { + // Disallow INF and -INF + if (is_infinite($value)) { + throw new OutOfRangeException('INF and -INF are not allowed for this float type.'); + } + // ... existing range check ... + } +} diff --git a/src/Attributes/Email.php b/src/Attributes/Email.php new file mode 100644 index 0000000..84fd30a --- /dev/null +++ b/src/Attributes/Email.php @@ -0,0 +1,16 @@ +getAttributes() as $attribute) { + $instance = $attribute->newInstance(); + match (true) { + $instance instanceof Range => self::validateRange($value, $instance), + $instance instanceof Email => self::validateEmail($value), + $instance instanceof Regex => self::validateRegex($value, $instance), + $instance instanceof NotNull => self::validateNotNull($value), + $instance instanceof Length => self::validateLength($value, $instance), + $instance instanceof Url => self::validateUrl($value), + $instance instanceof Uuid => self::validateUuid($value), + $instance instanceof IpAddress => self::validateIpAddress($value), + }; + } + } + + private static function validateRange(mixed $value, Range $range): void + { + if (!is_numeric($value)) { + throw new InvalidArgumentException('Value must be numeric for range validation'); + } + + $numValue = is_string($value) ? (float) $value : $value; + + if ($numValue < $range->min || $numValue > $range->max) { + throw new OutOfRangeException( + sprintf('Value must be between %s and %s', $range->min, $range->max) + ); + } + } + + private static function validateEmail(mixed $value): void + { + if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_EMAIL)) { + throw new InvalidArgumentException('Invalid email address'); + } + } + + private static function validateRegex(mixed $value, Regex $regex): void + { + if (!is_string($value) || !preg_match($regex->pattern, $value)) { + throw new InvalidArgumentException('Value does not match required pattern'); + } + } + + private static function validateNotNull(mixed $value): void + { + if ($value === null) { + throw new InvalidArgumentException('Value cannot be null'); + } + } + + private static function validateLength(mixed $value, Length $length): void + { + if (!is_string($value)) { + throw new InvalidArgumentException('Value must be a string for length validation'); + } + + $strLength = strlen($value); + + if ($length->min !== null && $strLength < $length->min) { + throw new InvalidArgumentException( + sprintf('String length must be at least %d characters', $length->min) + ); + } + + if ($length->max !== null && $strLength > $length->max) { + throw new InvalidArgumentException( + sprintf('String length must be at most %d characters', $length->max) + ); + } + } + + private static function validateUrl(mixed $value): void + { + if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) { + throw new InvalidArgumentException('Invalid URL'); + } + } + + private static function validateUuid(mixed $value): void + { + if (!is_string($value)) { + throw new InvalidArgumentException('Value must be a string for UUID validation'); + } + + $pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; + if (!preg_match($pattern, $value)) { + throw new InvalidArgumentException('Invalid UUID format'); + } + } + + private static function validateIpAddress(mixed $value): void + { + if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_IP)) { + throw new InvalidArgumentException('Invalid IP address'); + } + } +} diff --git a/src/Composite/Arrays/ByteSlice.php b/src/Composite/Arrays/ByteSlice.php index 939b036..71b91bc 100644 --- a/src/Composite/Arrays/ByteSlice.php +++ b/src/Composite/Arrays/ByteSlice.php @@ -1,47 +1,43 @@ The byte values (0-255). */ - private array $value; + protected array $value; /** * Constructor for ByteSlice. * * @param array $value The array of byte values. + * * @throws InvalidByteException If any value is not a valid byte. */ - public function __construct(array $value) + public function __construct(array $value, bool $trusted = false) { - $this->validateBytes($value); + if (!$trusted) { + $this->validateBytes($value); + } $this->value = $value; } /** - * Validate that all elements are valid bytes (0-255). - * - * @param array $array The array to validate. - * @throws InvalidByteException If any element is not a valid byte. - * @return void + * Construct without validation. Caller must guarantee every element is an int in 0..255. */ - private function validateBytes(array $array): void + public static function fromTrusted(array $value): self { - foreach ($array as $item) { - if (!is_int($item) || $item < 0 || $item > 255) { - throw new InvalidByteException("All elements must be valid bytes (0-255). Invalid value: " . $item); - } - } + return new self($value, true); } /** @@ -58,6 +54,7 @@ public function getValue(): array * Get the byte at a specific index. * * @param int $index The index. + * * @return int|null The byte value or null if index is out of bounds. */ public function getByte(int $index): ?int @@ -82,7 +79,7 @@ public function count(): int */ public function toHex(): string { - return implode('', array_map(fn($byte) => sprintf('%02X', $byte), $this->value)); + return implode('', array_map(fn ($byte) => sprintf('%02X', $byte), $this->value)); } /** @@ -90,7 +87,9 @@ public function toHex(): string * * @param int $offset The start offset. * @param int|null $length The length of the slice. + * * @return ByteSlice The sliced byte array. + * * @throws InvalidByteException */ public function slice(int $offset, ?int $length = null): self @@ -102,7 +101,9 @@ public function slice(int $offset, ?int $length = null): self * Merge the current byte array with another byte array. * * @param ByteSlice $other The other byte array to merge. + * * @return ByteSlice A new ByteSlice instance containing the merged bytes. + * * @throws InvalidByteException */ public function merge(ByteSlice $other): self @@ -114,6 +115,7 @@ public function merge(ByteSlice $other): self * ArrayAccess: Check if a byte exists at the given offset. * * @param int $offset The array offset. + * * @return bool True if offset exists, false otherwise. */ public function offsetExists(mixed $offset): bool @@ -125,6 +127,7 @@ public function offsetExists(mixed $offset): bool * ArrayAccess: Get the byte at the given offset. * * @param int $offset The array offset. + * * @return mixed The byte value at the given offset. */ public function offsetGet(mixed $offset): mixed @@ -137,6 +140,7 @@ public function offsetGet(mixed $offset): mixed * * @param int $offset The array offset. * @param mixed $value The value to set (not allowed). + * * @throws InvalidByteException Always thrown since ByteSlice is immutable. */ public function offsetSet(mixed $offset, mixed $value): void @@ -148,6 +152,7 @@ public function offsetSet(mixed $offset, mixed $value): void * ArrayAccess: Prevent unsetting by throwing an exception. * * @param int $offset The array offset. + * * @throws InvalidByteException Always thrown since ByteSlice is immutable. */ public function offsetUnset(mixed $offset): void @@ -158,9 +163,9 @@ public function offsetUnset(mixed $offset): void /** * Get an iterator for the byte array. * - * @return Traversable An iterator for the byte array. + * @return \ArrayIterator An iterator for the byte array. */ - public function getIterator(): Traversable + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->value); } diff --git a/src/Composite/Arrays/DynamicArray.php b/src/Composite/Arrays/DynamicArray.php new file mode 100644 index 0000000..28061dc --- /dev/null +++ b/src/Composite/Arrays/DynamicArray.php @@ -0,0 +1,116 @@ +capacity = $initialCapacity; + parent::__construct($elementType, $initialData); + if (count($initialData) > $this->capacity) { + $this->capacity = count($initialData); + } + } + + /** + * Get the current capacity + * + * @return int + */ + public function getCapacity(): int + { + return $this->capacity; + } + + /** + * Reserve capacity for at least $capacity elements + * + * @param int $capacity + * + * @return void + */ + public function reserve(int $capacity): void + { + if ($capacity > $this->capacity) { + $this->capacity = $capacity; + } + } + + /** + * Shrink the capacity to fit the current number of elements + * + * @return void + */ + public function shrinkToFit(): void + { + $this->capacity = count($this->getValue()); + } + + /** + * ArrayAccess implementation (override to grow capacity as needed) + */ + public function offsetSet($offset, $value): void + { + if (!$this->isValidType($value)) { + throw new TypeMismatchException( + "Value must be of type {$this->getElementType()}" + ); + } + if (is_null($offset)) { + // Appending + if (count($this->getValue()) >= $this->capacity) { + $this->capacity = max(1, $this->capacity * 2); + } + } else { + if ($offset >= $this->capacity) { + $this->capacity = $offset + 1; + } + } + parent::offsetSet($offset, $value); + } + + /** + * Set the array value (override to adjust capacity) + * + * @param mixed $value + * + * @throws TypeMismatchException + */ + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new TypeMismatchException('Value must be an array.'); + } + if (count($value) > $this->capacity) { + $this->capacity = count($value); + } + parent::setValue($value); + } +} diff --git a/src/Composite/Arrays/FixedSizeArray.php b/src/Composite/Arrays/FixedSizeArray.php new file mode 100644 index 0000000..7e80f23 --- /dev/null +++ b/src/Composite/Arrays/FixedSizeArray.php @@ -0,0 +1,158 @@ + $size) { + throw new InvalidArgumentException( + "Initial data size ({$size}) exceeds fixed size ({$size})" + ); + } + + $this->size = $size; + parent::__construct($elementType, $initialData); + } + + /** + * Get the fixed size of the array + * + * @return int The fixed size + */ + public function getSize(): int + { + return $this->size; + } + + /** + * Check if the array is full + * + * @return bool True if the array is at its maximum size + */ + public function isFull(): bool + { + return count($this->getValue()) >= $this->size; + } + + /** + * Check if the array is empty + * + * @return bool True if the array has no elements + */ + public function isEmpty(): bool + { + return count($this->getValue()) === 0; + } + + /** + * Get the number of remaining slots + * + * @return int The number of available slots + */ + public function getRemainingSlots(): int + { + return $this->size - count($this->getValue()); + } + + /** + * ArrayAccess implementation + */ + public function offsetSet($offset, $value): void + { + if (is_null($offset) && $this->isFull()) { + throw new InvalidArgumentException('Array is at maximum capacity'); + } + + if (!is_null($offset) && $offset >= $this->size) { + throw new InvalidArgumentException( + "Index {$offset} is out of bounds (size: {$this->size})" + ); + } + + parent::offsetSet($offset, $value); + } + + /** + * Set the array value + * + * @param mixed $value The new array data + * + * @throws TypeMismatchException If any element doesn't match the required type + * @throws InvalidArgumentException If the new array size exceeds the fixed size + */ + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new TypeMismatchException('Value must be an array'); + } + + if (count($value) > $this->size) { + throw new InvalidArgumentException( + "New array size (" . count($value) . ") exceeds fixed size ({$this->size})" + ); + } + + parent::setValue($value); + } + + /** + * Fill the array with a value up to its capacity + * + * @param mixed $value The value to fill with + * + * @return self + * + * @throws TypeMismatchException If the value doesn't match the required type + */ + public function fill($value): self + { + if (!$this->isValidType($value)) { + throw new TypeMismatchException( + "Value must be of type {$this->getElementType()}" + ); + } + + $this->setValue(array_fill(0, $this->size, $value)); + return $this; + } + + /** + * Create a new array with the same type and size + * + * @return self A new empty array with the same constraints + */ + public function createEmpty(): self + { + return new self($this->getElementType(), $this->size); + } +} diff --git a/src/Composite/Arrays/FloatArray.php b/src/Composite/Arrays/FloatArray.php index 50591d4..36db7f4 100644 --- a/src/Composite/Arrays/FloatArray.php +++ b/src/Composite/Arrays/FloatArray.php @@ -4,94 +4,37 @@ namespace Nejcc\PhpDatatypes\Composite\Arrays; -use Countable; -use ArrayAccess; -use IteratorAggregate; -use Traversable; +use Nejcc\PhpDatatypes\Abstract\ArrayAbstraction; use Nejcc\PhpDatatypes\Exceptions\InvalidFloatException; -readonly class FloatArray implements Countable, ArrayAccess, IteratorAggregate +final class FloatArray extends ArrayAbstraction implements \ArrayAccess { - /** - * @var array The float values. - */ - private array $value; - - /** - * Constructor for FloatArray. - * - * @param array $value The array of float values. - * @throws InvalidFloatException If any value is not a valid float. - */ - public function __construct(array $value) - { - $this->validateFloats($value); - $this->value = $value; - } - - /** - * Validate that all elements are floats. - * - * @param array $array The array to validate. - * @throws InvalidFloatException If any element is not a valid float. - * @return void - */ - private function validateFloats(array $array): void + public function __construct(array $value, bool $trusted = false) { - foreach ($array as $item) { - if (!is_float($item)) { - throw new InvalidFloatException("All elements must be floats. Invalid value: " . json_encode($item)); - } + if (!$trusted) { + $this->validateFloats($value); } + parent::__construct($value); } /** - * Get the array of float values. - * - * @return array The float array. + * Construct without validation. Caller must guarantee every element is a float. */ - public function getValue(): array + public static function fromTrusted(array $value): self { - return $this->value; + return new self($value, true); } - /** - * Get the float at a specific index. - * - * @param int $index The index. - * @return float|null The float value or null if index is out of bounds. - */ - public function getFloat(int $index): ?float + public function get(int $index): ?float { return $this->value[$index] ?? null; } - /** - * Get the count of floats in the array. - * - * @return int The number of floats. - */ - public function count(): int - { - return count($this->value); - } - - /** - * Calculate the sum of the float array. - * - * @return float The sum of the floats. - */ public function sum(): float { return array_sum($this->value); } - /** - * Calculate the average of the float array. - * - * @return float The average value of the floats. - * @throws InvalidFloatException If the array is empty. - */ public function average(): float { if ($this->count() === 0) { @@ -100,90 +43,41 @@ public function average(): float return $this->sum() / $this->count(); } - /** - * Add new floats to the array (returns a new instance). - * - * @param float ...$floats The floats to add. - * @return FloatArray The new FloatArray with added floats. - * @throws InvalidFloatException If any value is not a valid float. - */ public function add(float ...$floats): self { $this->validateFloats($floats); return new self(array_merge($this->value, $floats)); } - /** - * Remove specific floats from the array (returns a new instance). - * - * @param float ...$floats The floats to remove. - * @return FloatArray The new FloatArray with removed floats. - * @throws InvalidFloatException - */ public function remove(float ...$floats): self { $newArray = $this->value; foreach ($floats as $float) { - $index = array_search($float, $newArray, true); - if ($index !== false) { + $index = array_find_key($newArray, fn($value) => $value === $float); + if ($index !== null) { unset($newArray[$index]); } } return new self(array_values($newArray)); } - /** - * ArrayAccess: Check if a float exists at the given offset. - * - * @param int $offset The array offset. - * @return bool True if offset exists, false otherwise. - */ public function offsetExists(mixed $offset): bool { return isset($this->value[$offset]); } - /** - * ArrayAccess: Get the float at the given offset. - * - * @param int $offset The array offset. - * @return float|null The float value or null if index does not exist. - */ public function offsetGet(mixed $offset): mixed { return $this->value[$offset] ?? null; } - /** - * ArrayAccess: Prevent modification by throwing an exception. - * - * @param mixed $offset The array offset. - * @param mixed $value The value to set (not allowed). - * @throws InvalidFloatException Always thrown since FloatArray is immutable. - */ public function offsetSet(mixed $offset, mixed $value): void { throw new InvalidFloatException("Cannot modify an immutable FloatArray."); } - /** - * ArrayAccess: Prevent unsetting by throwing an exception. - * - * @param int $offset The array offset. - * @throws InvalidFloatException Always thrown since FloatArray is immutable. - */ public function offsetUnset(mixed $offset): void { throw new InvalidFloatException("Cannot unset a value in an immutable FloatArray."); } - - /** - * Get an iterator for the float array. - * - * @return Traversable An iterator for the float array. - */ - public function getIterator(): Traversable - { - return new \ArrayIterator($this->value); - } } diff --git a/src/Composite/Arrays/IntArray.php b/src/Composite/Arrays/IntArray.php index b9ed89b..1826df8 100644 --- a/src/Composite/Arrays/IntArray.php +++ b/src/Composite/Arrays/IntArray.php @@ -1,20 +1,52 @@ value = $value; + parent::__construct($value); + } + + /** + * Construct without validation. Caller must guarantee every element is an int. + * Use only in performance-critical paths where the input is already trusted. + */ + public static function fromTrusted(array $value): self + { + return new self($value, true); + } + + public function get(int $index): int + { + if (!isset($this->value[$index])) { + throw new \OutOfRangeException("Index out of range"); + } + return $this->value[$index]; + } + + public function set(int $index, int $value): void + { + if (!isset($this->value[$index])) { + throw new \OutOfRangeException("Index out of range"); + } + $this->value[$index] = $value; } - public function getValue(): array { - return $this->value; + public function append(int $value): void + { + $this->value[] = $value; } } diff --git a/src/Composite/Arrays/StringArray.php b/src/Composite/Arrays/StringArray.php index 52aa079..c8d0aba 100644 --- a/src/Composite/Arrays/StringArray.php +++ b/src/Composite/Arrays/StringArray.php @@ -1,4 +1,5 @@ validateArray($value); + if (!$trusted) { + $this->validateStrings($value); + } $this->value = $value; } /** - * Validates that the array consists only of strings. - * - * @param array $array - * @return void - * @throws InvalidStringException + * Construct without validation. Caller must guarantee every element is a string. */ - private function validateArray(array $array): void + public static function fromTrusted(array $value): self { - foreach ($array as $item) { - if (!is_string($item)) { - throw new InvalidStringException("All elements must be strings. Invalid value: " . json_encode($item)); - } - } + return new self($value, true); } /** @@ -60,12 +56,14 @@ public function getValue(): array * Add multiple strings to the array (returns a new instance). * * @param string ...$strings + * * @return self New instance with added values. + * * @throws InvalidStringException */ public function add(string ...$strings): self { - $this->validateArray($strings); + $this->validateStrings($strings); return new self(array_merge($this->value, $strings)); } @@ -73,15 +71,17 @@ public function add(string ...$strings): self * Remove multiple strings from the array (returns a new instance). * * @param string ...$strings + * * @return self New instance with removed values. + * * @throws InvalidStringException */ public function remove(string ...$strings): self { $newArray = $this->value; foreach ($strings as $string) { - $index = array_search($string, $newArray, true); - if ($index !== false) { + $index = array_find_key($newArray, fn($value) => $value === $string); + if ($index !== null) { unset($newArray[$index]); } } @@ -92,16 +92,12 @@ public function remove(string ...$strings): self * Check if multiple strings exist in the array. * * @param string ...$strings + * * @return bool True if all strings are found, false otherwise. */ public function contains(string ...$strings): bool { - foreach ($strings as $string) { - if (!in_array($string, $this->value, true)) { - return false; - } - } - return true; + return array_all($strings, fn($string) => in_array($string, $this->value, true)); } /** @@ -118,6 +114,7 @@ public function count(): int * Get the array as a comma-separated string or with custom separator. * * @param string $separator Separator to use between strings (default: ", "). + * * @return string */ public function toString(string $separator = ', '): string @@ -129,11 +126,12 @@ public function toString(string $separator = ', '): string * Find strings that start with a specific prefix. * * @param string $prefix + * * @return array Array of strings that start with the given prefix. */ public function filterByPrefix(string $prefix): array { - return array_values(array_filter($this->value, fn($str) => str_starts_with($str, $prefix))); + return array_values(array_filter($this->value, fn ($str) => str_starts_with($str, $prefix))); } @@ -141,11 +139,12 @@ public function filterByPrefix(string $prefix): array * Find strings that contain a specific substring. * * @param string $substring + * * @return array Array of strings that contain the substring. */ public function filterBySubstring(string $substring): array { - return array_values(array_filter($this->value, fn($str) => str_contains($str, $substring))); + return array_values(array_filter($this->value, fn ($str) => str_contains($str, $substring))); } @@ -153,6 +152,7 @@ public function filterBySubstring(string $substring): array * Get a string at a specific index. * * @param int $index + * * @return string|null */ public function get(int $index): ?string @@ -164,6 +164,7 @@ public function get(int $index): ?string * Convert all strings to uppercase (returns a new instance). * * @return self + * * @throws InvalidStringException */ public function toUpperCase(): self @@ -175,6 +176,7 @@ public function toUpperCase(): self * Convert all strings to lowercase (returns a new instance). * * @return self + * * @throws InvalidStringException */ public function toLowerCase(): self @@ -186,6 +188,7 @@ public function toLowerCase(): self * Clear the array (returns a new empty instance). * * @return self + * * @throws InvalidStringException */ public function clear(): self @@ -197,6 +200,7 @@ public function clear(): self * ArrayAccess method to check if an offset exists. * * @param mixed $offset + * * @return bool */ public function offsetExists(mixed $offset): bool @@ -208,6 +212,7 @@ public function offsetExists(mixed $offset): bool * ArrayAccess method to get an offset. * * @param mixed $offset + * * @return mixed */ public function offsetGet(mixed $offset): mixed @@ -220,7 +225,9 @@ public function offsetGet(mixed $offset): mixed * * @param mixed $offset * @param mixed $value + * * @return void + * * @throws InvalidStringException */ public function offsetSet(mixed $offset, mixed $value): void @@ -232,7 +239,9 @@ public function offsetSet(mixed $offset, mixed $value): void * ArrayAccess method to unset an offset (immutable, returns a new instance). * * @param mixed $offset + * * @return void + * * @throws InvalidStringException */ public function offsetUnset(mixed $offset): void @@ -243,9 +252,9 @@ public function offsetUnset(mixed $offset): void /** * Returns an iterator for traversing the array. * - * @return Traversable + * @return \ArrayIterator */ - public function getIterator(): Traversable + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->value); } diff --git a/src/Composite/Arrays/TypeSafeArray.php b/src/Composite/Arrays/TypeSafeArray.php new file mode 100644 index 0000000..44ee645 --- /dev/null +++ b/src/Composite/Arrays/TypeSafeArray.php @@ -0,0 +1,278 @@ +elementType = $elementType; + $this->data = []; + + if (!empty($initialData)) { + $this->validateArray($initialData); + $this->data = $initialData; + } + } + + /** + * String representation of the array + * + * @return string + */ + public function __toString(): string + { + return json_encode($this->data); + } + + /** + * Get the type of elements this array accepts + * + * @return string The element type + */ + public function getElementType(): string + { + return $this->elementType; + } + + /** + * Get all elements in the array + * + * @return array The array elements + */ + public function toArray(): array + { + return $this->data; + } + + /** + * ArrayAccess implementation + */ + public function offsetExists(mixed $offset): bool + { + return isset($this->data[$offset]); + } + + public function offsetGet(mixed $offset): mixed + { + return $this->data[$offset] ?? null; + } + + public function offsetSet(mixed $offset, mixed $value): void + { + if (!$this->isValidType($value)) { + throw new TypeMismatchException( + "Value must be of type {$this->elementType}" + ); + } + + if (is_null($offset)) { + $this->data[] = $value; + } else { + $this->data[$offset] = $value; + } + } + + public function offsetUnset(mixed $offset): void + { + unset($this->data[$offset]); + } + + /** + * Countable implementation + */ + public function count(): int + { + return count($this->data); + } + + /** + * Iterator implementation + */ + public function current(): mixed + { + return $this->data[$this->position]; + } + + public function key(): mixed + { + return $this->position; + } + + public function next(): void + { + ++$this->position; + } + + public function rewind(): void + { + $this->position = 0; + } + + public function valid(): bool + { + return isset($this->data[$this->position]); + } + + /** + * Map operation - apply a callback to each element + * + * @param callable $callback The callback to apply + * + * @return TypeSafeArray A new array with the mapped values + * + * @throws TypeMismatchException If the callback returns invalid types + */ + public function map(callable $callback): self + { + $result = new self($this->elementType); + foreach ($this->data as $key => $value) { + $result[$key] = $callback($value, $key); + } + return $result; + } + + /** + * Filter operation - filter elements based on a callback + * + * @param callable $callback The callback to use for filtering + * + * @return TypeSafeArray A new array with the filtered values + */ + public function filter(callable $callback): self + { + $result = new self($this->elementType); + foreach ($this->data as $key => $value) { + if ($callback($value, $key)) { + $result[$key] = $value; + } + } + return $result; + } + + /** + * Reduce operation - reduce the array to a single value + * + * @param callable $callback The callback to use for reduction + * @param mixed $initial The initial value + * + * @return mixed The reduced value + */ + public function reduce(callable $callback, $initial = null) + { + return array_reduce($this->data, $callback, $initial); + } + + /** + * Get the array value + * + * @return array The array data + */ + public function getValue(): array + { + return $this->data; + } + + /** + * Set the array value + * + * @param mixed $value The new array data + * + * @throws TypeMismatchException If any element doesn't match the required type + */ + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new TypeMismatchException('Value must be an array.'); + } + $this->validateArray($value); + $this->data = $value; + } + + /** + * Check if this array equals another array + * + * @param DataTypeInterface $other The other array to compare with + * + * @return bool True if the arrays are equal + */ + public function equals(DataTypeInterface $other): bool + { + if (!$other instanceof self) { + return false; + } + + if ($this->elementType !== $other->elementType) { + return false; + } + + return $this->data === $other->data; + } + + /** + * Check if a value matches the required type + * + * @param mixed $value The value to check + * + * @return bool True if the value matches the required type + */ + protected function isValidType(mixed $value): bool + { + return $value instanceof $this->elementType; + } + + /** + * Validate that all elements in an array match the required type + * + * @param array $data The array to validate + * + * @throws TypeMismatchException If any element doesn't match the required type + */ + private function validateArray(array $data): void + { + if (!array_all($data, fn($value) => $this->isValidType($value))) { + $invalidKey = array_find_key($data, fn($value) => !$this->isValidType($value)); + throw new TypeMismatchException( + "Element at key '{$invalidKey}' must be of type {$this->elementType}" + ); + } + } +} diff --git a/src/Composite/Dictionary.php b/src/Composite/Dictionary.php index 7229629..970fedb 100644 --- a/src/Composite/Dictionary.php +++ b/src/Composite/Dictionary.php @@ -1,4 +1,5 @@ $elements + * * @throws InvalidArgumentException + * * @return void */ public function __construct(array $elements = []) @@ -40,6 +43,7 @@ public function __construct(array $elements = []) * * @param string $key * @param mixed $value + * * @return void */ public function add(string $key, mixed $value): void @@ -51,7 +55,9 @@ public function add(string $key, mixed $value): void * Get the value associated with a key. * * @param string $key + * * @throws OutOfBoundsException + * * @return mixed */ public function get(string $key): mixed @@ -67,7 +73,9 @@ public function get(string $key): mixed * Remove a key-value pair by the key. * * @param string $key + * * @throws OutOfBoundsException + * * @return void */ public function remove(string $key): void @@ -83,6 +91,7 @@ public function remove(string $key): void * Check if a key exists in the dictionary. * * @param string $key + * * @return bool */ public function containsKey(string $key): bool @@ -129,4 +138,34 @@ public function clear(): void { $this->elements = []; } + + /** + * Convert the dictionary to an array. + * + * @return array + */ + public function toArray(): array + { + return $this->elements; + } + + /** + * Check if the dictionary is empty. + * + * @return bool + */ + public function isEmpty(): bool + { + return empty($this->elements); + } + + /** + * Get a copy of the dictionary with all elements. + * + * @return array + */ + public function getAll(): array + { + return $this->elements; + } } diff --git a/src/Composite/Json.php b/src/Composite/Json.php index a88db75..1f6067c 100644 --- a/src/Composite/Json.php +++ b/src/Composite/Json.php @@ -6,15 +6,15 @@ use InvalidArgumentException; use JsonException; +use Nejcc\PhpDatatypes\Abstract\ArrayAbstraction; use Nejcc\PhpDatatypes\Interfaces\DecoderInterface; use Nejcc\PhpDatatypes\Interfaces\EncoderInterface; - /** * Class Json * A strict and immutable type for handling JSON data with advanced features. */ -final class Json +final class Json extends ArrayAbstraction { /** * @var string The JSON string. @@ -36,6 +36,7 @@ final class Json * * @param string $json The JSON string. * @param string|null $schema Optional JSON schema for validation. + * * @throws InvalidArgumentException If the JSON is invalid or does not comply with the schema. */ public function __construct(string $json, ?string $schema = null) @@ -43,28 +44,14 @@ public function __construct(string $json, ?string $schema = null) $this->validateJson($json); $this->schema = $schema; $this->json = $json; + parent::__construct([]); // Not used, but required by ArrayAbstraction } - /** - * Validates if a string is valid JSON. - * - * @param string $json - * @throws InvalidArgumentException - */ - private function validateJson(string $json): void - { - try { - json_decode($json, true, 512, JSON_THROW_ON_ERROR); - } catch (JsonException $e) { - throw new InvalidArgumentException('Invalid JSON provided: ' . $e->getMessage()); - } - } - - /** * Serializes the JSON data to an array. * * @return array + * * @throws JsonException */ public function toArray(): array @@ -80,6 +67,7 @@ public function toArray(): array * Serializes the JSON data to an object. * * @return object + * * @throws JsonException */ public function toObject(): object @@ -92,7 +80,9 @@ public function toObject(): object * * @param array $data * @param string|null $schema + * * @return self + * * @throws InvalidArgumentException * @throws JsonException */ @@ -107,7 +97,9 @@ public static function fromArray(array $data, ?string $schema = null): self * * @param object $object * @param string|null $schema + * * @return self + * * @throws InvalidArgumentException * @throws JsonException */ @@ -131,6 +123,7 @@ public function getJson(): string * Compresses the JSON string using the provided encoder. * * @param EncoderInterface $encoder + * * @return string The compressed string. */ public function compress(EncoderInterface $encoder): string @@ -143,7 +136,9 @@ public function compress(EncoderInterface $encoder): string * * @param DecoderInterface $decoder * @param string $compressed + * * @return self + * * @throws InvalidArgumentException */ public static function decompress(DecoderInterface $decoder, string $compressed): self @@ -157,7 +152,9 @@ public static function decompress(DecoderInterface $decoder, string $compressed) * In case of conflicting keys, values from the other Json take precedence. * * @param Json $other + * * @return self + * * @throws JsonException */ public function merge(Json $other): self @@ -172,7 +169,9 @@ public function merge(Json $other): self * * @param string $key * @param mixed $value + * * @return self + * * @throws JsonException */ public function update(string $key, mixed $value): self @@ -187,7 +186,9 @@ public function update(string $key, mixed $value): self * Removes a key from the JSON data. * * @param string $key + * * @return self + * * @throws JsonException */ public function remove(string $key): self diff --git a/src/Composite/ListData.php b/src/Composite/ListData.php index 3a3b042..b4e9b0e 100644 --- a/src/Composite/ListData.php +++ b/src/Composite/ListData.php @@ -1,11 +1,12 @@ $elements + * * @return void */ public function __construct(array $elements = []) @@ -29,6 +31,7 @@ public function __construct(array $elements = []) * Add an element to the list. * * @param mixed $element + * * @return void */ public function add(mixed $element): void @@ -40,7 +43,9 @@ public function add(mixed $element): void * Remove an element by its index. * * @param int $index + * * @throws OutOfBoundsException + * * @return void */ public function remove(int $index): void @@ -56,7 +61,9 @@ public function remove(int $index): void * Get an element by its index. * * @param int $index + * * @throws OutOfBoundsException + * * @return mixed */ public function get(int $index): mixed @@ -82,6 +89,7 @@ public function getAll(): array * Check if the list contains an element. * * @param mixed $element + * * @return bool */ public function contains(mixed $element): bool diff --git a/src/Composite/Option.php b/src/Composite/Option.php new file mode 100644 index 0000000..0903828 --- /dev/null +++ b/src/Composite/Option.php @@ -0,0 +1,299 @@ +value = $value; + $this->isSome = $isSome; + } + + /** + * Create a Some Option with a value + * + * @param T $value + * @return self + */ + public static function some(mixed $value): self + { + return new self($value, true); + } + + /** + * Create a None Option + * + * @return self + */ + public static function none(): self + { + return new self(null, false); + } + + /** + * Create an Option from a nullable value + * + * @param T|null $value + * @return self + */ + public static function fromNullable(mixed $value): self + { + return $value === null ? self::none() : self::some($value); + } + + /** + * Check if this Option contains a value + * + * @return bool + */ + public function isSome(): bool + { + return $this->isSome; + } + + /** + * Check if this Option is empty + * + * @return bool + */ + public function isNone(): bool + { + return !$this->isSome; + } + + /** + * Get the value if Some, throw exception if None + * + * @return T + * @throws InvalidArgumentException + */ + public function unwrap(): mixed + { + if ($this->isNone()) { + throw new InvalidArgumentException('Cannot unwrap None Option'); + } + return $this->value; + } + + /** + * Get the value if Some, return default if None + * + * @param T $default + * @return T + */ + public function unwrapOr(mixed $default): mixed + { + return $this->isSome() ? $this->value : $default; + } + + /** + * Get the value if Some, return result of callback if None + * + * @param callable(): T $callback + * @return T + */ + public function unwrapOrElse(callable $callback): mixed + { + return $this->isSome() ? $this->value : $callback(); + } + + /** + * Transform the value if Some, return None if None + * + * @template U + * @param callable(T): U $callback + * @return self + */ + public function map(callable $callback): self + { + return $this->isSome() + ? self::some($callback($this->value)) + : self::none(); + } + + /** + * Transform the value if Some, return default if None + * + * @template U + * @param callable(T): U $callback + * @param U $default + * @return U + */ + public function mapOr(callable $callback, mixed $default): mixed + { + return $this->isSome() ? $callback($this->value) : $default; + } + + /** + * Transform the value if Some, return result of callback if None + * + * @template U + * @param callable(T): U $callback + * @param callable(): U $defaultCallback + * @return U + */ + public function mapOrElse(callable $callback, callable $defaultCallback): mixed + { + return $this->isSome() ? $callback($this->value) : $defaultCallback(); + } + + /** + * Chain another Option if this is Some + * + * @template U + * @param callable(T): self $callback + * @return self + */ + public function andThen(callable $callback): self + { + return $this->isSome() ? $callback($this->value) : self::none(); + } + + /** + * Return this Option if Some, return other if None + * + * @param self $other + * @return self + */ + public function or(self $other): self + { + return $this->isSome() ? $this : $other; + } + + /** + * Return this Option if Some, return result of callback if None + * + * @param callable(): self $callback + * @return self + */ + public function orElse(callable $callback): self + { + return $this->isSome() ? $this : $callback(); + } + + /** + * Filter the value if Some based on predicate + * + * @param callable(T): bool $predicate + * @return self + */ + public function filter(callable $predicate): self + { + return $this->isSome() && $predicate($this->value) ? $this : self::none(); + } + + /** + * Check if this Option equals another Option + * + * @param self $other + * @return bool + */ + public function equals(self $other): bool + { + if ($this->isSome() !== $other->isSome()) { + return false; + } + + if ($this->isNone()) { + return true; + } + + return $this->value === $other->value; + } + + /** + * Convert to array representation + * + * @return array{isSome: bool, value: T|null} + */ + public function toArray(): array + { + return [ + 'isSome' => $this->isSome, + 'value' => $this->value + ]; + } + + /** + * Create from array representation + * + * @param array{isSome: bool, value: T|null} $data + * @return self + */ + public static function fromArray(array $data): self + { + if (!isset($data['isSome']) || !is_bool($data['isSome'])) { + throw new InvalidArgumentException('Invalid Option array format'); + } + + return new self($data['value'] ?? null, $data['isSome']); + } + + /** + * Convert to JSON string + * + * @return string + */ + public function toJson(): string + { + return json_encode($this->toArray()); + } + + /** + * Create from JSON string + * + * @param string $json + * @return self + * @throws InvalidArgumentException + */ + public static function fromJson(string $json): self + { + $data = json_decode($json, true); + if (!is_array($data)) { + throw new InvalidArgumentException('Invalid JSON format for Option'); + } + + return self::fromArray($data); + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->isSome() + ? sprintf('Some(%s)', var_export($this->value, true)) + : 'None'; + } +} diff --git a/src/Composite/Result.php b/src/Composite/Result.php new file mode 100644 index 0000000..ac26158 --- /dev/null +++ b/src/Composite/Result.php @@ -0,0 +1,342 @@ +value = $value; + $this->isOk = $isOk; + } + + /** + * Create an Ok Result with a value + * + * @param T $value + * @return self + */ + public static function ok(mixed $value): self + { + return new self($value, true); + } + + /** + * Create an Err Result with an error + * + * @param E $error + * @return self + */ + public static function err(mixed $error): self + { + return new self($error, false); + } + + /** + * Create a Result from a callable that might throw + * + * @param callable(): T $callable + * @return self + */ + public static function try(callable $callable): self + { + try { + return self::ok($callable()); + } catch (\Throwable $e) { + return self::err($e); + } + } + + /** + * Check if this Result is Ok + * + * @return bool + */ + public function isOk(): bool + { + return $this->isOk; + } + + /** + * Check if this Result is Err + * + * @return bool + */ + public function isErr(): bool + { + return !$this->isOk; + } + + /** + * Get the value if Ok, throw exception if Err + * + * @return T + * @throws InvalidArgumentException + */ + public function unwrap(): mixed + { + if ($this->isErr()) { + throw new InvalidArgumentException('Cannot unwrap Err Result'); + } + return $this->value; + } + + /** + * Get the error if Err, throw exception if Ok + * + * @return E + * @throws InvalidArgumentException + */ + public function unwrapErr(): mixed + { + if ($this->isOk()) { + throw new InvalidArgumentException('Cannot unwrap error from Ok Result'); + } + return $this->value; + } + + /** + * Get the value if Ok, return default if Err + * + * @param T $default + * @return T + */ + public function unwrapOr(mixed $default): mixed + { + return $this->isOk() ? $this->value : $default; + } + + /** + * Get the value if Ok, return result of callback if Err + * + * @param callable(E): T $callback + * @return T + */ + public function unwrapOrElse(callable $callback): mixed + { + return $this->isOk() ? $this->value : $callback($this->value); + } + + /** + * Transform the value if Ok, return Err if Err + * + * @template U + * @param callable(T): U $callback + * @return self + */ + public function map(callable $callback): self + { + return $this->isOk() + ? self::ok($callback($this->value)) + : self::err($this->value); + } + + /** + * Transform the error if Err, return Ok if Ok + * + * @template F + * @param callable(E): F $callback + * @return self + */ + public function mapErr(callable $callback): self + { + return $this->isErr() + ? self::err($callback($this->value)) + : self::ok($this->value); + } + + /** + * Transform the value if Ok, return default if Err + * + * @template U + * @param callable(T): U $callback + * @param U $default + * @return U + */ + public function mapOr(callable $callback, mixed $default): mixed + { + return $this->isOk() ? $callback($this->value) : $default; + } + + /** + * Transform the value if Ok, return result of callback if Err + * + * @template U + * @param callable(T): U $callback + * @param callable(E): U $defaultCallback + * @return U + */ + public function mapOrElse(callable $callback, callable $defaultCallback): mixed + { + return $this->isOk() ? $callback($this->value) : $defaultCallback($this->value); + } + + /** + * Chain another Result if this is Ok + * + * @template U + * @param callable(T): self $callback + * @return self + */ + public function andThen(callable $callback): self + { + return $this->isOk() ? $callback($this->value) : self::err($this->value); + } + + /** + * Return this Result if Ok, return other if Err + * + * @param self $other + * @return self + */ + public function or(self $other): self + { + return $this->isOk() ? $this : $other; + } + + /** + * Return this Result if Ok, return result of callback if Err + * + * @param callable(E): self $callback + * @return self + */ + public function orElse(callable $callback): self + { + return $this->isOk() ? $this : $callback($this->value); + } + + /** + * Convert to Option: Some(value) if Ok, None if Err + * + * @return \Nejcc\PhpDatatypes\Composite\Option + */ + public function toOption(): \Nejcc\PhpDatatypes\Composite\Option + { + return $this->isOk() + ? \Nejcc\PhpDatatypes\Composite\Option::some($this->value) + : \Nejcc\PhpDatatypes\Composite\Option::none(); + } + + /** + * Convert to Option: Some(error) if Err, None if Ok + * + * @return \Nejcc\PhpDatatypes\Composite\Option + */ + public function toErrorOption(): \Nejcc\PhpDatatypes\Composite\Option + { + return $this->isErr() + ? \Nejcc\PhpDatatypes\Composite\Option::some($this->value) + : \Nejcc\PhpDatatypes\Composite\Option::none(); + } + + /** + * Check if this Result equals another Result + * + * @param self $other + * @return bool + */ + public function equals(self $other): bool + { + if ($this->isOk() !== $other->isOk()) { + return false; + } + + return $this->value === $other->value; + } + + /** + * Convert to array representation + * + * @return array{isOk: bool, value: T|E} + */ + public function toArray(): array + { + return [ + 'isOk' => $this->isOk, + 'value' => $this->value + ]; + } + + /** + * Create from array representation + * + * @param array{isOk: bool, value: T|E} $data + * @return self + */ + public static function fromArray(array $data): self + { + if (!isset($data['isOk']) || !is_bool($data['isOk'])) { + throw new InvalidArgumentException('Invalid Result array format'); + } + + return new self($data['value'] ?? null, $data['isOk']); + } + + /** + * Convert to JSON string + * + * @return string + */ + public function toJson(): string + { + return json_encode($this->toArray()); + } + + /** + * Create from JSON string + * + * @param string $json + * @return self + * @throws InvalidArgumentException + */ + public static function fromJson(string $json): self + { + $data = json_decode($json, true); + if (!is_array($data)) { + throw new InvalidArgumentException('Invalid JSON format for Result'); + } + + return self::fromArray($data); + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->isOk() + ? sprintf('Ok(%s)', var_export($this->value, true)) + : sprintf('Err(%s)', var_export($this->value, true)); + } +} diff --git a/src/Composite/String/AsciiString.php b/src/Composite/String/AsciiString.php new file mode 100644 index 0000000..25906f9 --- /dev/null +++ b/src/Composite/String/AsciiString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Base64String.php b/src/Composite/String/Base64String.php new file mode 100644 index 0000000..c98e366 --- /dev/null +++ b/src/Composite/String/Base64String.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/ColorString.php b/src/Composite/String/ColorString.php new file mode 100644 index 0000000..ed4f581 --- /dev/null +++ b/src/Composite/String/ColorString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/CommandString.php b/src/Composite/String/CommandString.php new file mode 100644 index 0000000..31225a4 --- /dev/null +++ b/src/Composite/String/CommandString.php @@ -0,0 +1,50 @@ +()\'"`\s]+$/', $value)) { + throw new InvalidArgumentException('Invalid command string format'); + } + $this->value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/CssString.php b/src/Composite/String/CssString.php new file mode 100644 index 0000000..9652cea --- /dev/null +++ b/src/Composite/String/CssString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/EmailString.php b/src/Composite/String/EmailString.php new file mode 100644 index 0000000..2b99142 --- /dev/null +++ b/src/Composite/String/EmailString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/HexString.php b/src/Composite/String/HexString.php new file mode 100644 index 0000000..ac3280f --- /dev/null +++ b/src/Composite/String/HexString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/HtmlString.php b/src/Composite/String/HtmlString.php new file mode 100644 index 0000000..b6abbb7 --- /dev/null +++ b/src/Composite/String/HtmlString.php @@ -0,0 +1,56 @@ +loadHTML($value, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + $errors = libxml_get_errors(); + libxml_clear_errors(); + libxml_use_internal_errors($previous); + + if (!empty($errors)) { + throw new InvalidArgumentException('Invalid HTML string format'); + } + $this->value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/IpString.php b/src/Composite/String/IpString.php new file mode 100644 index 0000000..8944323 --- /dev/null +++ b/src/Composite/String/IpString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/JsString.php b/src/Composite/String/JsString.php new file mode 100644 index 0000000..ab3e9a0 --- /dev/null +++ b/src/Composite/String/JsString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/JsonString.php b/src/Composite/String/JsonString.php new file mode 100644 index 0000000..bb3173a --- /dev/null +++ b/src/Composite/String/JsonString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/MacString.php b/src/Composite/String/MacString.php new file mode 100644 index 0000000..5d224ba --- /dev/null +++ b/src/Composite/String/MacString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/PasswordString.php b/src/Composite/String/PasswordString.php new file mode 100644 index 0000000..1cd9660 --- /dev/null +++ b/src/Composite/String/PasswordString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/PathString.php b/src/Composite/String/PathString.php new file mode 100644 index 0000000..5b57640 --- /dev/null +++ b/src/Composite/String/PathString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/RegexString.php b/src/Composite/String/RegexString.php new file mode 100644 index 0000000..02d5bc8 --- /dev/null +++ b/src/Composite/String/RegexString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/SemverString.php b/src/Composite/String/SemverString.php new file mode 100644 index 0000000..1c63df5 --- /dev/null +++ b/src/Composite/String/SemverString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/SlugString.php b/src/Composite/String/SlugString.php new file mode 100644 index 0000000..d29ee1b --- /dev/null +++ b/src/Composite/String/SlugString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/SqlString.php b/src/Composite/String/SqlString.php new file mode 100644 index 0000000..f45ab98 --- /dev/null +++ b/src/Composite/String/SqlString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Str16.php b/src/Composite/String/Str16.php new file mode 100644 index 0000000..778b765 --- /dev/null +++ b/src/Composite/String/Str16.php @@ -0,0 +1,52 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Str32.php b/src/Composite/String/Str32.php new file mode 100644 index 0000000..98f31d2 --- /dev/null +++ b/src/Composite/String/Str32.php @@ -0,0 +1,52 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Str36.php b/src/Composite/String/Str36.php new file mode 100644 index 0000000..177a6ad --- /dev/null +++ b/src/Composite/String/Str36.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Str64.php b/src/Composite/String/Str64.php new file mode 100644 index 0000000..bfcd148 --- /dev/null +++ b/src/Composite/String/Str64.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Str8.php b/src/Composite/String/Str8.php new file mode 100644 index 0000000..b4abf6c --- /dev/null +++ b/src/Composite/String/Str8.php @@ -0,0 +1,52 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/TrimmedString.php b/src/Composite/String/TrimmedString.php new file mode 100644 index 0000000..e1b5f69 --- /dev/null +++ b/src/Composite/String/TrimmedString.php @@ -0,0 +1,50 @@ +value = $trimmed; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/UrlString.php b/src/Composite/String/UrlString.php new file mode 100644 index 0000000..e43822b --- /dev/null +++ b/src/Composite/String/UrlString.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/Utf8String.php b/src/Composite/String/Utf8String.php new file mode 100644 index 0000000..b02032c --- /dev/null +++ b/src/Composite/String/Utf8String.php @@ -0,0 +1,49 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/UuidString.php b/src/Composite/String/UuidString.php new file mode 100644 index 0000000..c16a4b7 --- /dev/null +++ b/src/Composite/String/UuidString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/VersionString.php b/src/Composite/String/VersionString.php new file mode 100644 index 0000000..289c812 --- /dev/null +++ b/src/Composite/String/VersionString.php @@ -0,0 +1,50 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/String/XmlString.php b/src/Composite/String/XmlString.php new file mode 100644 index 0000000..4ddca25 --- /dev/null +++ b/src/Composite/String/XmlString.php @@ -0,0 +1,55 @@ +value = $value; + } + + /** + * Get the string value + * + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + return $this->value; + } +} \ No newline at end of file diff --git a/src/Composite/Struct/AdvancedStruct.php b/src/Composite/Struct/AdvancedStruct.php new file mode 100644 index 0000000..fc46a79 --- /dev/null +++ b/src/Composite/Struct/AdvancedStruct.php @@ -0,0 +1,122 @@ +schema = $schema; + foreach ($schema as $field => $def) { + $alias = $def['alias'] ?? $field; + $type = $def['type'] ?? 'mixed'; + $nullable = $def['nullable'] ?? false; + $default = $def['default'] ?? null; + $rules = $def['rules'] ?? []; + $value = $values[$field] ?? $values[$alias] ?? $default; + if ($value === null && !$nullable && $default === null && !array_key_exists($field, $values)) { + throw new InvalidArgumentException("Field '$field' is required and has no value"); + } + if ($value !== null) { + $this->validateField($field, $value, $type, $rules, $nullable); + } + $this->data[$field] = $value; + } + } + + protected function validateField(string $field, $value, $type, array $rules, bool $nullable): void + { + if ($value === null && $nullable) { + return; + } + // Type check + if ($type !== 'mixed' && !$this->isValidType($value, $type)) { + throw new InvalidArgumentException("Field '$field' must be of type $type"); + } + // Rules + foreach ($rules as $rule) { + if (is_callable($rule)) { + if (!$rule($value)) { + throw new ValidationException("Validation failed for field '$field'"); + } + } + } + } + + protected function isValidType($value, $type): bool + { + if ($type === 'int' || $type === 'integer') return is_int($value); + if ($type === 'float' || $type === 'double') return is_float($value); + if ($type === 'string') return is_string($value); + if ($type === 'bool' || $type === 'boolean') return is_bool($value); + if ($type === 'array') return is_array($value); + if ($type === 'object') return is_object($value); + if (class_exists($type)) return $value instanceof $type; + return true; + } + + public function get(string $field) + { + return $this->data[$field] ?? null; + } + + public function toArray(bool $useAliases = false): array + { + $result = []; + foreach ($this->schema as $field => $def) { + $alias = $def['alias'] ?? $field; + $value = $this->data[$field]; + if ($value instanceof self) { + $value = $value->toArray($useAliases); + } + $result[$useAliases ? $alias : $field] = $value; + } + return $result; + } + + public static function fromArray(array $schema, array $data): self + { + return new self($schema, $data); + } + + public function toJson(bool $useAliases = false): string + { + return json_encode($this->toArray($useAliases)); + } + + public static function fromJson(array $schema, string $json): self + { + $data = json_decode($json, true); + return new self($schema, $data); + } + + public function toXml(bool $useAliases = false): string + { + $arr = $this->toArray($useAliases); + $xml = new \SimpleXMLElement(''); + foreach ($arr as $k => $v) { + $xml->addChild($k, htmlspecialchars((string)$v)); + } + return $xml->asXML(); + } + + public static function fromXml(array $schema, string $xml): self + { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false) { + foreach ($data as $k => $v) { + $arr[$k] = (string)$v; + } + } + return new self($schema, $arr); + } +} \ No newline at end of file diff --git a/src/Composite/Struct/ImmutableStruct.php b/src/Composite/Struct/ImmutableStruct.php new file mode 100644 index 0000000..d33c0e7 --- /dev/null +++ b/src/Composite/Struct/ImmutableStruct.php @@ -0,0 +1,484 @@ + The struct fields + */ + private array $fields = []; + + /** + * @var bool Whether the struct is frozen (immutable) + */ + private bool $frozen = false; + + /** + * @var array The struct data + */ + private array $data; + + /** + * @var array The validation rules for each field + */ + private array $rules; + + /** + * @var ImmutableStruct|null The parent struct for inheritance + */ + private ?ImmutableStruct $parent = null; + + /** + * Create a new ImmutableStruct instance + * + * @param array $fieldDefinitions Field definitions + * @param array $initialValues Initial values for fields + * @param ImmutableStruct|null $parent Optional parent struct for inheritance + * @throws InvalidArgumentException If field definitions are invalid or initial values don't match + * @throws ValidationException If validation rules fail + */ + public function __construct(array $fieldDefinitions, array $initialValues = [], ?ImmutableStruct $parent = null) + { + $this->parent = $parent; + $this->fields = []; + + // Initialize fields from parent if present + if ($parent !== null) { + foreach ($parent->getFields() as $name => $field) { + $this->fields[$name] = [ + 'type' => $field['type'], + 'value' => $field['value'], + 'required' => $field['required'], + 'default' => $field['default'], + 'rules' => $field['rules'] + ]; + } + } + + // Initialize child fields, overriding parent fields if they exist + $this->initializeFields($fieldDefinitions); + $this->setInitialValues($initialValues); + $this->frozen = true; + } + + /** + * Validate the struct data + * + * @throws ValidationException If validation fails + */ + private function validate(): void + { + // Validate parent struct if it exists + if ($this->parent !== null) { + $this->parent->validate(); + } + + // Validate current struct + foreach ($this->rules as $field => $fieldRules) { + if (!isset($this->data[$field])) { + throw new ValidationException("Field '{$field}' is required"); + } + foreach ($fieldRules as $rule) { + $rule->validate($this->data[$field]); + } + } + } + + /** + * Get the parent struct + * + * @return ImmutableStruct|null + */ + public function getParent(): ?ImmutableStruct + { + return $this->parent; + } + + /** + * Check if this struct has a parent + * + * @return bool + */ + public function hasParent(): bool + { + return $this->parent !== null; + } + + /** + * Get all fields including inherited fields + * + * @return array + */ + public function getAllFields(): array + { + $result = []; + foreach ($this->fields as $name => $field) { + $value = $field['value']; + if ($value instanceof StructInterface) { + $result[$name] = $value->toArray(); + } else { + $result[$name] = $value; + } + } + return $result; + } + + /** + * Get all validation rules including inherited rules + * + * @return array + */ + public function getAllRules(): array + { + $rules = []; + foreach ($this->fields as $name => $field) { + $rules[$name] = $field['rules']; + } + return $rules; + } + + /** + * Get a field value + * + * @param string $field The field name + * @return mixed The field value + * @throws InvalidArgumentException If the field does not exist + */ + public function getField(string $field): mixed + { + if (!isset($this->data[$field])) { + throw new InvalidArgumentException("Field '{$field}' does not exist in the struct"); + } + return $this->data[$field]; + } + + /** + * Set a field value + * + * @param string $field The field name + * @param mixed $value The field value + * @throws InvalidArgumentException If the field does not exist + * @throws ImmutableException If the struct is immutable + */ + public function setField(string $field, mixed $value): void + { + if (!isset($this->data[$field])) { + throw new InvalidArgumentException("Field '{$field}' does not exist in the struct"); + } + if ($this->frozen) { + throw new ImmutableException("Cannot modify an immutable struct"); + } + $this->data[$field] = $value; + } + + /** + * Check if a field exists + * + * @param string $field The field name + * @return bool True if the field exists, false otherwise + */ + public function hasField(string $field): bool + { + return isset($this->data[$field]); + } + + /** + * Get all field names + * + * @return array The field names + */ + public function getFieldNames(): array + { + return array_keys($this->data); + } + + /** + * Get all field values + * + * @return array The field values + */ + public function getFieldValues(): array + { + return $this->data; + } + + /** + * Get the validation rules for a field + * + * @param string $field The field name + * @return ValidationRule[] The validation rules + * @throws InvalidArgumentException If the field does not exist + */ + public function getFieldRules(string $field): array + { + if (!isset($this->fields[$field])) { + throw new InvalidArgumentException("Field '$field' does not exist in the struct"); + } + return $this->fields[$field]['rules']; + } + + /** + * Check if a field is required + * + * @param string $field The field name + * @return bool True if the field is required, false otherwise + * @throws InvalidArgumentException If the field does not exist + */ + public function isFieldRequired(string $field): bool + { + if (!isset($this->fields[$field])) { + throw new InvalidArgumentException("Field '$field' does not exist in the struct"); + } + return $this->fields[$field]['required']; + } + + /** + * Get the type of a field + * + * @param string $field The field name + * @return string The field type + * @throws InvalidArgumentException If the field does not exist + */ + public function getFieldType(string $field): string + { + if (!isset($this->fields[$field])) { + throw new InvalidArgumentException("Field '$field' does not exist in the struct"); + } + return $this->fields[$field]['type']; + } + + /** + * Convert the struct to an array + * + * @return array The struct data + */ + public function toArray(): array + { + $result = []; + foreach ($this->fields as $name => $field) { + $value = $field['value']; + if ($value instanceof StructInterface) { + $result[$name] = $value->toArray(); + } else { + $result[$name] = $value; + } + } + return $result; + } + + /** + * Convert the struct to a string + * + * @return string The struct data as a string + */ + public function __toString(): string + { + return json_encode($this->toArray()); + } + + /** + * Create a new struct with updated values + * + * @param array $values New values to set + * + * @return self A new struct instance with the updated values + * + * @throws InvalidArgumentException If values don't match field definitions + * @throws ValidationException If validation rules fail + */ + public function with(array $values): self + { + $newFields = []; + foreach ($this->fields as $name => $field) { + $newFields[$name] = [ + 'type' => $field['type'], + 'required' => $field['required'], + 'default' => $field['default'], + 'rules' => $field['rules'] + ]; + } + + $newStruct = new self($newFields, $values); + return $newStruct; + } + + /** + * Get a new struct with a single field updated + * + * @param string $name Field name + * @param mixed $value New value + * + * @return self A new struct instance with the updated field + * + * @throws InvalidArgumentException If the field doesn't exist or value doesn't match type + * @throws ValidationException If validation rules fail + */ + public function withField(string $name, mixed $value): self + { + return $this->with([$name => $value]); + } + + /** + * Initialize the struct fields from definitions + * + * @param array $fieldDefinitions + * + * @throws InvalidArgumentException If field definitions are invalid + */ + private function initializeFields(array $fieldDefinitions): void + { + foreach ($fieldDefinitions as $name => $definition) { + if (!isset($definition['type'])) { + throw new InvalidArgumentException("Field '$name' must have a type definition"); + } + $this->fields[$name] = [ + 'type' => $definition['type'], + 'value' => $definition['default'] ?? null, + 'required' => $definition['required'] ?? false, + 'default' => $definition['default'] ?? null, + 'rules' => $definition['rules'] ?? [] + ]; + } + } + + /** + * Set initial values for fields + * + * @param array $initialValues + * + * @throws InvalidArgumentException If initial values don't match field definitions + * @throws ValidationException If validation rules fail + */ + private function setInitialValues(array $initialValues): void + { + foreach ($initialValues as $name => $value) { + if (!isset($this->fields[$name])) { + throw new InvalidArgumentException("Field '$name' is not defined in the struct"); + } + $this->set($name, $value); + } + // Validate required fields + foreach ($this->fields as $name => $field) { + if ($field['required'] && $field['value'] === null) { + throw new InvalidArgumentException("Required field '$name' has no value"); + } + } + } + + /** + * Validate a value against a field's type and rules + * + * @param string $name Field name + * @param mixed $value Value to validate + * + * @throws InvalidArgumentException If the value doesn't match the field type + * @throws ValidationException If validation rules fail + */ + private function validateValue(string $name, mixed $value): void + { + $type = $this->fields[$name]['type']; + $actualType = get_debug_type($value); + // Handle nullable types + if ($this->isNullable($type) && $value === null) { + return; + } + $baseType = $this->stripNullable($type); + // Handle nested structs + if (is_subclass_of($baseType, StructInterface::class)) { + if (!($value instanceof $baseType)) { + throw new InvalidArgumentException( + "Field '$name' expects type '$type', but got '$actualType'" + ); + } + return; + } + // Handle primitive types + if ($actualType !== $baseType && !is_subclass_of($value, $baseType)) { + throw new InvalidArgumentException( + "Field '$name' expects type '$type', but got '$actualType'" + ); + } + // Apply validation rules + foreach ($this->fields[$name]['rules'] as $rule) { + $rule->validate($value, $name); + } + } + + /** + * Check if a type is nullable + * + * @param string $type Type to check + * + * @return bool True if the type is nullable + */ + private function isNullable(string $type): bool + { + return str_starts_with($type, '?'); + } + + /** + * Strip nullable prefix from a type + * + * @param string $type Type to strip + * + * @return string Type without nullable prefix + */ + private function stripNullable(string $type): string + { + return ltrim($type, '?'); + } + + // Implement StructInterface methods + public function set(string $name, mixed $value): void + { + if ($this->frozen) { + throw new ImmutableException("Cannot modify a frozen struct"); + } + if (!isset($this->fields[$name])) { + throw new InvalidArgumentException("Field '$name' does not exist in the struct"); + } + $this->validateValue($name, $value); + $this->fields[$name]['value'] = $value; + } + + public function get(string $name): mixed + { + if (!isset($this->fields[$name])) { + throw new InvalidArgumentException("Field '$name' does not exist in the struct"); + } + return $this->fields[$name]['value']; + } + + public function getFields(): array + { + return $this->fields; + } +} diff --git a/src/Composite/Struct/Rules/CompositeRule.php b/src/Composite/Struct/Rules/CompositeRule.php new file mode 100644 index 0000000..1dbe35c --- /dev/null +++ b/src/Composite/Struct/Rules/CompositeRule.php @@ -0,0 +1,58 @@ +rules = $rules; + } + + public function validate(mixed $value, string $fieldName): bool + { + foreach ($this->rules as $rule) { + $rule->validate($value, $fieldName); + } + + return true; + } + + /** + * Create a new composite rule from an array of rules + * + * @param ValidationRule[] $rules + * + * @return self + */ + public static function fromArray(array $rules): self + { + return new self(...$rules); + } + + /** + * Add a rule to the composite + * + * @param ValidationRule $rule + * + * @return self A new composite rule with the added rule + */ + public function withRule(ValidationRule $rule): self + { + return new self(...array_merge($this->rules, [$rule])); + } +} diff --git a/src/Composite/Struct/Rules/CustomRule.php b/src/Composite/Struct/Rules/CustomRule.php new file mode 100644 index 0000000..22725b9 --- /dev/null +++ b/src/Composite/Struct/Rules/CustomRule.php @@ -0,0 +1,40 @@ +validator = $validator; + $this->errorMessage = $errorMessage; + } + + public function validate(mixed $value, string $fieldName): bool + { + $isValid = ($this->validator)($value); + + if (!$isValid) { + throw new ValidationException( + "Field '$fieldName': {$this->errorMessage}" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/EmailRule.php b/src/Composite/Struct/Rules/EmailRule.php new file mode 100644 index 0000000..64fb0e6 --- /dev/null +++ b/src/Composite/Struct/Rules/EmailRule.php @@ -0,0 +1,28 @@ +minLength = $minLength; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_string($value)) { + throw new ValidationException( + "Field '$fieldName' must be a string to validate length" + ); + } + + if (strlen($value) < $this->minLength) { + throw new ValidationException( + "Field '$fieldName' must be at least {$this->minLength} characters long" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/PasswordRule.php b/src/Composite/Struct/Rules/PasswordRule.php new file mode 100644 index 0000000..8fc38dc --- /dev/null +++ b/src/Composite/Struct/Rules/PasswordRule.php @@ -0,0 +1,82 @@ +minLength = $minLength; + $this->requireUppercase = $requireUppercase; + $this->requireLowercase = $requireLowercase; + $this->requireNumbers = $requireNumbers; + $this->requireSpecialChars = $requireSpecialChars; + $this->maxLength = $maxLength; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_string($value)) { + throw new ValidationException( + "Field '$fieldName' must be a string to validate password" + ); + } + + $length = strlen($value); + if ($length < $this->minLength) { + throw new ValidationException( + "Field '$fieldName' must be at least {$this->minLength} characters long" + ); + } + + if ($this->maxLength !== null && $length > $this->maxLength) { + throw new ValidationException( + "Field '$fieldName' must not exceed {$this->maxLength} characters" + ); + } + + if ($this->requireUppercase && !preg_match('/[A-Z]/', $value)) { + throw new ValidationException( + "Field '$fieldName' must contain at least one uppercase letter" + ); + } + + if ($this->requireLowercase && !preg_match('/[a-z]/', $value)) { + throw new ValidationException( + "Field '$fieldName' must contain at least one lowercase letter" + ); + } + + if ($this->requireNumbers && !preg_match('/[0-9]/', $value)) { + throw new ValidationException( + "Field '$fieldName' must contain at least one number" + ); + } + + if ($this->requireSpecialChars && !preg_match('/[^a-zA-Z0-9]/', $value)) { + throw new ValidationException( + "Field '$fieldName' must contain at least one special character" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/PatternRule.php b/src/Composite/Struct/Rules/PatternRule.php new file mode 100644 index 0000000..9d2484d --- /dev/null +++ b/src/Composite/Struct/Rules/PatternRule.php @@ -0,0 +1,35 @@ +pattern = $pattern; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_string($value)) { + throw new ValidationException( + "Field '$fieldName' must be a string to validate pattern" + ); + } + + if (!preg_match($this->pattern, $value)) { + throw new ValidationException( + "Field '$fieldName' does not match the required pattern" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/RangeRule.php b/src/Composite/Struct/Rules/RangeRule.php new file mode 100644 index 0000000..554c76c --- /dev/null +++ b/src/Composite/Struct/Rules/RangeRule.php @@ -0,0 +1,38 @@ +min = $min; + $this->max = $max; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_numeric($value)) { + throw new ValidationException( + "Field '$fieldName' must be numeric to validate range" + ); + } + + $numValue = (float)$value; + if ($numValue < $this->min || $numValue > $this->max) { + throw new ValidationException( + "Field '$fieldName' must be between {$this->min} and {$this->max}" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/SlugRule.php b/src/Composite/Struct/Rules/SlugRule.php new file mode 100644 index 0000000..798202d --- /dev/null +++ b/src/Composite/Struct/Rules/SlugRule.php @@ -0,0 +1,68 @@ +minLength = $minLength; + $this->maxLength = $maxLength; + $this->allowUnderscores = $allowUnderscores; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_string($value)) { + throw new ValidationException( + "Field '$fieldName' must be a string to validate slug" + ); + } + + $length = strlen($value); + if ($length < $this->minLength) { + throw new ValidationException( + "Field '$fieldName' must be at least {$this->minLength} characters long" + ); + } + + if ($length > $this->maxLength) { + throw new ValidationException( + "Field '$fieldName' must not exceed {$this->maxLength} characters" + ); + } + + // Basic slug pattern: lowercase letters, numbers, hyphens, and optionally underscores + $pattern = $this->allowUnderscores + ? '/^[a-z0-9][a-z0-9-_]*[a-z0-9]$/' + : '/^[a-z0-9][a-z0-9-]*[a-z0-9]$/'; + + if (!preg_match($pattern, $value)) { + $message = $this->allowUnderscores + ? "Field '$fieldName' must contain only lowercase letters, numbers, hyphens, and underscores" + : "Field '$fieldName' must contain only lowercase letters, numbers, and hyphens"; + throw new ValidationException($message); + } + + // Check for consecutive hyphens or underscores + if (str_contains($value, '--') || ($this->allowUnderscores && str_contains($value, '__'))) { + throw new ValidationException( + "Field '$fieldName' must not contain consecutive hyphens or underscores" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Rules/UrlRule.php b/src/Composite/Struct/Rules/UrlRule.php new file mode 100644 index 0000000..2e515c7 --- /dev/null +++ b/src/Composite/Struct/Rules/UrlRule.php @@ -0,0 +1,41 @@ +requireHttps = $requireHttps; + } + + public function validate(mixed $value, string $fieldName): bool + { + if (!is_string($value)) { + throw new ValidationException( + "Field '$fieldName' must be a string to validate URL" + ); + } + + if (!filter_var($value, FILTER_VALIDATE_URL)) { + throw new ValidationException( + "Field '$fieldName' must be a valid URL" + ); + } + + if ($this->requireHttps && !str_starts_with($value, 'https://')) { + throw new ValidationException( + "Field '$fieldName' must be a secure HTTPS URL" + ); + } + + return true; + } +} diff --git a/src/Composite/Struct/Struct.php b/src/Composite/Struct/Struct.php index bd68945..4cceb65 100644 --- a/src/Composite/Struct/Struct.php +++ b/src/Composite/Struct/Struct.php @@ -1,95 +1,187 @@ $fields Array of field names and their expected types. - */ - public function __construct(array $fields) + protected array $data = []; + protected array $schema = []; + + public function __construct(array $schema, array $values = []) { - foreach ($fields as $name => $type) { - $this->addField($name, $type); + // Backward compatibility: convert old format ['id' => 'int', ...] to new format + $first = reset($schema); + if (is_string($first)) { + $newSchema = []; + foreach ($schema as $field => $type) { + $newSchema[$field] = ['type' => $type, 'nullable' => true]; + } + $schema = $newSchema; + } + $this->schema = $schema; + foreach ($schema as $field => $def) { + $alias = $def['alias'] ?? $field; + $type = $def['type'] ?? 'mixed'; + $nullable = $def['nullable'] ?? false; + $default = $def['default'] ?? null; + $rules = $def['rules'] ?? []; + $value = $values[$field] ?? $values[$alias] ?? $default; + if ($value === null && !$nullable && $default === null && !array_key_exists($field, $values)) { + throw new InvalidArgumentException("Field '$field' is required and has no value"); + } + if ($value !== null) { + $this->validateField($field, $value, $type, $rules, $nullable); + } + $this->data[$field] = $value; } } - /** - * {@inheritDoc} - */ - public function set(string $name, mixed $value): void + protected function validateField(string $field, mixed $value, string $type, array $rules, bool $nullable): void { - if (!isset($this->fields[$name])) { - throw new InvalidArgumentException("Field '$name' does not exist in the struct."); + if ($value === null && $nullable) { + return; + } + // Type check + if ($type !== 'mixed' && !$this->isValidType($value, $type)) { + throw new InvalidArgumentException("Field '$field' must be of type $type"); } + // Rules + if (!array_all($rules, fn($rule) => !is_callable($rule) || $rule($value))) { + throw new ValidationException("Validation failed for field '$field'"); + } + } + + protected function isValidType(mixed $value, string $type): bool + { + if ($type === 'int' || $type === 'integer') return is_int($value); + if ($type === 'float' || $type === 'double') return is_float($value); + if ($type === 'string') return is_string($value); + if ($type === 'bool' || $type === 'boolean') return is_bool($value); + if ($type === 'array') return is_array($value); + if ($type === 'object') return is_object($value); + if (class_exists($type)) return $value instanceof $type; + return true; + } - $expectedType = $this->fields[$name]['type']; - $actualType = get_debug_type($value); + public function get(string $field): mixed + { + if (!array_key_exists($field, $this->schema)) { + throw new InvalidArgumentException("Field '$field' does not exist in the struct."); + } + return $this->data[$field] ?? null; + } - // Handle nullable types (e.g., "?string") - if ($this->isNullable($expectedType) && $value === null) { - $this->fields[$name]['value'] = $value; - return; + public function toArray(bool $useAliases = false): array + { + $result = []; + foreach ($this->schema as $field => $def) { + $alias = $def['alias'] ?? $field; + $value = $this->data[$field]; + if ($value instanceof self) { + $value = $value->toArray($useAliases); + } + $result[$useAliases ? $alias : $field] = $value; } + return $result; + } - $baseType = $this->stripNullable($expectedType); + public static function fromArray(array $schema, array $data): self + { + return new self($schema, $data); + } - if ($actualType !== $baseType && !is_subclass_of($value, $baseType)) { - throw new InvalidArgumentException("Field '$name' expects type '$expectedType', but got '$actualType'."); + public function toJson(bool $useAliases = false): string + { + return json_encode($this->toArray($useAliases)); + } + + public static function fromJson(array $schema, string $json): self + { + $data = json_decode($json, true); + return new self($schema, $data); + } + + public function toXml(bool $useAliases = false): string + { + $arr = $this->toArray($useAliases); + $xml = new \SimpleXMLElement(''); + foreach ($arr as $k => $v) { + $xml->addChild($k, htmlspecialchars((string)$v)); } + return $xml->asXML(); + } - $this->fields[$name]['value'] = $value; + public static function fromXml(array $schema, string $xml): self + { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false) { + foreach ($data as $k => $v) { + $type = $schema[$k]['type'] ?? 'mixed'; + $value = (string)$v; + // Cast to appropriate type + if ($type === 'int' || $type === 'integer') { + $value = (int)$value; + } elseif ($type === 'float' || $type === 'double') { + $value = (float)$value; + } elseif ($type === 'bool' || $type === 'boolean') { + $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); + } + $arr[$k] = $value; + } + } + return new self($schema, $arr); } - /** - * {@inheritDoc} - */ - public function get(string $name): mixed + public function set(string $field, mixed $value): void { - if (!isset($this->fields[$name])) { - throw new InvalidArgumentException("Field '$name' does not exist in the struct."); + if (!array_key_exists($field, $this->schema)) { + throw new InvalidArgumentException("Field '$field' does not exist in the struct."); + } + $def = $this->schema[$field]; + $type = $def['type'] ?? 'mixed'; + $nullable = $def['nullable'] ?? false; + $rules = $def['rules'] ?? []; + if ($value === null && !$nullable) { + throw new InvalidArgumentException("Field '$field' cannot be null"); } + $this->validateField($field, $value, $type, $rules, $nullable); + $this->data[$field] = $value; + } - return $this->fields[$name]['value']; + public function __set(string $field, mixed $value): void + { + $this->set($field, $value); } - /** - * {@inheritDoc} - */ - public function getFields(): array + public function __get(string $field): mixed { - return $this->fields; + return $this->get($field); } - /** - * Magic method for accessing fields like object properties. - * - * @param string $name The field name. - * @return mixed The field value. - * - * @throws InvalidArgumentException if the field doesn't exist. - */ - public function __get(string $name): mixed + public function getFields(): array { - return $this->get($name); + $fields = []; + foreach ($this->schema as $field => $def) { + $fields[$field] = [ + 'type' => $def['type'] ?? 'mixed', + 'value' => $this->data[$field] ?? null, + ]; + } + return $fields; } - /** - * Magic method for setting fields like object properties. - * - * @param string $name The field name. - * @param mixed $value The field value. - * @return void - * - * @throws InvalidArgumentException if the field doesn't exist or the value type doesn't match. - */ - public function __set(string $name, mixed $value): void + public function addField(string $field, string $type): void { - $this->set($name, $value); + if (array_key_exists($field, $this->schema)) { + throw new InvalidArgumentException("Field '$field' already exists in the struct."); + } + $this->schema[$field] = ['type' => $type, 'nullable' => true]; + $this->data[$field] = null; } } diff --git a/src/Composite/Struct/ValidationRule.php b/src/Composite/Struct/ValidationRule.php new file mode 100644 index 0000000..2e01c92 --- /dev/null +++ b/src/Composite/Struct/ValidationRule.php @@ -0,0 +1,23 @@ +value; } + /** + * Determine if the current value is of a specific type. + * + * @param string $type + * + * @return bool + */ + public function isType(string $type): bool + { + $actualType = gettype($this->value); + + // Map to shorthand if applicable + $shorthandType = array_search($actualType, self::$typeMap, true); + $actualType = $shorthandType ?: $actualType; + + return $actualType === $type || $this->value instanceof $type; + } + + /** + * Add a new type to the allowed types of the union. + * + * @param string $type + * + * @return void + */ + public function addAllowedType(string $type): void + { + if (!in_array($type, $this->allowedTypes, true)) { + $this->allowedTypes[] = $type; + } + } + /** * Validate the type of the value against allowed types. * * @param mixed $value + * * @return void * * @throws InvalidArgumentException @@ -98,34 +135,4 @@ private function validateType(mixed $value): void ); } } - - /** - * Determine if the current value is of a specific type. - * - * @param string $type - * @return bool - */ - public function isType(string $type): bool - { - $actualType = gettype($this->value); - - // Map to shorthand if applicable - $shorthandType = array_search($actualType, self::$typeMap, true); - $actualType = $shorthandType ?: $actualType; - - return $actualType === $type || $this->value instanceof $type; - } - - /** - * Add a new type to the allowed types of the union. - * - * @param string $type - * @return void - */ - public function addAllowedType(string $type): void - { - if (!in_array($type, $this->allowedTypes, true)) { - $this->allowedTypes[] = $type; - } - } } diff --git a/src/Composite/Union/UnionType.php b/src/Composite/Union/UnionType.php new file mode 100644 index 0000000..66f7ca2 --- /dev/null +++ b/src/Composite/Union/UnionType.php @@ -0,0 +1,452 @@ + The values for each type key + */ + private array $values = []; + + /** + * @var array The expected type for each key + */ + private array $typeMap = []; + + /** + * @var string|null The current active type key + */ + private ?string $activeType = null; + + /** + * A mapping of PHP shorthand types to their gettype() equivalents + */ + private static array $phpTypeMap = [ + 'int' => 'integer', + 'float' => 'double', + 'bool' => 'boolean', + ]; + + /** + * Create a new UnionType instance + * + * @param array $typeMap The expected type for each key (e.g. ['string' => 'string', 'int' => 'int']) + * @param array $initialValues Optional initial values for each key + * @throws InvalidArgumentException If no types are provided + */ + public function __construct(array $typeMap, array $initialValues = []) + { + if (empty($typeMap)) { + throw new InvalidArgumentException('Union type must have at least one possible type'); + } + $this->typeMap = $typeMap; + foreach ($typeMap as $key => $expectedType) { + $this->values[$key] = $initialValues[$key] ?? null; + } + } + + /** + * Get the currently active type + * + * @return string + * @throws InvalidArgumentException if no active type is set + */ + public function getActiveType(): string + { + if ($this->activeType === null) { + throw new InvalidArgumentException('No active type set'); + } + return $this->activeType; + } + + /** + * Check if a type key is active + * + * @param string $key + * @return bool + */ + public function isType(string $key): bool + { + return $this->activeType === $key; + } + + /** + * Get the value of the current active type + * + * @return mixed + * @throws TypeMismatchException + */ + public function getValue(): mixed + { + if ($this->activeType === null) { + throw new TypeMismatchException('No type is currently active'); + } + return $this->values[$this->activeType]; + } + + /** + * Set the value for a specific type key + * + * @param string $key + * @param mixed $value + * @throws InvalidArgumentException + */ + public function setValue(string $key, mixed $value): void + { + if (!isset($this->typeMap[$key])) { + throw new InvalidArgumentException("Type key '$key' is not valid in this union"); + } + $this->validateType($value, $this->typeMap[$key], $key); + $this->values[$key] = $value; + $this->activeType = $key; + } + + /** + * Get all possible type keys + * + * @return array + */ + public function getTypes(): array + { + return array_keys($this->typeMap); + } + + /** + * Add a new type to the union + * + * @param string $key + * @param string $expectedType + * @param mixed $initialValue + * @throws InvalidArgumentException + */ + public function addType(string $key, string $expectedType, mixed $initialValue = null): void + { + if (isset($this->typeMap[$key])) { + throw new InvalidArgumentException("Type key '$key' already exists in this union"); + } + $this->validateType($initialValue, $expectedType, $key); + $this->typeMap[$key] = $expectedType; + $this->values[$key] = $initialValue; + } + + /** + * Pattern match on the active type + * + * @param array $patterns + * @return mixed + * @throws TypeMismatchException + */ + public function match(array $patterns): mixed + { + if ($this->activeType === null) { + throw new TypeMismatchException('No type is currently active'); + } + if (!isset($patterns[$this->activeType])) { + throw new TypeMismatchException("No pattern defined for type '{$this->activeType}'"); + } + return $patterns[$this->activeType]($this->values[$this->activeType]); + } + + /** + * Pattern match with a default case + * + * @param array $patterns + * @param callable $default + * @return mixed + */ + public function matchWithDefault(array $patterns, callable $default): mixed + { + if ($this->activeType === null) { + return $default(); + } + if (!isset($patterns[$this->activeType])) { + return $default(); + } + return $patterns[$this->activeType]($this->values[$this->activeType]); + } + + /** + * String representation + * + * @return string + */ + public function __toString(): string + { + if ($this->activeType === null) { + return 'UnionType'; + } + return "UnionType<{$this->activeType}>"; + } + + /** + * Validate a value against an expected type + * + * @param mixed $value + * @param string $expectedType + * @param string $key + * @throws InvalidArgumentException + */ + private function validateType(mixed $value, string $expectedType, string $key): void + { + if ($value === null) { + return; + } + // Handle class instances + if (class_exists($expectedType) && $value instanceof $expectedType) { + return; + } + // Handle arrays + if ($expectedType === 'array' && is_array($value)) { + return; + } + // Handle objects + if ($expectedType === 'object' && is_object($value)) { + return; + } + // Handle primitive types + $actualType = $this->canonicalTypeName($value); + $expectedTypeName = $this->canonicalTypeName($expectedType); + if ($actualType !== $expectedTypeName) { + throw new InvalidArgumentException( + "Invalid type for key '$key': expected '$expectedTypeName', got '$actualType'" + ); + } + } + + /** + * Canonical PHP type name for error messages + * + * @param mixed|string $valueOrType + * @return string + */ + private function canonicalTypeName($valueOrType): string + { + if (is_object($valueOrType)) { + return get_class($valueOrType); + } + if (is_string($valueOrType) && class_exists($valueOrType)) { + return $valueOrType; + } + // If this is a type name, return the mapped type + if (is_string($valueOrType) && in_array($valueOrType, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'string', 'array', 'object', 'null'])) { + return self::$phpTypeMap[$valueOrType] ?? $valueOrType; + } + // Otherwise, return the type of the value + $type = gettype($valueOrType); + return self::$phpTypeMap[$type] ?? $type; + } + + /** + * Safely cast the current value to the specified type + * + * @param string $type + * @return mixed + * @throws TypeMismatchException + */ + public function castTo(string $type): mixed + { + if ($this->activeType === null) { + throw new TypeMismatchException('No type is currently active'); + } + if ($this->typeMap[$this->activeType] !== $type && $this->activeType !== $type) { + throw new TypeMismatchException("Cannot cast active type '{$this->activeType}' to '{$type}'"); + } + return $this->values[$this->activeType]; + } + + /** + * Check if this union equals another union + * + * @param UnionType $other + * @return bool + */ + public function equals(UnionType $other): bool + { + if ($this->activeType === null || $other->activeType === null) { + return false; + } + return $this->activeType === $other->activeType && $this->values[$this->activeType] === $other->values[$other->activeType]; + } + + /** + * Convert the union to a JSON string + * + * @return string + */ + public function toJson(): string + { + $data = [ + 'activeType' => $this->activeType, + 'value' => $this->activeType !== null ? $this->values[$this->activeType] : null + ]; + return json_encode($data); + } + + /** + * Create a UnionType instance from a JSON string + * + * @param string $json + * @return UnionType + * @throws InvalidArgumentException + */ + public static function fromJson(string $json): UnionType + { + $data = json_decode($json, true); + if (!is_array($data) || !isset($data['activeType']) || !isset($data['value'])) { + throw new InvalidArgumentException('Invalid JSON format for UnionType'); + } + $union = new UnionType([$data['activeType'] => $data['activeType']]); + if ($data['activeType'] !== null) { + $union->setValue($data['activeType'], $data['value']); + } + return $union; + } + + /** + * Convert the union to an XML string, with optional namespace support + * + * @param string|null $namespaceUri + * @param string|null $prefix + * @return string + */ + public function toXml(?string $namespaceUri = null, ?string $prefix = null): string + { + if ($namespaceUri && $prefix) { + $rootName = $prefix . ':union'; + $xml = new \SimpleXMLElement("<{$rootName} xmlns:{$prefix}='{$namespaceUri}'>"); + $xml->addAttribute('activeType', $this->activeType ?? ''); + if ($this->activeType !== null) { + $xml->addChild($prefix . ':value', (string)$this->values[$this->activeType], $namespaceUri); + } + } else if ($namespaceUri) { + $xml = new \SimpleXMLElement(""); + $xml->addAttribute('activeType', $this->activeType ?? ''); + if ($this->activeType !== null) { + $xml->addChild('value', (string)$this->values[$this->activeType], $namespaceUri); + } + } else { + $xml = new \SimpleXMLElement(''); + $xml->addAttribute('activeType', $this->activeType ?? ''); + if ($this->activeType !== null) { + $xml->addChild('value', (string)$this->values[$this->activeType]); + } + } + return $xml->asXML(); + } + + /** + * Create a UnionType instance from an XML string + * + * @param string $xml + * @return UnionType + * @throws InvalidArgumentException + */ + public static function fromXml(string $xml): UnionType + { + $data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING); + if ($data === false || !($data instanceof \SimpleXMLElement) || $data->getName() !== 'union' || !isset($data['activeType'])) { + throw new InvalidArgumentException('Invalid XML format for UnionType'); + } + $activeType = (string)$data['activeType']; + if ($activeType === '') { + $activeType = null; + } + $union = new UnionType([$activeType => $activeType]); + if ($activeType !== null) { + // Try to get the namespace URI from the root element + $namespaces = $data->getNamespaces(true); + $value = ''; + if (!empty($namespaces)) { + foreach ($namespaces as $prefix => $uri) { + $children = $data->children($uri); + if (isset($children->value)) { + $value = (string)$children->value; + break; + } + } + } + if ($value === '') { + // Fallback to non-namespaced value + $value = (string)($data->value ?? $data->children()->value ?? ''); + if ($value === '' && count($data->children()) > 0) { + foreach ($data->children() as $child) { + if ($child->getName() === 'value') { + $value = (string)$child; + break; + } + } + } + } + $union->setValue($activeType, $value); + } + return $union; + } + + /** + * Validate an XML string against an XSD schema + * + * @param string $xml + * @param string $xsd + * @return bool + * @throws InvalidArgumentException + */ + public static function validateXmlSchema(string $xml, string $xsd): bool + { + $dom = new \DOMDocument(); + libxml_use_internal_errors(true); + if (!$dom->loadXML($xml)) { + throw new InvalidArgumentException('Invalid XML provided for schema validation'); + } + $result = $dom->schemaValidateSource($xsd); + if (!$result) { + $errors = libxml_get_errors(); + libxml_clear_errors(); + $errorMsg = isset($errors[0]) ? $errors[0]->message : 'Unknown schema validation error'; + throw new InvalidArgumentException('XML does not validate against schema: ' . $errorMsg); + } + return true; + } + + /** + * Convert the union to a binary string using PHP's serialize + * + * @return string + */ + public function toBinary(): string + { + $data = [ + 'activeType' => $this->activeType, + 'value' => $this->activeType !== null ? $this->values[$this->activeType] : null + ]; + return serialize($data); + } + + /** + * Create a UnionType instance from a binary string + * + * @param string $binary + * @return UnionType + * @throws InvalidArgumentException + */ + public static function fromBinary(string $binary): UnionType + { + $data = @unserialize($binary); + if ($data === false || !is_array($data) || !isset($data['activeType']) || !isset($data['value'])) { + throw new InvalidArgumentException('Invalid binary format for UnionType'); + } + $union = new UnionType([$data['activeType'] => $data['activeType']]); + if ($data['activeType'] !== null) { + $union->setValue($data['activeType'], $data['value']); + } + return $union; + } +} \ No newline at end of file diff --git a/src/Composite/Vector/Vec2.php b/src/Composite/Vector/Vec2.php new file mode 100644 index 0000000..db61b60 --- /dev/null +++ b/src/Composite/Vector/Vec2.php @@ -0,0 +1,60 @@ +getComponent(0); + } + + public function getY(): float + { + return $this->getComponent(1); + } + + public function cross(Vec2 $other): float + { + return ($this->getX() * $other->getY()) - ($this->getY() * $other->getX()); + } + + public static function zero(): self + { + return new self([0.0, 0.0]); + } + + public static function unitX(): self + { + return new self([1.0, 0.0]); + } + + public static function unitY(): self + { + return new self([0.0, 1.0]); + } + + public function getValue(): array + { + return $this->components; + } + + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new InvalidArgumentException('Value must be an array of components.'); + } + $this->validateComponents($value); + $this->components = $value; + } + protected function validateComponents(array $components): void + { + $this->validateComponentCount($components, 2); + $this->validateNumericComponents($components); + } +} diff --git a/src/Composite/Vector/Vec3.php b/src/Composite/Vector/Vec3.php new file mode 100644 index 0000000..3276890 --- /dev/null +++ b/src/Composite/Vector/Vec3.php @@ -0,0 +1,74 @@ +getComponent(0); + } + + public function getY(): float + { + return $this->getComponent(1); + } + + public function getZ(): float + { + return $this->getComponent(2); + } + + public function cross(Vec3 $other): self + { + return new self([ + $this->getY() * $other->getZ() - $this->getZ() * $other->getY(), + $this->getZ() * $other->getX() - $this->getX() * $other->getZ(), + $this->getX() * $other->getY() - $this->getY() * $other->getX() + ]); + } + + public static function zero(): self + { + return new self([0.0, 0.0, 0.0]); + } + + public static function unitX(): self + { + return new self([1.0, 0.0, 0.0]); + } + + public static function unitY(): self + { + return new self([0.0, 1.0, 0.0]); + } + + public static function unitZ(): self + { + return new self([0.0, 0.0, 1.0]); + } + + public function getValue(): array + { + return $this->components; + } + + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new InvalidArgumentException('Value must be an array of components.'); + } + $this->validateComponents($value); + $this->components = $value; + } + protected function validateComponents(array $components): void + { + $this->validateComponentCount($components, 3); + $this->validateNumericComponents($components); + } +} diff --git a/src/Composite/Vector/Vec4.php b/src/Composite/Vector/Vec4.php new file mode 100644 index 0000000..596ddd8 --- /dev/null +++ b/src/Composite/Vector/Vec4.php @@ -0,0 +1,75 @@ +getComponent(0); + } + + public function getY(): float + { + return $this->getComponent(1); + } + + public function getZ(): float + { + return $this->getComponent(2); + } + + public function getW(): float + { + return $this->getComponent(3); + } + + public static function zero(): self + { + return new self([0.0, 0.0, 0.0, 0.0]); + } + + public static function unitX(): self + { + return new self([1.0, 0.0, 0.0, 0.0]); + } + + public static function unitY(): self + { + return new self([0.0, 1.0, 0.0, 0.0]); + } + + public static function unitZ(): self + { + return new self([0.0, 0.0, 1.0, 0.0]); + } + + public static function unitW(): self + { + return new self([0.0, 0.0, 0.0, 1.0]); + } + + public function getValue(): array + { + return $this->components; + } + + public function setValue(mixed $value): void + { + if (!is_array($value)) { + throw new InvalidArgumentException('Value must be an array of components.'); + } + $this->validateComponents($value); + $this->components = $value; + } + protected function validateComponents(array $components): void + { + $this->validateComponentCount($components, 4); + $this->validateNumericComponents($components); + } +} diff --git a/src/Encoding/Base64Encoding.php b/src/Encoding/Base64Encoding.php index 30e1c51..923435d 100644 --- a/src/Encoding/Base64Encoding.php +++ b/src/Encoding/Base64Encoding.php @@ -11,12 +11,13 @@ * Class Base64Encoding * Implements Base64 encoding. */ -class Base64Encoding implements EncoderInterface, DecoderInterface +final class Base64Encoding implements DecoderInterface, EncoderInterface { /** * Encodes the data using Base64. * * @param string $data + * * @return string */ public function encode(string $data): string @@ -28,6 +29,7 @@ public function encode(string $data): string * Decodes the data using Base64. * * @param string $data + * * @return string */ public function decode(string $data): string diff --git a/src/Encoding/GzipEncoding.php b/src/Encoding/GzipEncoding.php index b3ec8d1..ce7b8b7 100644 --- a/src/Encoding/GzipEncoding.php +++ b/src/Encoding/GzipEncoding.php @@ -11,12 +11,13 @@ * Class GzipEncoding * Implements Gzip compression. */ -class GzipEncoding implements EncoderInterface, DecoderInterface +final class GzipEncoding implements DecoderInterface, EncoderInterface { /** * Encodes the data using Gzip. * * @param string $data + * * @return string */ public function encode(string $data): string @@ -32,6 +33,7 @@ public function encode(string $data): string * Decodes the data using Gzip. * * @param string $data + * * @return string */ public function decode(string $data): string diff --git a/src/Encoding/HuffmanEncoding.php b/src/Encoding/HuffmanEncoding.php index 4fd8405..65c1dd6 100644 --- a/src/Encoding/HuffmanEncoding.php +++ b/src/Encoding/HuffmanEncoding.php @@ -2,23 +2,23 @@ declare(strict_types=1); - namespace Nejcc\PhpDatatypes\Encoding; +use InvalidArgumentException; use Nejcc\PhpDatatypes\Interfaces\DecoderInterface; use Nejcc\PhpDatatypes\Interfaces\EncoderInterface; -use InvalidArgumentException; /** * Class HuffmanEncoding * Implements Huffman compression and decompression. */ -class HuffmanEncoding implements EncoderInterface, DecoderInterface +final class HuffmanEncoding implements DecoderInterface, EncoderInterface { /** * Encodes the data using Huffman encoding. * * @param string $data + * * @return string The encoded data with serialized frequency table. */ public function encode(string $data): string @@ -64,6 +64,7 @@ public function encode(string $data): string * Decodes the data using Huffman decoding. * * @param string $data The encoded data with serialized frequency table. + * * @return string The original decoded data. */ public function decode(string $data): string @@ -118,6 +119,7 @@ public function decode(string $data): string * Builds a frequency table for the given data. * * @param string $data + * * @return array Associative array with characters as keys and frequencies as values. */ private function buildFrequencyTable(string $data): array @@ -138,6 +140,7 @@ private function buildFrequencyTable(string $data): array * Builds the Huffman tree from the frequency table. * * @param array $frequency + * * @return Node The root of the Huffman tree. */ private function buildHuffmanTree(array $frequency): Node @@ -176,6 +179,7 @@ private function buildHuffmanTree(array $frequency): Node * @param Node $node * @param string $prefix * @param array &$codes + * * @return void */ private function generateCodes(Node $node, string $prefix, array &$codes): void @@ -199,6 +203,7 @@ private function generateCodes(Node $node, string $prefix, array &$codes): void * Converts a bit string to a byte string. * * @param string $bits + * * @return string */ private function bitsToBytes(string $bits): string @@ -219,6 +224,7 @@ private function bitsToBytes(string $bits): string * Converts a byte string back to a bit string. * * @param string $bytes + * * @return string */ private function bytesToBits(string $bytes): string diff --git a/src/Encoding/Node.php b/src/Encoding/Node.php index 48a7cc4..67964a4 100644 --- a/src/Encoding/Node.php +++ b/src/Encoding/Node.php @@ -8,7 +8,7 @@ * Class Node * Represents a node in the Huffman tree. */ -class Node +final class Node { public string $character; public int $frequency; diff --git a/src/Enums/Http/HttpStatusCode.php b/src/Enums/Http/HttpStatusCode.php index e18dc1e..7db0e9a 100644 --- a/src/Enums/Http/HttpStatusCode.php +++ b/src/Enums/Http/HttpStatusCode.php @@ -171,7 +171,7 @@ public function buildResponse(array $data = [], array $headers = []): array */ public static function getSuccessCodes(): array { - return array_filter(self::cases(), fn($case) => $case->isSuccess()); + return array_filter(self::cases(), fn ($case) => $case->isSuccess()); } /** @@ -179,6 +179,6 @@ public static function getSuccessCodes(): array */ public static function getClientErrorCodes(): array { - return array_filter(self::cases(), fn($case) => $case->isClientError()); + return array_filter(self::cases(), fn ($case) => $case->isClientError()); } } diff --git a/src/Exceptions/ImmutableException.php b/src/Exceptions/ImmutableException.php new file mode 100644 index 0000000..5604d64 --- /dev/null +++ b/src/Exceptions/ImmutableException.php @@ -0,0 +1,9 @@ +invalidValue = $invalidValue; @@ -31,22 +33,22 @@ public function __construct($invalidValue, string $message = null, int $code = 0 } /** - * Get the invalid value that caused the exception. + * String representation of the exception, including the invalid value. * - * @return mixed The invalid value. + * @return string */ - public function getInvalidValue(): mixed + public function __toString(): string { - return $this->invalidValue; + return __CLASS__ . ": [{$this->code}]: {$this->message}. Invalid value: " . json_encode($this->invalidValue) . "\n"; } /** - * String representation of the exception, including the invalid value. + * Get the invalid value that caused the exception. * - * @return string + * @return mixed The invalid value. */ - public function __toString(): string + public function getInvalidValue(): mixed { - return __CLASS__ . ": [{$this->code}]: {$this->message}. Invalid value: " . json_encode($this->invalidValue) . "\n"; + return $this->invalidValue; } } diff --git a/src/Exceptions/TypeMismatchException.php b/src/Exceptions/TypeMismatchException.php new file mode 100644 index 0000000..69aeb44 --- /dev/null +++ b/src/Exceptions/TypeMismatchException.php @@ -0,0 +1,9 @@ +getValue(); + } + + return (int) $value; + } +} diff --git a/src/Laravel/Http/Controllers/PhpDatatypesController.php b/src/Laravel/Http/Controllers/PhpDatatypesController.php new file mode 100644 index 0000000..a7946bb --- /dev/null +++ b/src/Laravel/Http/Controllers/PhpDatatypesController.php @@ -0,0 +1,142 @@ +input('int8_value')); + $int32 = new Int32($request->input('int32_value')); + $uint8 = new UInt8($request->input('uint8_value')); + $float32 = new Float32($request->input('float32_value')); + + return response()->json([ + 'message' => 'Validation successful', + 'data' => [ + 'int8' => $int8->getValue(), + 'int32' => $int32->getValue(), + 'uint8' => $uint8->getValue(), + 'float32' => $float32->getValue(), + ] + ]); + } + + /** + * Example endpoint using manual validation + */ + public function validateManually(Request $request): JsonResponse + { + $request->validate([ + 'user_id' => ['required', 'uint8'], + 'age' => ['required', 'int8'], + 'balance' => ['required', 'float32'], + ]); + + $userId = new UInt8($request->input('user_id')); + $age = new Int8($request->input('age')); + $balance = new Float32($request->input('balance')); + + return response()->json([ + 'user_id' => $userId->getValue(), + 'age' => $age->getValue(), + 'balance' => $balance->getValue(), + ]); + } + + /** + * Example using Option type for nullable values + */ + public function handleOptionalData(Request $request): JsonResponse + { + $optionalValue = Option::fromNullable($request->input('optional_field')); + + $result = $optionalValue + ->map(fn($value) => strtoupper($value)) + ->unwrapOr('DEFAULT_VALUE'); + + return response()->json([ + 'processed_value' => $result, + 'was_present' => $optionalValue->isSome(), + ]); + } + + /** + * Example using Result type for error handling + */ + public function safeOperation(Request $request): JsonResponse + { + $result = Result::try(function () use ($request) { + $value = $request->input('value'); + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Value must be numeric'); + } + return new Int32((int) $value); + }); + + if ($result->isOk()) { + $int32 = $result->unwrap(); + return response()->json([ + 'success' => true, + 'value' => $int32->getValue(), + ]); + } + + return response()->json([ + 'success' => false, + 'error' => $result->unwrapErr()->getMessage(), + ], 400); + } + + /** + * Example using arithmetic operations + */ + public function performCalculations(Request $request): JsonResponse + { + $request->validate([ + 'a' => ['required', 'int8'], + 'b' => ['required', 'int8'], + ]); + + $a = new Int8($request->input('a')); + $b = new Int8($request->input('b')); + + try { + $sum = $a->add($b); + $difference = $a->subtract($b); + $product = $a->multiply($b); + + return response()->json([ + 'a' => $a->getValue(), + 'b' => $b->getValue(), + 'sum' => $sum->getValue(), + 'difference' => $difference->getValue(), + 'product' => $product->getValue(), + ]); + } catch (\OverflowException | \UnderflowException $e) { + return response()->json([ + 'error' => 'Arithmetic operation resulted in overflow or underflow', + 'message' => $e->getMessage(), + ], 400); + } + } +} diff --git a/src/Laravel/Http/Requests/PhpDatatypesFormRequest.php b/src/Laravel/Http/Requests/PhpDatatypesFormRequest.php new file mode 100644 index 0000000..c4bf921 --- /dev/null +++ b/src/Laravel/Http/Requests/PhpDatatypesFormRequest.php @@ -0,0 +1,66 @@ +|string> + */ + public function rules(): array + { + return [ + // Integer validation rules + 'int8_value' => ['required', 'int8'], + 'int16_value' => ['required', 'int16'], + 'int32_value' => ['required', 'int32'], + 'int64_value' => ['required', 'int64'], + 'uint8_value' => ['required', 'uint8'], + 'uint16_value' => ['required', 'uint16'], + 'uint32_value' => ['required', 'uint32'], + 'uint64_value' => ['required', 'uint64'], + + // Float validation rules + 'float32_value' => ['required', 'float32'], + 'float64_value' => ['required', 'float64'], + ]; + } + + /** + * Get custom error messages for validation rules. + * + * @return array + */ + public function messages(): array + { + return [ + 'int8_value.int8' => 'The int8_value must be a valid 8-bit signed integer (-128 to 127).', + 'int16_value.int16' => 'The int16_value must be a valid 16-bit signed integer (-32,768 to 32,767).', + 'int32_value.int32' => 'The int32_value must be a valid 32-bit signed integer.', + 'int64_value.int64' => 'The int64_value must be a valid 64-bit signed integer.', + 'uint8_value.uint8' => 'The uint8_value must be a valid 8-bit unsigned integer (0 to 255).', + 'uint16_value.uint16' => 'The uint16_value must be a valid 16-bit unsigned integer (0 to 65,535).', + 'uint32_value.uint32' => 'The uint32_value must be a valid 32-bit unsigned integer.', + 'uint64_value.uint64' => 'The uint64_value must be a valid 64-bit unsigned integer.', + 'float32_value.float32' => 'The float32_value must be a valid 32-bit floating point number.', + 'float64_value.float64' => 'The float64_value must be a valid 64-bit floating point number.', + ]; + } +} diff --git a/src/Laravel/Models/ExampleModel.php b/src/Laravel/Models/ExampleModel.php new file mode 100644 index 0000000..5fad09d --- /dev/null +++ b/src/Laravel/Models/ExampleModel.php @@ -0,0 +1,141 @@ + + */ + protected $fillable = [ + 'name', + 'age', + 'user_id', + 'balance', + 'score', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'age' => Int8Cast::class, + 'user_id' => 'integer', // Will be cast to UInt8 in accessor + 'balance' => 'decimal:2', // Will be cast to Float32 in accessor + 'score' => 'integer', // Will be cast to Int8 in accessor + ]; + + /** + * Get the age as Int8 + */ + public function getAgeAttribute($value): Int8 + { + return new Int8($value); + } + + /** + * Set the age from Int8 + */ + public function setAgeAttribute($value): void + { + if ($value instanceof Int8) { + $this->attributes['age'] = $value->getValue(); + } else { + $this->attributes['age'] = $value; + } + } + + /** + * Get the user_id as UInt8 + */ + public function getUserIdAttribute($value): UInt8 + { + return new UInt8($value); + } + + /** + * Set the user_id from UInt8 + */ + public function setUserIdAttribute($value): void + { + if ($value instanceof UInt8) { + $this->attributes['user_id'] = $value->getValue(); + } else { + $this->attributes['user_id'] = $value; + } + } + + /** + * Get the balance as Float32 + */ + public function getBalanceAttribute($value): Float32 + { + return new Float32((float) $value); + } + + /** + * Set the balance from Float32 + */ + public function setBalanceAttribute($value): void + { + if ($value instanceof Float32) { + $this->attributes['balance'] = $value->getValue(); + } else { + $this->attributes['balance'] = $value; + } + } + + /** + * Get the score as Int8 + */ + public function getScoreAttribute($value): Int8 + { + return new Int8($value); + } + + /** + * Set the score from Int8 + */ + public function setScoreAttribute($value): void + { + if ($value instanceof Int8) { + $this->attributes['score'] = $value->getValue(); + } else { + $this->attributes['score'] = $value; + } + } + + /** + * Example method using arithmetic operations + */ + public function addToScore(Int8 $points): Int8 + { + $currentScore = $this->getScoreAttribute($this->attributes['score']); + return $currentScore->add($points); + } + + /** + * Example method using comparison + */ + public function isHighScore(): bool + { + $score = $this->getScoreAttribute($this->attributes['score']); + $threshold = new Int8(100); + return $score->greaterThan($threshold); + } +} diff --git a/src/Laravel/PhpDatatypesServiceProvider.php b/src/Laravel/PhpDatatypesServiceProvider.php new file mode 100644 index 0000000..29ebed6 --- /dev/null +++ b/src/Laravel/PhpDatatypesServiceProvider.php @@ -0,0 +1,88 @@ +registerValidationRules(); + } + + /** + * Register custom validation rules + */ + private function registerValidationRules(): void + { + // Integer validation rules + Validator::extend('int8', Int8Rule::class); + Validator::extend('int16', Int16Rule::class); + Validator::extend('int32', Int32Rule::class); + Validator::extend('int64', Int64Rule::class); + Validator::extend('uint8', UInt8Rule::class); + Validator::extend('uint16', UInt16Rule::class); + Validator::extend('uint32', UInt32Rule::class); + Validator::extend('uint64', UInt64Rule::class); + + // Float validation rules + Validator::extend('float32', Float32Rule::class); + Validator::extend('float64', Float64Rule::class); + + // Add custom error messages + $this->addValidationMessages(); + } + + /** + * Add custom validation error messages + */ + private function addValidationMessages(): void + { + $messages = [ + 'int8' => 'The :attribute must be a valid 8-bit signed integer (-128 to 127).', + 'int16' => 'The :attribute must be a valid 16-bit signed integer (-32,768 to 32,767).', + 'int32' => 'The :attribute must be a valid 32-bit signed integer.', + 'int64' => 'The :attribute must be a valid 64-bit signed integer.', + 'uint8' => 'The :attribute must be a valid 8-bit unsigned integer (0 to 255).', + 'uint16' => 'The :attribute must be a valid 16-bit unsigned integer (0 to 65,535).', + 'uint32' => 'The :attribute must be a valid 32-bit unsigned integer.', + 'uint64' => 'The :attribute must be a valid 64-bit unsigned integer.', + 'float32' => 'The :attribute must be a valid 32-bit floating point number.', + 'float64' => 'The :attribute must be a valid 64-bit floating point number.', + ]; + + foreach ($messages as $rule => $message) { + Validator::replacer($rule, function ($message, $attribute, $rule, $parameters) { + return str_replace(':attribute', $attribute, $message); + }); + } + } +} diff --git a/src/Laravel/README.md b/src/Laravel/README.md new file mode 100644 index 0000000..337d946 --- /dev/null +++ b/src/Laravel/README.md @@ -0,0 +1,294 @@ +# PHP Datatypes Laravel Integration + +This package provides seamless integration between PHP Datatypes and Laravel, including validation rules, Eloquent casts, and form requests. + +## Installation + +1. Install the package via Composer: +```bash +composer require nejcc/php-datatypes +``` + +2. Register the service provider in your `config/app.php`: +```php +'providers' => [ + // ... + Nejcc\PhpDatatypes\Laravel\PhpDatatypesServiceProvider::class, +], +``` + +3. Publish the configuration file (optional): +```bash +php artisan vendor:publish --provider="Nejcc\PhpDatatypes\Laravel\PhpDatatypesServiceProvider" +``` + +## Validation Rules + +The package automatically registers validation rules for all PHP Datatypes: + +### Integer Types +- `int8` - 8-bit signed integer (-128 to 127) +- `int16` - 16-bit signed integer (-32,768 to 32,767) +- `int32` - 32-bit signed integer +- `int64` - 64-bit signed integer +- `uint8` - 8-bit unsigned integer (0 to 255) +- `uint16` - 16-bit unsigned integer (0 to 65,535) +- `uint32` - 32-bit unsigned integer +- `uint64` - 64-bit unsigned integer + +### Floating Point Types +- `float32` - 32-bit floating point number +- `float64` - 64-bit floating point number + +### Usage in Form Requests + +```php + ['required', 'int8'], + 'user_id' => ['required', 'uint8'], + 'balance' => ['required', 'float32'], + ]; + } +} +``` + +### Usage in Controllers + +```php +validate([ + 'age' => ['required', 'int8'], + 'user_id' => ['required', 'uint8'], + ]); + + $age = new Int8($request->input('age')); + $userId = new UInt8($request->input('user_id')); + + // Use the type-safe values... + } +} +``` + +## Eloquent Casts + +You can use PHP Datatypes as Eloquent casts: + +```php + Int8Cast::class, + 'user_id' => 'uint8', // Custom cast + 'balance' => 'float32', // Custom cast + ]; +} +``` + +### Custom Casts + +You can create custom casts for your models: + +```php +attributes['age'] = $value->getValue(); + } else { + $this->attributes['age'] = $value; + } + } +} +``` + +## Form Requests + +The package includes example form requests that demonstrate proper usage: + +```php + ['required', 'int8'], + 'user_id' => ['required', 'uint8'], + 'balance' => ['required', 'float32'], + ]; + } +} +``` + +## Controllers + +Example controller showing various usage patterns: + +```php +input('optional_field')); + + $result = $optionalValue + ->map(fn($value) => strtoupper($value)) + ->unwrapOr('DEFAULT_VALUE'); + + return response()->json([ + 'processed_value' => $result, + 'was_present' => $optionalValue->isSome(), + ]); + } + + public function safeOperation(Request $request) + { + $result = Result::try(function () use ($request) { + $value = $request->input('value'); + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Value must be numeric'); + } + return new Int8((int) $value); + }); + + if ($result->isOk()) { + $int8 = $result->unwrap(); + return response()->json([ + 'success' => true, + 'value' => $int8->getValue(), + ]); + } + + return response()->json([ + 'success' => false, + 'error' => $result->unwrapErr()->getMessage(), + ], 400); + } +} +``` + +## Configuration + +You can customize the behavior by publishing and modifying the configuration file: + +```php +// config/php-datatypes.php + +return [ + 'auto_register_validation_rules' => true, + 'auto_register_casts' => true, + 'validation_messages' => [ + 'int8' => 'Custom message for int8 validation', + // ... + ], +]; +``` + +## Error Messages + +Customize validation error messages in your language files: + +```php +// resources/lang/en/validation.php + +return [ + 'int8' => 'The :attribute must be a valid 8-bit signed integer.', + 'uint8' => 'The :attribute must be a valid 8-bit unsigned integer.', + // ... +]; +``` + +## Examples + +See the example files in the `src/Laravel/` directory for complete working examples: + +- `Http/Controllers/PhpDatatypesController.php` - Controller examples +- `Http/Requests/PhpDatatypesFormRequest.php` - Form request examples +- `Models/ExampleModel.php` - Model examples +- `Casts/Int8Cast.php` - Custom cast examples + +## Best Practices + +1. **Use Form Requests**: Always use form requests for validation to keep your controllers clean. + +2. **Type Safety**: Leverage the type safety provided by PHP Datatypes to prevent runtime errors. + +3. **Error Handling**: Use the `Result` type for operations that might fail. + +4. **Nullable Values**: Use the `Option` type for handling nullable values safely. + +5. **Arithmetic Operations**: Use the built-in arithmetic methods to prevent overflow/underflow. + +## Troubleshooting + +### Common Issues + +1. **Validation Rules Not Found**: Make sure the service provider is registered. + +2. **Cast Errors**: Ensure your database columns can store the expected values. + +3. **Overflow/Underflow**: Use the appropriate integer type for your data range. + +### Debug Mode + +Enable debug mode in your configuration to see detailed error messages: + +```php +// config/php-datatypes.php +'debug' => true, +``` diff --git a/src/Laravel/Validation/Rules/Float32Rule.php b/src/Laravel/Validation/Rules/Float32Rule.php new file mode 100644 index 0000000..f290038 --- /dev/null +++ b/src/Laravel/Validation/Rules/Float32Rule.php @@ -0,0 +1,45 @@ + true, + + /* + |-------------------------------------------------------------------------- + | Default Error Messages + |-------------------------------------------------------------------------- + | + | Customize the default error messages for validation rules. + | You can override these in your language files. + | + */ + 'validation_messages' => [ + 'int8' => 'The :attribute must be a valid 8-bit signed integer (-128 to 127).', + 'int16' => 'The :attribute must be a valid 16-bit signed integer (-32,768 to 32,767).', + 'int32' => 'The :attribute must be a valid 32-bit signed integer.', + 'int64' => 'The :attribute must be a valid 64-bit signed integer.', + 'uint8' => 'The :attribute must be a valid 8-bit unsigned integer (0 to 255).', + 'uint16' => 'The :attribute must be a valid 16-bit unsigned integer (0 to 65,535).', + 'uint32' => 'The :attribute must be a valid 32-bit unsigned integer.', + 'uint64' => 'The :attribute must be a valid 64-bit unsigned integer.', + 'float32' => 'The :attribute must be a valid 32-bit floating point number.', + 'float64' => 'The :attribute must be a valid 64-bit floating point number.', + ], + + /* + |-------------------------------------------------------------------------- + | Eloquent Casts + |-------------------------------------------------------------------------- + | + | Enable automatic registration of Eloquent casts. + | When enabled, casts like 'int8', 'uint8', 'float32', etc. will be + | automatically available in your models. + | + */ + 'auto_register_casts' => true, + + /* + |-------------------------------------------------------------------------- + | Performance Settings + |-------------------------------------------------------------------------- + | + | Configure performance-related settings for the library. + | + */ + 'performance' => [ + /* + |-------------------------------------------------------------------------- + | Enable Caching + |-------------------------------------------------------------------------- + | + | Enable caching of validation rules and casts for better performance. + | + */ + 'enable_caching' => true, + + /* + |-------------------------------------------------------------------------- + | Cache TTL + |-------------------------------------------------------------------------- + | + | Time to live for cached validation rules and casts in seconds. + | + */ + 'cache_ttl' => 3600, + ], +]; diff --git a/src/Scalar/Boolean.php b/src/Scalar/Boolean.php new file mode 100644 index 0000000..eb86c3f --- /dev/null +++ b/src/Scalar/Boolean.php @@ -0,0 +1,24 @@ +and(new Boolean(false)); // Returns false + * $string = (string) $bool; // Returns "true" + * ``` + */ +final class Boolean extends BooleanAbstraction +{ +} diff --git a/src/Scalar/Byte.php b/src/Scalar/Byte.php index 880eae1..3db7734 100644 --- a/src/Scalar/Byte.php +++ b/src/Scalar/Byte.php @@ -1,226 +1,32 @@ setValue($value); - } - - /** - * Set the byte value ensuring it is between 0 and 255. - * - * @param int $value - * @return void - * - * @throws \InvalidArgumentException - */ - private function setValue(int $value): void - { - if ($value < 0 || $value > 255) { - throw new \InvalidArgumentException('Byte value must be between 0 and 255.'); - } - - $this->value = $value; - } - - /** - * Get the byte value. - * - * @return int - */ - public function getValue(): int - { - return $this->value; - } - - /** - * Perform a bitwise AND operation on this byte and another. - * - * @param Byte $byte - * @return Byte - */ - public function and(Byte $byte): Byte - { - return new self($this->value & $byte->getValue()); - } - - /** - * Perform a bitwise OR operation on this byte and another. - * - * @param Byte $byte - * @return Byte - */ - public function or(Byte $byte): Byte - { - return new self($this->value | $byte->getValue()); - } - - /** - * Perform a bitwise XOR operation on this byte and another. - * - * @param Byte $byte - * @return Byte - */ - public function xor(Byte $byte): Byte - { - return new self($this->value ^ $byte->getValue()); - } - - /** - * Perform a bitwise NOT operation on this byte. - * - * @return Byte - */ - public function not(): Byte - { - return new self(~$this->value & 0xFF); // Ensures the result stays within 8 bits - } - - /** - * Shift the bits of this byte to the left. - * - * @param int $positions - * @return Byte - */ - public function shiftLeft(int $positions): Byte - { - return new self(($this->value << $positions) & 0xFF); // Prevent overflow - } - - /** - * Shift the bits of this byte to the right. - * - * @param int $positions - * @return Byte - */ - public function shiftRight(int $positions): Byte - { - return new self($this->value >> $positions); - } - - /** - * Determine if this byte is equal to another byte. - * - * @param Byte $byte - * @return bool - */ - public function equals(Byte $byte): bool - { - return $this->value === $byte->getValue(); - } - - /** - * Determine if this byte is greater than another byte. - * - * @param Byte $byte - * @return bool - */ - public function isGreaterThan(Byte $byte): bool - { - return $this->value > $byte->getValue(); - } - - /** - * Determine if this byte is less than another byte. - * - * @param Byte $byte - * @return bool - */ - public function isLessThan(Byte $byte): bool - { - return $this->value < $byte->getValue(); - } - - /** - * Get the binary string representation of the byte. - * - * @return string - */ - public function toBinary(): string - { - return sprintf('%08b', $this->value); - } - - /** - * Get the hexadecimal string representation of the byte. - * - * @return string - */ - public function toHex(): string - { - return sprintf('%02X', $this->value); - } - - /** - * Convert the byte value to a string. - * - * @return string - */ - public function __toString(): string - { - return (string) $this->value; - } - - /** - * Create a byte instance from a binary string. - * - * @param string $binary - * @return Byte - */ - public static function fromBinary(string $binary): Byte - { - return new self(bindec($binary)); - } - - /** - * Create a byte instance from a hexadecimal string. - * - * @param string $hex - * @return Byte - */ - public static function fromHex(string $hex): Byte - { - return new self(hexdec($hex)); - } +use Nejcc\PhpDatatypes\Abstract\ByteAbstraction; +/** + * Concrete Byte type (8-bit unsigned integer, 0-255). + * Inherits all logic from ByteAbstraction. + */ +final class Byte extends ByteAbstraction +{ /** - * Add an integer to the byte value, wrapping around at 255. + * Flyweight cache of all 256 valid Byte values. Lazily populated. * - * @param int $number - * @return Byte + * @var array */ - public function add(int $number): Byte - { - return new self(($this->value + $number) & 0xFF); // Wrap around at 255 - } + private static array $cache = []; /** - * Subtract an integer from the byte value, wrapping around at 0. + * Return a cached Byte instance for the given value. * - * @param int $number - * @return Byte + * The Byte domain is exactly 256 values (0-255) and instances are + * immutable, so sharing is safe. */ - public function subtract(int $number): Byte + public static function of(int $value): self { - return new self(($this->value - $number) & 0xFF); // Wrap around at 0 + return self::$cache[$value] ??= new self($value); } } diff --git a/src/Scalar/Char.php b/src/Scalar/Char.php index a54cdd9..b42f69e 100644 --- a/src/Scalar/Char.php +++ b/src/Scalar/Char.php @@ -1,147 +1,15 @@ value = $value; - } - - /** - * Get the character value. - * - * @return string - */ - public function getValue(): string - { - return $this->value; - } - - /** - * Convert the character to its uppercase representation. - * - * @return Char - */ - public function toUpperCase(): Char - { - return new self(strtoupper($this->value)); - } - - /** - * Convert the character to its lowercase representation. - * - * @return Char - */ - public function toLowerCase(): Char - { - return new self(strtolower($this->value)); - } - - /** - * Determine if the character is a letter. - * - * @return bool - */ - public function isLetter(): bool - { - return ctype_alpha($this->value); - } +use Nejcc\PhpDatatypes\Abstract\AbstractChar; - /** - * Determine if the character is a digit. - * - * @return bool - */ - public function isDigit(): bool - { - return ctype_digit($this->value); - } - - /** - * Determine if the character is an uppercase letter. - * - * @return bool - */ - public function isUpperCase(): bool - { - return ctype_upper($this->value); - } - - /** - * Determine if the character is a lowercase letter. - * - * @return bool - */ - public function isLowerCase(): bool - { - return ctype_lower($this->value); - } - - /** - * Compare the current character with another Char instance. - * - * @param Char $char - * @return bool - */ - public function equals(Char $char): bool - { - return $this->value === $char->getValue(); - } - - /** - * Convert the character to its ASCII code. - * - * @return int - */ - public function toAscii(): int - { - return ord($this->value); - } - - /** - * Convert the ASCII code to a Char. - * - * @param int $ascii - * @return Char - */ - public static function fromAscii(int $ascii): Char - { - if ($ascii < 0 || $ascii > 255) { - throw new \InvalidArgumentException('ASCII value must be between 0 and 255.'); - } - - return new self(chr($ascii)); - } - - /** - * Convert the character to a string. - * - * @return string - */ - public function __toString(): string - { - return $this->value; - } +/** + * Concrete Char type (single character). + * Inherits all logic from AbstractChar. + */ +final class Char extends AbstractChar +{ } diff --git a/src/Scalar/FloatingPoints/Float32.php b/src/Scalar/FloatingPoints/Float32.php index 51a6383..189ee19 100644 --- a/src/Scalar/FloatingPoints/Float32.php +++ b/src/Scalar/FloatingPoints/Float32.php @@ -7,23 +7,76 @@ use Nejcc\PhpDatatypes\Abstract\AbstractFloat; /** - * Represents a 32-bit float. + * Represents a 32-bit floating-point number (single precision). * - * @package Nejcc\PhpDatatypes\Floats + * This class provides a type-safe way to work with 32-bit floating-point numbers, + * ensuring values stay within the valid range and maintaining proper precision. + * It includes arithmetic operations, comparisons, and mathematical functions. + * + * @package Nejcc\PhpDatatypes\Scalar\FloatingPoints + * + * @example + * ```php + * // Create a new Float32 instance + * $number = new Float32(3.14159); + * + * // Perform arithmetic operations + * $sum = $number->add(new Float32(2.0)); // Returns new Float32(5.14159) + * $product = $number->multiply(new Float32(2.0)); // Returns new Float32(6.28318) + * + * // Use mathematical functions + * $sine = $number->sin(); // Returns sine of the number + * $sqrt = $number->sqrt(); // Returns square root of the number + * + * // Rounding operations + * $rounded = $number->round(2); // Returns new Float32(3.14) + * ``` + * + * @property-read float $value The underlying float value + * + * @method Float32 add(Float32 $other) Adds two Float32 numbers + * @method Float32 subtract(Float32 $other) Subtracts two Float32 numbers + * @method Float32 multiply(Float32 $other) Multiplies two Float32 numbers + * @method Float32 divide(Float32 $other) Divides two Float32 numbers + * @method Float32 round(int $precision = 0) Rounds the number to specified precision + * @method Float32 ceil() Rounds the number up to the nearest integer + * @method Float32 floor() Rounds the number down to the nearest integer + * @method Float32 sin() Returns the sine of the number + * @method Float32 cos() Returns the cosine of the number + * @method Float32 tan() Returns the tangent of the number + * @method Float32 sqrt() Returns the square root of the number + * + * @throws \OutOfRangeException When the value is outside the valid range + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \InvalidArgumentException When invalid arguments are provided to methods */ final class Float32 extends AbstractFloat { /** - * The minimum allowable value for Float32. + * The minimum allowable value for Float32 (-3.4028235E+38). * * @var float */ - public const MIN_VALUE = -3.4028235e38; + public const MIN_VALUE = -3.4028235E+38; /** - * The maximum allowable value for Float32. + * The maximum allowable value for Float32 (3.4028235E+38). * * @var float */ - public const MAX_VALUE = 3.4028235e38; + public const MAX_VALUE = 3.4028235E+38; + + /** + * The smallest positive value for Float32 (1.17549435E-38). + * + * @var float + */ + public const MIN_POSITIVE_VALUE = 1.17549435E-38; + + /** + * The precision of Float32 (approximately 7 decimal digits). + * + * @var int + */ + public const PRECISION = 7; } diff --git a/src/Scalar/Integers/Signed/Int128.php b/src/Scalar/Integers/Signed/Int128.php index dcde4a1..6487a9c 100644 --- a/src/Scalar/Integers/Signed/Int128.php +++ b/src/Scalar/Integers/Signed/Int128.php @@ -9,23 +9,49 @@ /** * Represents a 128-bit signed integer. * + * This class provides a type-safe way to work with 128-bit signed integers, + * ensuring values stay within the range of -2^127 to 2^127-1. It includes arithmetic + * operations, comparisons, and range validation. + * * @package Nejcc\PhpDatatypes\Integers\Signed + * + * @example + * ```php + * // Create a new Int128 instance + * $number = new Int128('170141183460469231731687303715884105727'); + * + * // Perform arithmetic operations + * $sum = $number->add(new Int128('1')); // Returns new Int128('170141183460469231731687303715884105728') + * $diff = $number->subtract(new Int128('100')); // Returns new Int128('170141183460469231731687303715884105627') + * + * // Compare values + * $isGreater = $number->greaterThan(new Int128('170141183460469231731687303715884105626')); // Returns true + * + * // Get the underlying value + * $value = $number->getValue(); // Returns '170141183460469231731687303715884105727' + * ``` + * + * @throws \OutOfRangeException When the value is outside the valid range + * @throws \OverflowException When an arithmetic operation results in a value greater than MAX_VALUE + * @throws \UnderflowException When an arithmetic operation results in a value less than MIN_VALUE + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \UnexpectedValueException When division results in a non-integer value */ final class Int128 extends AbstractBigInteger { /** - * The minimum allowable value for Int128. + * The minimum allowable value for Int128 (-2^127). * * @var string */ public const MIN_VALUE = '-170141183460469231731687303715884105728'; /** - * The maximum allowable value for Int128. + * The maximum allowable value for Int128 (2^127 - 1). * * @var string */ public const MAX_VALUE = '170141183460469231731687303715884105727'; - // Inherit methods from AbstractInteger. + // Inherit methods from AbstractBigInteger. } diff --git a/src/Scalar/Integers/Signed/Int32.php b/src/Scalar/Integers/Signed/Int32.php index 200f1a3..92ed9a2 100644 --- a/src/Scalar/Integers/Signed/Int32.php +++ b/src/Scalar/Integers/Signed/Int32.php @@ -26,4 +26,6 @@ final class Int32 extends AbstractNativeInteger * @var int */ public const MAX_VALUE = 2147483647; + + } diff --git a/src/Scalar/Integers/Signed/Int64.php b/src/Scalar/Integers/Signed/Int64.php index 285c9ba..9980139 100644 --- a/src/Scalar/Integers/Signed/Int64.php +++ b/src/Scalar/Integers/Signed/Int64.php @@ -9,23 +9,49 @@ /** * Represents a 64-bit signed integer. * + * This class provides a type-safe way to work with 64-bit signed integers, + * ensuring values stay within the range of -9223372036854775808 to 9223372036854775807. + * It includes arithmetic operations, comparisons, and range validation. + * * @package Nejcc\PhpDatatypes\Integers\Signed + * + * @example + * ```php + * // Create a new Int64 instance + * $number = new Int64('9223372036854775800'); + * + * // Perform arithmetic operations + * $sum = $number->add(new Int64('7')); // Returns new Int64('9223372036854775807') + * $diff = $number->subtract(new Int64('100')); // Returns new Int64('9223372036854775700') + * + * // Compare values + * $isGreater = $number->greaterThan(new Int64('9223372036854775700')); // Returns true + * + * // Get the underlying value + * $value = $number->getValue(); // Returns '9223372036854775800' + * ``` + * + * @throws \OutOfRangeException When the value is outside the valid range + * @throws \OverflowException When an arithmetic operation results in a value greater than MAX_VALUE + * @throws \UnderflowException When an arithmetic operation results in a value less than MIN_VALUE + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \UnexpectedValueException When division results in a non-integer value */ final class Int64 extends AbstractBigInteger { /** - * The minimum allowable value for Int64. + * The minimum allowable value for Int64 (-2^63). * * @var string */ public const MIN_VALUE = '-9223372036854775808'; /** - * The maximum allowable value for Int64. + * The maximum allowable value for Int64 (2^63 - 1). * * @var string */ public const MAX_VALUE = '9223372036854775807'; - // Inherit methods from AbstractInteger. + // Inherit methods from AbstractBigInteger. } diff --git a/src/Scalar/Integers/Signed/Int8.php b/src/Scalar/Integers/Signed/Int8.php index 6a7c961..718a7aa 100644 --- a/src/Scalar/Integers/Signed/Int8.php +++ b/src/Scalar/Integers/Signed/Int8.php @@ -9,21 +9,66 @@ /** * Represents an 8-bit signed integer. * + * This class provides a type-safe way to work with 8-bit signed integers, + * ensuring values stay within the range of -128 to 127. It includes arithmetic + * operations, comparisons, and range validation. + * * @package Nejcc\PhpDatatypes\Integers\Signed + * + * @example + * ```php + * // Create a new Int8 instance + * $number = new Int8(42); + * + * // Perform arithmetic operations + * $sum = $number->add(new Int8(10)); // Returns new Int8(52) + * $diff = $number->subtract(new Int8(5)); // Returns new Int8(37) + * + * // Compare values + * $isGreater = $number->greaterThan(new Int8(40)); // Returns true + * + * // Get the underlying value + * $value = $number->getValue(); // Returns 42 + * ``` + * + * @throws \OutOfRangeException When the value is outside the valid range (-128 to 127) + * @throws \OverflowException When an arithmetic operation results in a value greater than 127 + * @throws \UnderflowException When an arithmetic operation results in a value less than -128 + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \UnexpectedValueException When division results in a non-integer value */ final class Int8 extends AbstractNativeInteger { /** - * The minimum allowable value for Int8. + * The minimum allowable value for Int8 (-128). * * @var int */ public const MIN_VALUE = -128; /** - * The maximum allowable value for Int8. + * The maximum allowable value for Int8 (127). * * @var int */ public const MAX_VALUE = 127; + + /** + * Flyweight cache of all 256 valid Int8 values. Lazily populated. + * + * @var array + */ + private static array $cache = []; + + /** + * Return a cached Int8 instance for the given value. + * + * Since the Int8 domain is exactly 256 values and instances are immutable, + * this is safe and ~5–7× faster than `new Int8($v)` for repeated values + * in hot paths. + */ + public static function of(int $value): self + { + return self::$cache[$value] ??= new self($value); + } } diff --git a/src/Scalar/Integers/Unsigned/UInt128.php b/src/Scalar/Integers/Unsigned/UInt128.php index e69de29..2b718d5 100644 --- a/src/Scalar/Integers/Unsigned/UInt128.php +++ b/src/Scalar/Integers/Unsigned/UInt128.php @@ -0,0 +1,57 @@ +add(new UInt128('1')); // Returns new UInt128('340282366920938463463374607431768211456') + * $diff = $number->subtract(new UInt128('100')); // Returns new UInt128('340282366920938463463374607431768211355') + * + * // Compare values + * $isGreater = $number->greaterThan(new UInt128('340282366920938463463374607431768211354')); // Returns true + * + * // Get the underlying value + * $value = $number->getValue(); // Returns '340282366920938463463374607431768211455' + * ``` + * + * @throws \OutOfRangeException When the value is outside the valid range (0 to 2^128-1) + * @throws \OverflowException When an arithmetic operation results in a value greater than MAX_VALUE + * @throws \UnderflowException When an arithmetic operation results in a value less than 0 + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \UnexpectedValueException When division results in a non-integer value + */ +final class UInt128 extends AbstractBigInteger +{ + /** + * The minimum allowable value for UInt128 (0). + * + * @var string + */ + public const MIN_VALUE = '0'; + + /** + * The maximum allowable value for UInt128 (2^128 - 1). + * + * @var string + */ + public const MAX_VALUE = '340282366920938463463374607431768211455'; + + // Inherit methods from AbstractBigInteger. +} diff --git a/src/Scalar/Integers/Unsigned/UInt64.php b/src/Scalar/Integers/Unsigned/UInt64.php index e69de29..10d9e87 100644 --- a/src/Scalar/Integers/Unsigned/UInt64.php +++ b/src/Scalar/Integers/Unsigned/UInt64.php @@ -0,0 +1,59 @@ +add(new UInt64('1')); // Returns new UInt64('18446744073709551616') + * $diff = $number->subtract(new UInt64('100')); // Returns new UInt64('18446744073709551515') + * + * // Compare values + * $isGreater = $number->greaterThan(new UInt64('18446744073709551515')); // Returns true + * + * // Get the underlying value + * $value = $number->getValue(); // Returns '18446744073709551615' + * ``` + * + * @throws \OutOfRangeException When the value is outside the valid range (0 to 2^64-1) + * @throws \OverflowException When an arithmetic operation results in a value greater than MAX_VALUE + * @throws \UnderflowException When an arithmetic operation results in a value less than 0 + * @throws \DivisionByZeroError When attempting to divide by zero + * @throws \UnexpectedValueException When division results in a non-integer value + */ +final class UInt64 extends AbstractBigInteger +{ + /** + * The minimum allowable value for UInt64 (0). + * + * @var string + */ + public const MIN_VALUE = '0'; + + /** + * The maximum allowable value for UInt64 (2^64 - 1). + * + * @var string + */ + public const MAX_VALUE = '18446744073709551615'; + + + + // Inherit methods from AbstractBigInteger. +} diff --git a/src/Scalar/Integers/Unsigned/UInt8.php b/src/Scalar/Integers/Unsigned/UInt8.php index 8525836..0bc0850 100644 --- a/src/Scalar/Integers/Unsigned/UInt8.php +++ b/src/Scalar/Integers/Unsigned/UInt8.php @@ -6,7 +6,6 @@ use Nejcc\PhpDatatypes\Abstract\AbstractNativeInteger; - /** * Represents an 8-bit unsigned integer. * @@ -16,4 +15,23 @@ final class UInt8 extends AbstractNativeInteger { public const MIN_VALUE = 0; public const MAX_VALUE = 255; + + /** + * Flyweight cache of all 256 valid UInt8 values. Lazily populated. + * + * @var array + */ + private static array $cache = []; + + /** + * Return a cached UInt8 instance for the given value. + * + * Since the UInt8 domain is exactly 256 values and instances are immutable, + * this is safe and ~2-3x faster than `new UInt8($v)` for repeated values + * in hot paths. + */ + public static function of(int $value): self + { + return self::$cache[$value] ??= new self($value); + } } diff --git a/src/Traits/ArithmeticOperationsTrait.php b/src/Traits/ArithmeticOperationsTrait.php deleted file mode 100644 index b48c611..0000000 --- a/src/Traits/ArithmeticOperationsTrait.php +++ /dev/null @@ -1,67 +0,0 @@ -performOperation($other, [$this, 'addValues'], 'add'); - } - - /** - * @param NativeIntegerInterface $other - * @return $this - */ - public function subtract(NativeIntegerInterface $other): static - { - return $this->performOperation($other, [$this, 'subtractValues'], 'subtract'); - } - - /** - * @param NativeIntegerInterface $other - * @return $this - */ - public function multiply(NativeIntegerInterface $other): static - { - return $this->performOperation($other, [$this, 'multiplyValues'], 'multiply'); - } - - /** - * @param NativeIntegerInterface $other - * @return $this - */ - public function divide(NativeIntegerInterface $other): static - { - return $this->performOperation($other, [$this, 'divideValues'], 'divide'); - } - - /** - * @param NativeIntegerInterface $other - * @return $this - */ - public function mod(NativeIntegerInterface $other): static - { - return $this->performOperation($other, [$this, 'modValues'], 'mod'); - } -} diff --git a/src/Traits/BigArithmeticOperationsTrait.php b/src/Traits/BigArithmeticOperationsTrait.php new file mode 100644 index 0000000..68899df --- /dev/null +++ b/src/Traits/BigArithmeticOperationsTrait.php @@ -0,0 +1,87 @@ +value, (string)$other->getValue(), 0); + if (bccomp($result, (string)static::MAX_VALUE) > 0) { + throw new \OverflowException('Result is out of bounds.'); + } + if (bccomp($result, (string)static::MIN_VALUE) < 0) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('subtract() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function subtract(BigIntegerInterface $other): static + { + $result = bcsub($this->value, (string)$other->getValue(), 0); + if (bccomp($result, (string)static::MAX_VALUE) > 0) { + throw new \OverflowException('Result is out of bounds.'); + } + if (bccomp($result, (string)static::MIN_VALUE) < 0) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('multiply() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function multiply(BigIntegerInterface $other): static + { + $result = bcmul($this->value, (string)$other->getValue(), 0); + if (bccomp($result, (string)static::MAX_VALUE) > 0) { + throw new \OverflowException('Result is out of bounds.'); + } + if (bccomp($result, (string)static::MIN_VALUE) < 0) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('divide() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function divide(BigIntegerInterface $other): static + { + $b = (string)$other->getValue(); + if ($b === '0') { + throw new \DivisionByZeroError('Division by zero.'); + } + if (bcmod($this->value, $b) !== '0') { + throw new \UnexpectedValueException('Division result is not an integer.'); + } + $result = bcdiv($this->value, $b, 0); + if (bccomp($result, (string)static::MAX_VALUE) > 0) { + throw new \OverflowException('Result is out of bounds.'); + } + if (bccomp($result, (string)static::MIN_VALUE) < 0) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('mod() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function mod(BigIntegerInterface $other): static + { + $b = (string)$other->getValue(); + if ($b === '0') { + throw new \DivisionByZeroError('Division by zero.'); + } + $result = bcmod($this->value, $b); + if (bccomp($result, (string)static::MAX_VALUE) > 0) { + throw new \OverflowException('Result is out of bounds.'); + } + if (bccomp($result, (string)static::MIN_VALUE) < 0) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + +} diff --git a/src/Traits/BigIntegerComparisonTrait.php b/src/Traits/BigIntegerComparisonTrait.php new file mode 100644 index 0000000..116daba --- /dev/null +++ b/src/Traits/BigIntegerComparisonTrait.php @@ -0,0 +1,40 @@ +getValue() === $other->getValue(); + } + + /** + * @param BigIntegerInterface $other + * + * @return bool + */ + public function isGreaterThan(BigIntegerInterface $other): bool + { + return $this->getValue() > $other->getValue(); + } + + /** + * @param BigIntegerInterface $other + * + * @return bool + */ + public function isLessThan(BigIntegerInterface $other): bool + { + return $this->getValue() < $other->getValue(); + } +} diff --git a/src/Traits/IntegerComparisonTrait.php b/src/Traits/IntegerComparisonTrait.php deleted file mode 100644 index dadb491..0000000 --- a/src/Traits/IntegerComparisonTrait.php +++ /dev/null @@ -1,21 +0,0 @@ -compare($other) === 0; - } -} diff --git a/src/Traits/NativeArithmeticOperationsTrait.php b/src/Traits/NativeArithmeticOperationsTrait.php new file mode 100644 index 0000000..9ea0105 --- /dev/null +++ b/src/Traits/NativeArithmeticOperationsTrait.php @@ -0,0 +1,88 @@ +value + $other->getValue(); + if ($result > static::MAX_VALUE) { + throw new \OverflowException('Result is out of bounds.'); + } + if ($result < static::MIN_VALUE) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('subtract() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function subtract(NativeIntegerInterface $other): static + { + $result = $this->value - $other->getValue(); + if ($result > static::MAX_VALUE) { + throw new \OverflowException('Result is out of bounds.'); + } + if ($result < static::MIN_VALUE) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('multiply() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function multiply(NativeIntegerInterface $other): static + { + $result = $this->value * $other->getValue(); + if ($result > static::MAX_VALUE) { + throw new \OverflowException('Result is out of bounds.'); + } + if ($result < static::MIN_VALUE) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('divide() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function divide(NativeIntegerInterface $other): static + { + $b = $other->getValue(); + if ($b === 0) { + throw new \DivisionByZeroError('Division by zero.'); + } + $a = $this->value; + if ($a % $b !== 0) { + throw new \UnexpectedValueException('Division result is not an integer.'); + } + $result = intdiv($a, $b); + if ($result > static::MAX_VALUE) { + throw new \OverflowException('Result is out of bounds.'); + } + if ($result < static::MIN_VALUE) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + + #[\NoDiscard('mod() returns a new immutable instance; the original is unchanged so discarding the result is always a bug')] + public function mod(NativeIntegerInterface $other): static + { + $b = $other->getValue(); + if ($b === 0) { + throw new \DivisionByZeroError('Division by zero.'); + } + $result = $this->value % $b; + if ($result > static::MAX_VALUE) { + throw new \OverflowException('Result is out of bounds.'); + } + if ($result < static::MIN_VALUE) { + throw new \UnderflowException('Result is out of bounds.'); + } + return new static($result, true); + } + +} diff --git a/src/Traits/NativeIntegerComparisonTrait.php b/src/Traits/NativeIntegerComparisonTrait.php new file mode 100644 index 0000000..c939a04 --- /dev/null +++ b/src/Traits/NativeIntegerComparisonTrait.php @@ -0,0 +1,40 @@ +getValue() === $other->getValue(); + } + + /** + * @param NativeIntegerInterface $other + * + * @return bool + */ + public function isGreaterThan(NativeIntegerInterface $other): bool + { + return $this->getValue() > $other->getValue(); + } + + /** + * @param NativeIntegerInterface $other + * + * @return bool + */ + public function isLessThan(NativeIntegerInterface $other): bool + { + return $this->getValue() < $other->getValue(); + } +} diff --git a/src/helpers.php b/src/helpers.php index 95f7da0..d097618 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,4 +1,5 @@ $union->getActiveType(), + 'value' => $union->getValue() + ]; + } +} + +if (!function_exists('fromUnion')) { + function fromUnion(array $data): UnionType + { + if (!isset($data['activeType']) || !isset($data['value'])) { + throw new InvalidArgumentException('Invalid union data format'); + } + $union = new UnionType([$data['activeType'] => $data['activeType']]); + if ($data['activeType'] !== null) { + $union->setValue($data['activeType'], $data['value']); + } + return $union; + } +} + +// --- Serialization/Deserialization Helpers --- + +// StringArray +if (!function_exists('toJsonStringArray')) { + function toJsonStringArray(StringArray $arr): string { return json_encode($arr->toArray()); } +} +if (!function_exists('fromJsonStringArray')) { + function fromJsonStringArray(string $json): StringArray { return new StringArray(json_decode($json, true)); } +} + +// IntArray +if (!function_exists('toJsonIntArray')) { + function toJsonIntArray(IntArray $arr): string { return json_encode($arr->toArray()); } +} +if (!function_exists('fromJsonIntArray')) { + function fromJsonIntArray(string $json): IntArray { return new IntArray(json_decode($json, true)); } +} + +// FloatArray +if (!function_exists('toJsonFloatArray')) { + function toJsonFloatArray(FloatArray $arr): string { return json_encode($arr->toArray()); } +} +if (!function_exists('fromJsonFloatArray')) { + function fromJsonFloatArray(string $json): FloatArray { return new FloatArray(json_decode($json, true)); } +} + +// ByteSlice +if (!function_exists('toJsonByteSlice')) { + function toJsonByteSlice(ByteSlice $arr): string { return json_encode($arr->toArray()); } +} +if (!function_exists('fromJsonByteSlice')) { + function fromJsonByteSlice(string $json): ByteSlice { return new ByteSlice(json_decode($json, true)); } +} + +// Struct +if (!function_exists('toJsonStruct')) { + function toJsonStruct(Struct $struct): string { return json_encode($struct->toArray()); } +} +if (!function_exists('fromJsonStruct')) { + function fromJsonStruct(string $json): Struct { return new Struct(json_decode($json, true)); } +} + +// Dictionary +if (!function_exists('toJsonDictionary')) { + function toJsonDictionary(Dictionary $dict): string { return json_encode($dict->toArray()); } +} +if (!function_exists('fromJsonDictionary')) { + function fromJsonDictionary(string $json): Dictionary { return new Dictionary(json_decode($json, true)); } +} + +// ListData +if (!function_exists('toJsonListData')) { + function toJsonListData(ListData $list): string { return json_encode($list->toArray()); } +} +if (!function_exists('fromJsonListData')) { + function fromJsonListData(string $json): ListData { return new ListData(json_decode($json, true)); } +} + +// UnionType (already present for JSON, XML, Binary) +if (!function_exists('unionToJson')) { + function unionToJson(UnionType $union): string { return $union->toJson(); } +} +if (!function_exists('unionFromJson')) { + function unionFromJson(string $json): UnionType { return UnionType::fromJson($json); } +} +if (!function_exists('unionToXml')) { + function unionToXml(UnionType $union, ?string $namespaceUri = null, ?string $prefix = null): string { return $union->toXml($namespaceUri, $prefix); } +} +if (!function_exists('unionFromXml')) { + function unionFromXml(string $xml): UnionType { return UnionType::fromXml($xml); } +} +if (!function_exists('unionToBinary')) { + function unionToBinary(UnionType $union): string { return $union->toBinary(); } +} +if (!function_exists('unionFromBinary')) { + function unionFromBinary(string $binary): UnionType { return UnionType::fromBinary($binary); } +} + +// --- XML and Binary Serialization/Deserialization Helpers --- + +// StringArray +if (!function_exists('toXmlStringArray')) { + function toXmlStringArray(StringArray $arr): string { + $xml = new SimpleXMLElement(''); + foreach ($arr->toArray() as $item) { + $xml->addChild('item', htmlspecialchars((string)$item)); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlStringArray')) { + function fromXmlStringArray(string $xml): StringArray { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $arr[] = (string)$item; + } + } + return new StringArray($arr); + } +} +if (!function_exists('toBinaryStringArray')) { + function toBinaryStringArray(StringArray $arr): string { return serialize($arr->toArray()); } +} +if (!function_exists('fromBinaryStringArray')) { + function fromBinaryStringArray(string $bin): StringArray { return new StringArray(unserialize($bin)); } +} + +// IntArray +if (!function_exists('toXmlIntArray')) { + function toXmlIntArray(IntArray $arr): string { + $xml = new SimpleXMLElement(''); + foreach ($arr->toArray() as $item) { + $xml->addChild('item', (string)$item); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlIntArray')) { + function fromXmlIntArray(string $xml): IntArray { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $arr[] = (int)$item; + } + } + return new IntArray($arr); + } +} +if (!function_exists('toBinaryIntArray')) { + function toBinaryIntArray(IntArray $arr): string { return serialize($arr->toArray()); } +} +if (!function_exists('fromBinaryIntArray')) { + function fromBinaryIntArray(string $bin): IntArray { return new IntArray(unserialize($bin)); } +} + +// FloatArray +if (!function_exists('toXmlFloatArray')) { + function toXmlFloatArray(FloatArray $arr): string { + $xml = new SimpleXMLElement(''); + foreach ($arr->toArray() as $item) { + $xml->addChild('item', (string)$item); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlFloatArray')) { + function fromXmlFloatArray(string $xml): FloatArray { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $arr[] = (float)$item; + } + } + return new FloatArray($arr); + } +} +if (!function_exists('toBinaryFloatArray')) { + function toBinaryFloatArray(FloatArray $arr): string { return serialize($arr->toArray()); } +} +if (!function_exists('fromBinaryFloatArray')) { + function fromBinaryFloatArray(string $bin): FloatArray { return new FloatArray(unserialize($bin)); } +} + +// ByteSlice +if (!function_exists('toXmlByteSlice')) { + function toXmlByteSlice(ByteSlice $arr): string { + $xml = new SimpleXMLElement(''); + foreach ($arr->toArray() as $item) { + $xml->addChild('item', (string)$item); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlByteSlice')) { + function fromXmlByteSlice(string $xml): ByteSlice { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $arr[] = (int)$item; + } + } + return new ByteSlice($arr); + } +} +if (!function_exists('toBinaryByteSlice')) { + function toBinaryByteSlice(ByteSlice $arr): string { return serialize($arr->toArray()); } +} +if (!function_exists('fromBinaryByteSlice')) { + function fromBinaryByteSlice(string $bin): ByteSlice { return new ByteSlice(unserialize($bin)); } +} + +// Struct +if (!function_exists('toXmlStruct')) { + function toXmlStruct(Struct $struct): string { + $xml = new SimpleXMLElement(''); + foreach ($struct->toArray() as $key => $value) { + $xml->addChild($key, htmlspecialchars((string)$value)); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlStruct')) { + function fromXmlStruct(string $xml): Struct { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false) { + foreach ($data as $key => $value) { + $arr[$key] = (string)$value; + } + } + return new Struct($arr); + } +} +if (!function_exists('toBinaryStruct')) { + function toBinaryStruct(Struct $struct): string { return serialize($struct->toArray()); } +} +if (!function_exists('fromBinaryStruct')) { + function fromBinaryStruct(string $bin): Struct { return new Struct(unserialize($bin)); } +} + +// Dictionary +if (!function_exists('toXmlDictionary')) { + function toXmlDictionary(Dictionary $dict): string { + $xml = new SimpleXMLElement(''); + foreach ($dict->toArray() as $key => $value) { + $item = $xml->addChild('item'); + $item->addChild('key', htmlspecialchars((string)$key)); + $item->addChild('value', htmlspecialchars((string)$value)); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlDictionary')) { + function fromXmlDictionary(string $xml): Dictionary { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $k = isset($item->key) ? (string)$item->key : null; + $v = isset($item->value) ? (string)$item->value : null; + if ($k !== null) $arr[$k] = $v; + } + } + return new Dictionary($arr); + } +} +if (!function_exists('toBinaryDictionary')) { + function toBinaryDictionary(Dictionary $dict): string { return serialize($dict->toArray()); } +} +if (!function_exists('fromBinaryDictionary')) { + function fromBinaryDictionary(string $bin): Dictionary { return new Dictionary(unserialize($bin)); } +} + +// ListData +if (!function_exists('toXmlListData')) { + function toXmlListData(ListData $list): string { + $xml = new SimpleXMLElement(''); + foreach ($list->toArray() as $item) { + $xml->addChild('item', htmlspecialchars((string)$item)); + } + return $xml->asXML(); + } +} +if (!function_exists('fromXmlListData')) { + function fromXmlListData(string $xml): ListData { + $data = @simplexml_load_string($xml); + $arr = []; + if ($data !== false && isset($data->item)) { + foreach ($data->item as $item) { + $arr[] = (string)$item; + } + } + return new ListData($arr); + } +} +if (!function_exists('toBinaryListData')) { + function toBinaryListData(ListData $list): string { return serialize($list->toArray()); } +} +if (!function_exists('fromBinaryListData')) { + function fromBinaryListData(string $bin): ListData { return new ListData(unserialize($bin)); } +} + +// --- Option Type Helpers --- + +if (!function_exists('some')) { + /** + * Create a Some Option with a value + * + * @param mixed $value + * @return Option + */ + function some(mixed $value): Option + { + return Option::some($value); + } +} + +if (!function_exists('none')) { + /** + * Create a None Option + * + * @return Option + */ + function none(): Option + { + return Option::none(); + } +} + +if (!function_exists('option')) { + /** + * Create an Option from a nullable value + * + * @param mixed|null $value + * @return Option + */ + function option(mixed $value = null): Option + { + return Option::fromNullable($value); + } +} + +if (!function_exists('toJsonOption')) { + /** + * Convert Option to JSON string + * + * @param Option $option + * @return string + */ + function toJsonOption(Option $option): string + { + return $option->toJson(); + } +} + +if (!function_exists('fromJsonOption')) { + /** + * Create Option from JSON string + * + * @param string $json + * @return Option + */ + function fromJsonOption(string $json): Option + { + return Option::fromJson($json); + } +} + +// --- Result Type Helpers --- + +if (!function_exists('ok')) { + /** + * Create an Ok Result with a value + * + * @param mixed $value + * @return Result + */ + function ok(mixed $value): Result + { + return Result::ok($value); + } +} + +if (!function_exists('err')) { + /** + * Create an Err Result with an error + * + * @param mixed $error + * @return Result + */ + function err(mixed $error): Result + { + return Result::err($error); + } +} + +if (!function_exists('result')) { + /** + * Create a Result from a callable that might throw + * + * @param callable $callable + * @return Result + */ + function result(callable $callable): Result + { + return Result::try($callable); + } +} + +if (!function_exists('toJsonResult')) { + /** + * Convert Result to JSON string + * + * @param Result $result + * @return string + */ + function toJsonResult(Result $result): string + { + return $result->toJson(); + } +} + +if (!function_exists('fromJsonResult')) { + /** + * Create Result from JSON string + * + * @param string $json + * @return Result + */ + function fromJsonResult(string $json): Result + { + return Result::fromJson($json); + } +}