pandu / exact
A lightweight type-safe data modeling library for PHP
Requires
- php: ^8.2
Requires (Dev)
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Exact is a Scala-inspired domain modeling library for PHP. It provides explicit data types, immutable-style collections, value abstractions, and composable functional utilities.
Quick Introduction
Exact is designed for PHP code that benefits from making domain states explicit instead of representing them only with nullable values, exceptions, or loosely structured arrays. Its data types model alternatives such as an optional value, a success or error, and a left or right branch.
The library focuses on type-oriented programming, immutability in its data and collection transformations, and small operations that can be combined through mapping, folding, pattern matching, and function composition.
Core Features
- Data Types —
Option,Result,Either,Validated, tuples, and pairs for explicit domain values and outcomes. - Algebraic Data Types — named
Variantvalues withPatternMatchandMatcherfor case-based handling. - Value Modeling —
Newtypefor immutable wrapped values andValueObjectas a base abstraction for equality-based domain values. - Collections —
Seq,NonEmptySeq,Map,Set, and lazyLazySeqwith transformation and conversion operations. - Function Composition — right-to-left composition, left-to-right piping, currying, partial application, and immutable value pipelines.
Example
<?php use Exact\Data\Option\Option; use Exact\Data\Result\Result; $email = Option::from('USER@example.com') ->map(static fn (string $value): string => strtolower($value)) ->getOrElse('noreply@example.com'); $total = Result::ok(42) ->map(static fn (int $value): int => $value + 8) ->getOrElse(0);
Option::from(null) creates None; a non-null value creates Some. Result::map() transforms an Ok value while preserving an Err branch.
Installation
Exact requires PHP 8.2 or later and has no runtime dependencies.
composer require pandu/exact
Documentation
The documentation directories are present in the repository; detailed documentation files are not currently included.
Project Status
The repository includes PHPUnit tests and is still evolving. The public API may change as the library develops, and no separate backward-compatibility or production-readiness guarantee is declared.
License
MIT, as declared in composer.json.