endoumame / php-monad
📦 Implemented the concept of monads in functional programming in PHP.
v1.0.2
2026-02-16 14:27 UTC
Requires
- php: >=8.3
Requires (Dev)
- endou-mame/php-cs-fixer-config: ~8.4.4
- jiripudil/phpstan-sealed-classes: ^1.2
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^1.12
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.3
README
関数型プログラミングのモナド概念を PHP で実装したライブラリです。Rust の Option / Result 型に着想を得ています。
インストール
composer require endoumame/php-monad
使用例
Option
use EndouMame\PhpMonad\Option; $name = Option\fromValue($user['name'] ?? null) ->map(fn($n) => strtoupper($n)) ->filter(fn($n) => strlen($n) > 0) ->unwrapOr('Anonymous');
Result
use EndouMame\PhpMonad\Result; $result = Result\fromThrowable( fn() => json_decode($json, flags: JSON_THROW_ON_ERROR), fn($e) => "Parse error: {$e->getMessage()}" ); $data = $result->map(fn($d) => $d['key'])->unwrapOr(null);
ドキュメント
詳細なガイドと API リファレンスは ドキュメントサイト を参照してください。
要件
- PHP 8.3 以上
ライセンス
MIT License