定制 derheyne/laravel-collection-mapwithcast 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

derheyne/laravel-collection-mapwithcast

Composer 安装命令:

composer require derheyne/laravel-collection-mapwithcast

包简介

Automatically cast values in Laravel collections when using mapWithCast with typed closures.

README 文档

README

GitHub Tests Action Status

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 and Carbon\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.

derheyne/laravel-collection-mapwithcast 适用场景与选型建议

derheyne/laravel-collection-mapwithcast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 255 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Daniel Heyne」 「laravel-collection-mapwithcast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 derheyne/laravel-collection-mapwithcast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 derheyne/laravel-collection-mapwithcast 我们能提供哪些服务?
定制开发 / 二次开发

基于 derheyne/laravel-collection-mapwithcast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 255
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-13