haspadar/primus
最新稳定版本:v0.5.0
Composer 安装命令:
composer require haspadar/primus
包简介
Primitive wrappers for PHP: strong typing for strings, ints, arrays, and more
README 文档
README
Object‑Oriented PHP Primitives
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.
📦 Core Idea
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.
🧭 Quick Reference
| 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)) |
🧱 Modules
Text
Trimmed, Lowered, Uppered, Sub, WithoutTags, Abbreviated, LengthOfText, TextOf
Logic
Yes, No, IsEmpty, IsEmail, IsUuid, IsUrl, ThrowsIf, LogicEnvelope
Scalar
ScalarOf, ScalarEnvelope, EqualTo, GreaterThan, LessThan, Ternary, Sticky
Func
Func, FuncOf, FuncEnvelope, BiFunc, Proc, Predicate, StickyFunc, Repeated
Iterator
IteratorOf, Mapped, Filtered, Joined, NoNulls
Iterable
IterableOf, Mapped, Filtered, Joined, NoNulls
Numeric (WIP)
Positive, NonZero, Rounded
🧠 Design Principles
-
No
null -
No
static -
No procedural helpers
-
No mutable state
-
Immutable objects
-
Final classes
-
One class = one behavior
-
Composition over inheritance
🧪 Testing & Static Analysis
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
📥 Installation
composer require haspadar/primus
Requires PHP ≥ 8.2
📄 License
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-10