Primus is a library of object‑oriented PHP primitives.
It provides common operations as small composable objects instead of functions.
Inspired by Elegant Objects and cactoos.
Procedural PHP:
strtolower(trim(substr($s, 0, 5)));Primus:
(new Sub(
new Lowered(
new Trimmed($s)
),
5
))->value();Each object represents exactly one operation.
Objects are immutable, final, and easy to combine.
| Procedural | Primus |
|---|---|
trim($s) |
new Trimmed($s) |
strtolower($s) |
new Lowered($s) |
substr($s, 0, 5) |
new Sub($s, 5) |
strip_tags($s) |
new WithoutTags($s) |
strlen($s) |
new LengthOfText($s) |
array_map(fn, $a) |
new Mapped($a, new FuncOf(fn)) |
array_filter($a, fn) |
new Filtered($a, new PredicateOf(fn)) |
Trimmed, Lowered, Uppered, Sub, WithoutTags, Abbreviated, LengthOfText, TextOf
Yes, No, IsEmpty, IsEmail, IsUuid, IsUrl, ThrowsIf, LogicEnvelope
ScalarOf, ScalarEnvelope, EqualTo, GreaterThan, LessThan, Ternary, Sticky
Func, FuncOf, FuncEnvelope, BiFunc, Proc, Predicate, StickyFunc, Repeated
IteratorOf, Mapped, Filtered, Joined, NoNulls
IterableOf, Mapped, Filtered, Joined, NoNulls
Positive, NonZero, Rounded
-
No
null -
No
static -
No procedural helpers
-
No mutable state
-
Immutable objects
-
Final classes
-
One class = one behavior
-
Composition over inheritance
Primus includes:
- Custom PHPUnit constraints (
HasIteratorValues,EqualsValue, …) - Mutation testing (Infection)
- Static analysis:
- PHPStan level 9
- Psalm +
haspadar/psalm-eo-rules
The Psalm rules enforce:
- No
static - No
null - No
isset()/empty() - All state must be
readonly - No traits or unnecessary inheritance
composer require haspadar/primusRequires PHP ≥ 8.2