Hyperf resource 与 collection 合并处理

laravel API 资源文档

基础设计和场景

  • 按照laravel的设计,一个resource表示一个单一模型需要被转换成 JSON 格式,当你需要吐出的数据,跨越了单个模型数据范围,这个时候你就可以使用collection.这一套API里面能够给你支持自定义数据格式,数据展现条件,嵌套等,这些都很有必要,但是依旧不够灵活
  • 通常业务比较复杂,when的用法就显得太不够用
  • resource本来就是负责格式化数据出去,链接模型,集合与吐出数据的纽带,按道理它只负责有什么数据就吐什么数据,它里面应该不包含业务

基于以上的原因,做了一点扩展

  • 对resource本身API无任何冲突
  • 更加面向业务的链式调用
  • 直面resource进行编程

一点点简单的改变

use Fangx\Resource\Json\JsonResource as OrgJsonResource;

class JsonResource extends OrgJsonResource
{
    protected $field = [];

    public function __construct($resource = NULL) {
        parent::__construct($resource);
    }

    public function withoutWrapping() {
        $this->wrap = NULL;
        return $this;
    }

    /**
     *
     * 兼容单行数据(model)和数据集(collection)
     *
     * @param $resource
     *
     * @return array
     */
    public function toApi($resource) {

        if ($resource instanceof \Traversable) {
            $res = [];
            foreach ($resource as $item) {
                $this->resource = $item;
                $res[]          = array_merge($this->toArray(), $this->fieldAppend());
            }
            return $res;
        } else {
            $this->resource = $resource;
            $res            = array_merge($this->toArray(), $this->fieldAppend());
            return $res;
        }
    }

    /**
     * 处理回调
     *
     * @return array
     */
    public function fieldAppend(): array {
        $res = [];
        if (!empty($this->field)) {
            foreach ($this->field as $index => $item) {
                $res[$index] = is_callable($item) ? $item($this->resource) : $item;
            }
        }
        return $res;
    }

resource编码实例

<?php

namespace App\Resource;


class TestResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array
     */
    public function toArray(): array {
        return [
            'id'   => 1,
            'name' => 'come'
        ];
    }

    public function withTest1() {
        $this->field['role1'] = [
            'id'   => 1,
            'name' => '主管'
        ];
        return $this;
    }


    public function withTest2() {
        //这个resource 就是模型里面传的 resource
        $this->field['role2'] = function ($resource) {
           return  [
                'id'       => 2,
                'name'     => '主任',
                'resrouce' => $resource
            ];
        };
        return $this;
    }
}

调用实例

return $this->succ((new TestResource())
                  ->withTest1()
                  ->withTest2()
                  ->toApi(['who' => '我是resource']));

结果

{
    "msg": "操作成功",
    "err": 0,
    "ext": {
        "id": 1,
        "name": "come",
        "role1": {
            "id": 1,
            "name": "主管"
        },
        "role2": {
            "id": 2,
            "name": "主任",
            "resrouce": {
                "who": "我是resource"
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。