定制 oguzhankrcb/auto-casting-json-resource 二次开发

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

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

oguzhankrcb/auto-casting-json-resource

最新稳定版本:v0.0.1

Composer 安装命令:

composer require oguzhankrcb/auto-casting-json-resource

包简介

This Laravel package automatically casts your JsonResource data using the casting functions you have defined before.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

This package makes easier to add global castings to your JsonResource files

Installation

You can install the package via composer:

composer require oguzhankrcb/auto-casting-json-resource

Usage

Just add this trait to your JsonResource

use AutoCastingJsonResource;

And then you can cast whatever you want in casts array

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return $this->autoCast(parent::toArray($request));
    }

    public function casts(): array
    {
        return [
            'integer' => fn ($value) => (int) ($value / 100), // It will divide all integer objects with 100
            Money::class => fn (Money $value) => $value->getMinorAmount()->toInt() / 2, // It will cast all Brick\Money\Money objects to integer and divide them with 2
        ];
    }

You can exclude columns if you want (id column is excluded by default)

    public function excludedColumns(): array
    {
        return [
            'id', // id column will be excluded from castings
        ];
    }

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固