Download the PHP package majesko/fp-kit without Composer
On this page you can find all versions of the php package majesko/fp-kit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download majesko/fp-kit
More information about majesko/fp-kit
Files in majesko/fp-kit
Package fp-kit
Short Description Small functional programming utilities for PHP (pipe/compose, Result, Option, Validation).
License MIT
Informations about the package fp-kit
majesko/fp-kit
A lightweight functional programming toolkit for PHP 8.2+. This library provides immutable data types, function composition utilities, and array helpers that enable a clean functional programming style in PHP.
Features
- Monadic Types:
Result,Option, andValidationfor safe, composable error handling - Function Composition:
pipe,compose,tap, andpartialfor building complex operations - Array Utilities: Functional helpers like
map,filter,reduce,groupBy, andindexBy - Zero Dependencies: Pure PHP with no external dependencies
- Fully Typed: Strict types throughout with PHPStan level 7 compliance
- Comprehensive Tests: 79 tests with extensive edge case coverage
Table of Contents
- Installation
- Quick Start
- Function Composition
- pipe
- compose
- tap
- partial
- Array Functions
- Result Type
- Option Type
- Validation Type
- API Reference
- Testing
- Contributing
- License
Installation
Requires PHP 8.2 or higher.
Quick Start
Function Composition
pipe
Chains functions left-to-right, passing the result of each function to the next.
compose
Chains functions right-to-left (mathematical composition).
tap
Executes a side effect without changing the value (useful for debugging).
partial
Creates a new function with some arguments pre-filled.
Array Functions
Functional array manipulation utilities.
Result Type
Result represents an operation that can succeed (ok) or fail (err). It's perfect for error handling without exceptions.
Creating Results
Checking Results
Transforming Results
Pattern Matching
Real-World Example
Option Type
Option represents a value that may or may not exist, similar to null but composable.
Creating Options
Transforming Options
Pattern Matching
Interop with Result
Validation Type
Validation is similar to Result but accumulates multiple errors instead of short-circuiting on the first error. Perfect for form validation.
Creating Validations
Combining Validations
Lifting Functions
Pattern Matching
API Reference
Function Composition
| Function | Signature | Description |
|---|---|---|
pipe |
(callable ...$fns): callable |
Left-to-right function composition |
compose |
(callable ...$fns): callable |
Right-to-left function composition |
tap |
(callable $fn): callable |
Execute side effect without changing value |
partial |
(callable $fn, mixed ...$args): callable |
Partial application |
Array Functions
| Function | Signature | Description |
|---|---|---|
map |
(array $xs, callable $fn): array |
Transform each element |
filter |
(array $xs, callable $fn): array |
Keep elements matching predicate |
reduce |
(array $xs, callable $fn, mixed $init): mixed |
Reduce to single value |
groupBy |
(array $xs, callable $fn): array |
Group by key function |
indexBy |
(array $xs, callable $fn): array |
Create associative array by key |
Result Type
| Function | Signature | Description |
|---|---|---|
ok |
(mixed $value): array |
Create success Result |
err |
(mixed $error): array |
Create error Result |
isOk |
(array $r): bool |
Check if Result is success |
isErr |
(array $r): bool |
Check if Result is error |
map |
(array $r, callable $fn): array |
Transform success value |
bind |
(array $r, callable $fn): array |
Chain Result-returning operations |
mapError |
(array $r, callable $fn): array |
Transform error value |
unwrapOr |
(array $r, mixed $default): mixed |
Get value or default |
fold |
(array $r, callable $ok, callable $err): mixed |
Extract value with handlers |
matchResult |
(array $r, callable $ok, callable $err): mixed |
Pattern match |
Option Type
| Function | Signature | Description |
|---|---|---|
some |
(mixed $value): array |
Create present Option |
none |
(): array |
Create absent Option |
isSome |
(array $o): bool |
Check if Option has value |
isNone |
(array $o): bool |
Check if Option is empty |
map |
(array $o, callable $fn): array |
Transform value if present |
bind |
(array $o, callable $fn): array |
Chain Option-returning operations |
unwrapOr |
(array $o, mixed $default): mixed |
Get value or default |
fromNullable |
(?mixed $value): array |
Create from nullable value |
fromArray |
(array $arr, int\|string $key): array |
Create from array key |
toResult |
(array $o, mixed $error): array |
Convert to Result |
matchOption |
(array $o, callable $some, callable $none): mixed |
Pattern match |
Validation Type
| Function | Signature | Description |
|---|---|---|
valid |
(mixed $value): array |
Create valid Validation |
invalid |
(array $errors): array |
Create invalid Validation |
isValid |
(array $v): bool |
Check if valid |
isInvalid |
(array $v): bool |
Check if invalid |
errors |
(array $v): array |
Get error list |
map |
(array $v, callable $fn): array |
Transform value if valid |
combine |
(array $validations): array |
Combine, accumulating errors |
lift |
(callable $fn, array ...$validations): array |
Apply function to validations |
toResult |
(array $v): array |
Convert to Result |
matchValidation |
(array $v, callable $valid, callable $invalid): mixed |
Pattern match |
Testing
Run the test suite:
Contributing
Contributions are welcome! This project follows:
- PSR-12 coding standard
- PHPStan level 7 static analysis
- Strict types throughout
- Comprehensive test coverage for all features
Please ensure all tests pass and code style checks succeed before submitting a PR.
License
MIT License. See LICENSE file for details.
Why fp-kit?
Modern PHP has powerful features like arrow functions, named arguments, and union types that make functional programming more ergonomic. This library provides:
- Type Safety: Strict types and PHPStan ensure correctness
- Composability: Functions designed to work together seamlessly
- No Magic: Simple array-based types, no complex OOP hierarchies
- Zero Overhead: No dependencies, minimal abstraction cost
- Practical: Focused on real-world use cases, not academic purity
Inspired by functional programming patterns from Rust, Haskell, and Scala, adapted for PHP's strengths.