Add fluent Numberable class (Number equivalent of Stringable) #58652
-
SummaryI'd like to propose adding a MotivationCurrently, // Static
Str::upper(Str::trim(' hello '));
// Fluent (much cleaner)
str(' hello ')->trim()->upper();However, // Current approach - nested calls (hard to read)
Number::currency(Number::clamp($price, 0, 1000), 'EUR');
// Current approach - temporary variables
$clamped = Number::clamp($price, 0, 1000);
$formatted = Number::currency($clamped, 'EUR');Proposed API// New fluent approach with num() helper
num(1234.567)->clamp(0, 1000)->currency('EUR');
// Mathematical operations
num(100)
->multiply(1.2)
->add(50)
->clamp(0, 150)
->currency('USD');
// With Conditionable trait
num($bytes)
->when($bytes > 1024, fn($n) => $n->divide(1024))
->fileSize();Proposed ImplementationA Chainable methods (return
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
That would be awesome! In fact, I also already thought about that 👍🏻 You forgot a group of methods: Static constructors
There are more terminal methods:
Furthermore, we could have fluent setters for formatters (probably rather
|
Beta Was this translation helpful? Give feedback.
-
|
@shaedrich i release the package there is the link https://github.com/Tresor-Kasenda/laravel-numberable |
Beta Was this translation helpful? Give feedback.
That would be awesome! In fact, I also already thought about that 👍🏻
You forgot a group of methods: Static constructors
parse()parseInt()parseFloat()There are more terminal methods:
pairs()Furthermore, we could have fluent setters for formatters (probably rather
__toString()) and suchformatAs()('currency'|'percentage'|'spell'|'ordinal'|'spellOrdinal'|'abbreviated'|'summarized'|'fileSize'|'humanReadable')withLocale(),withCurrency())precision,maxPrecision, others)