endou-mame/php-monad

📦 Implemented the concept of monads in functional programming in PHP.

Installs: 5

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/endou-mame/php-monad

v1.0.2 2026-02-16 14:27 UTC

This package is auto-updated.

Last update: 2026-02-16 14:29:02 UTC


README

Packagist Version PHP Version PHPStan Documentation License

関数型プログラミングのモナド概念を PHP で実装したライブラリです。Rust の Option / Result 型に着想を得ています。

インストール

composer require endou-mame/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