wp-grogu / laravel-fluent-plus
A fork from janpantel/laravel-fluent-plus
Installs: 2 159
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.3|^8.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-10-12 09:29:23 UTC
README
This package extends Laravel's Fluent
class to provide a
supercharged experience like type casting and recursive
Fluent access to sub-assocs.
Installation
Use composer to grep the package :
composer require wp-grogu/laravel-fluent-plus
Basic usage
Instead of extending Illuminate\Support\Fluent
you extend
Grogu\FluentPlus\FluentPlus
instead. As this
class is a superset of Fluent it can be treated as a drop
in replacement.
FluentPlus
implements a $casts
property that defines
how the attributes of your assoc should be cast.
For the following example assume NamesContainer
, Price
and StockLevel
to be classes extending Fluent
or FluentPlus
as well.
class MyContainer extends FluentPlus { protected $casts = [ // Use the contents of the `names` attribute // to initialize a new `NamesContainer` instance. 'names' => NamesContainer::class, // Assumes that the `price` attribute contains // an array and initializes a new `Price` instance // for every item. 'prices' => [Price::class], // The same as the array syntax but using // Laravel's `Collection` class instead. 'stock' => [ \Illuminate\Support\Collection::class, StockLevel::class ], ]; }
The resulting object now offers convenient property access:
$container = new MyContainer($sourceAssoc); var_dump($container->names->english); var_dump($container->prices[0]->amount); var_dump($container->stock->get('store_1')->quantity);
The problem this package solves
If you work with assocs a lot and like using property access
over assoc access Laravel's Fluent
class is a convenient
way to do so.
The use case that made me write this package is working
with responses fetched from 3rd party APIs. Since I want
to use @property
PHP docs to have "type safety" while
not manually assigning properties.
Advanced usage
Recursive array casts
FluentPlus
casts sub-assocs that do not
have a cast defined into other FluentPlus for infinite
dimension property access.
$instance = new Fluent(['foo' => ['bar' => 'baz']]); $instancePlus = new FluentPlus(['foo' => ['bar' => 'baz']]); // breaks :( var_dump($instance->foo->bar); // works :) var_dump($instancePlus->foo->bar);
You can turn this feature off by setting the $recursive
property in your derived FluentPlus
to false
or using
the NonRecursiveFluentPlus
utility class if you want
to inline it instead.