haspadar/mono

Primitive wrappers for PHP: strong typing for strings, ints, arrays, and more

v0.1.0 2025-07-04 09:30 UTC

README

PHP Version Code Style CI PHP Metrics

Tests Coverage PHPStan Level Psalm Psalm Type Coverage Mutation MSI

๐Ÿ“ฆ About

Mono is a library of immutable value objects that wrap primitive types like string, int, bool, and array.

Instead of passing around loose values, you use small, self-contained wrappers like:

  • Lowercased, Trimmed, TruncatedRight (strings)
  • Yes, IsEmail, ThrowsIf (logic)
  • Mapped, SequenceOf (collections)

Each class encapsulates one behavior and can be composed with others to form robust, intention-revealing objects.

๐Ÿง  Philosophy

  • โŒ No null, static, or shared state in the public API
  • โœ… One object = one responsibility
  • โœ… Final classes, immutability by default
  • โœ… Composition over inheritance
  • โœ… Behavior and data live together

Inspired by Elegant Objects and cactoos.

โœจ Example

use Mono\Text\Lowercased;
use Mono\Text\TruncatedRight;
use Mono\Text\Trimmed;

$text = new TruncatedRight(
    new Lowercased(
        new Trimmed("  Hello, world!  ")
    ),
    5
);

echo $text->toString(); // "hello"

Each wrapper adds one behavior:

  • Trimmed removes whitespace
  • TruncatedRight shortens the string

All wrappers implement the same interface and can be freely composed.

๐Ÿงฑ Modules

  • Text โ€” Trimmed, Uppercased, Lowercased, TruncatedRight, HtmlSanitized, LengthOf, Preview, TextOf
  • Logic โ€” Yes, No, ThrowsIf, IsEmpty, IsEmail, IsUuid, LogicEnvelope
  • Iterable โ€” Sequence, SequenceOf, Mapped, Filtered
  • Number โ€” (coming soon) Positive, NonZero, Rounded, etc.

๐Ÿงช Quality & CI

Every push and pull request is checked via GitHub Actions:

  • โœ… Static analysis with PHPStan (level 9) and Psalm (level 8)
  • โœ… Type coverage report via Shepherd
  • โœ… Code style check with PHP-CS-Fixer (only changed files)
  • โœ… Unit tests with PHPUnit
  • โœ… Code coverage via Codecov
  • โœ… Mutation testing with Infection
  • โœ… Composer validation, platform checks, security audit
  • โœ… Automatic refactoring via Rector

๐Ÿ“ฅ Installation

composer require haspadar/mono

Requires PHP 8.4

๐Ÿ“„ License

MIT