Laravel Worth Objects is a set of general-purpose worth objects you should use in your Laravel utility. Worth objects assist characterize easy entities reminiscent of cash or X/Y coordinates on a 2D axis. Particularly, this bundle presents the next worth objects you’d sometimes run into:
- Boolean
- Quantity
- Textual content
- Electronic mail
- Full Title
- Title
- Tax Quantity
- UUID
Take working with a person’s full title, for instance you may want to separate up a title
string into a primary and final title:
1$title = new FullName(' Joe Person ');
2Â
3$title->fullName(); // 'Joe Person'
4$title->firstName(); // 'Joe'
5$title->lastName(); // 'Person'
This bundle additionally offers some Laravel-specific goodies, reminiscent of extending worth objects through Laravel’s Macroable trait and Laravel’s Conditionable trait, which applies a callback when a situation is “truthy”:
1TaxNumber::from('PL0123456789')
2 ->when(operate ($quantity) {
3 return $quantity->prefix() !== null;
4 })
5 ->prefix();
One other instance is the Quantity
worth object, which you should use to scale numbers for instance:
1$quantity = new Quantity('10.20999', scale: 2);
2$quantity = Quantity::make('10.20999', scale: 2);
3$quantity = Quantity::from('10.20999', scale: 2);
4Â
5$quantity->worth(); // '10.20'
6(string) $quantity; // '10.20'
7$quantity->toArray(); // ['10.20']
As it’s also possible to see, this bundle offers a couple of static constructors you should use to create a price object and deal with invalid information through the makeOrNull()
static constructor.
You’ll be able to be taught extra about this bundle, get full set up directions, and consider the supply code on GitHub. The readme has documentation on the way to use every of the offered worth objects.