derheyne / laravel-collection-mapwithcast
Automatically cast values in Laravel collections when using mapWithCast with typed closures.
Fund package maintenance!
derheyne
Requires
- php: ^8.3
- illuminate/contracts: ^11.35||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^2.36||^3.8
- pestphp/pest-plugin-arch: ^2.7||^3.1
- pestphp/pest-plugin-laravel: ^2.4||^3.1
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2025-06-16 19:08:44 UTC
README
Automatically cast values in Laravel collections when using mapWithCast
with typed closures.
collect([['name' => 'John'], ['name' => 'Jane']])
->mapWithCast(fn (Fluent $data) => $data->name)
// Result: ['John', 'Jane']
๐ฆ About
mapWithCast
is a Laravel collection macro that enhances the map method by automatically casting each item in the
collection to the type hinted in your closure. It saves you from manual casting and enforces better type safety, making
your code cleaner and more expressive.
It supports both scalar types like int
and string
and complex Laravel-specific types like Collection
, Fluent
, and
Stringable
.
๐ Installation
Install the package via Composer:
composer require derheyne/laravel-collection-mapwithcast
The macro will be automatically registered thanks to Laravel's package discovery.
๐ง Type Support
โ Supported Types (Out of the Box)
int
float
bool
string
array
object
Illuminate\Support\Carbon
andCarbon\Carbon
Illuminate\Support\Collection
Illuminate\Support\Fluent
Illuminate\Support\Optional
Illuminate\Support\Stringable
Illuminate\Support\Uri
โ๏ธ Extending with Custom Casters
Need to handle your own types or custom logic? You can register additional casters by publishing the config file:
php artisan vendor:publish --tag=laravel-collection-mapwithcast-config
This will create a config file where you can specify your own custom casters:
// config/mapwithcast.php
return [
'casters' => [
App\Caster\SpatieLaravelDataCaster::class
],
];
namespace App\Casters;
use dhy\LaravelMapWithCastMacro\Contract\Caster;
use Spatie\LaravelData\Data;
class SpatieLaravelDataCaster implements Caster
{
public function qualifies(mixed $type): bool
{
if (! is_string($type)) {
return false;
}
return is_subclass_of(object_or_class: $type, class: Data::class, allow_string: true);
}
/** @param Data $type */
public function cast(mixed $value, mixed $type): Data
{
return $type::from($value);
}
}
Now you can automatically cast the value into the specified laravel-data DTO:
use Spatie\LaravelData\Data;
class CustomerData extends Data {
public function __construct(
public string $prename,
public string $surname,
public string $city,
) {}
}
collect([['prename' => 'Jane', 'surname' => 'Doe', 'city' => 'New York']])
->mapWithCast(fn (CustomerData $customer) => $customer->prename.' '.$customer->surname)
// Returns: ['Jane Doe']
๐ Examples
๐งฎ Convert and Process Numbers
$totals = collect(['10.50', '20.75', '30'])
->mapWithCast(fn (float $price) => $price * 1.2);
// Result: [12.6, 24.9, 36.0]
๐ง Cast to Laravel Collection
$sums = collect([[1, 2, 3], [4, 5, 6]])
->mapWithCast(fn (Collection $items) => $items->sum());
// Result: [6, 15]
๐ Cast to Stringable
$slugs = collect(['Laravel Tips', 'PHP Tricks'])
->mapWithCast(fn (Stringable $str) => $str->slug());
// Result: ['laravel-tips', 'php-tricks']
๐ชก Cast by specifying a custom caster
class CustomDataObject
{
public function __construct(
public string $value,
) {}
}
collect(['one', 'two', 'three'])
->mapWithCast(
callback: fn (CustomDataObject $value) => 'Value: '.$value->value,
caster: fn ($value, $type) => new $type($value),
);
// Return ['Value: one', 'Value: two', 'Value: three']
โ Testing
composer test
๐งช Compatibility
- Laravel 11.x, 12.x
- PHP 8.3+
License
The MIT License (MIT). Please see License File for more information.