Transformer 方法能否添加一个参数,返回结果根据参数转换?

您好,php新手,还不太理解php语法,试了几个写法都行不通,在此请教。
能否在下面的转换层,添加一个参数$type,让方法通过不同的$type进行转换?
如果可以的话,应该怎么调用呢?下面两种方式偶读不对

$this->response->paginator($user, new UserTransformer(),true);
$this->response->paginator($user, new UserTransformer(true));

例如:

    public function transform(User $user,$type)
    {
        if ($type) {
            return [
                'id' => $user->id,
                'phone' => $user->phone,
                'name' => $user->name,
                'nickname' => $user->nickname];
        }
        return [
            'id' => $user->id,
            'phone' => $user->phone,
            'name' => $user->name,
            'nickname' => $user->nickname,
            'avatar' => $user->avatar ? config('app.url') . $user->avatar : null,
            'introduction' => $user->introduction,
            'school_id' => $user->school_id,
            'bound_wechat' => ($user->weixin_unionid || $user->weixin_openid) ? true : false,
        ];
    }
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
liyu001989
最佳答案

你可能想这样

<?php

namespace App\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    protected $type;

    public __construct($type)
    {
        $this->type = $type;
    }

    public function transform(User $user)
    {
        if ($this->type)......
    }
}
6年前 评论
讨论数量: 5
liyu001989

你可能想这样

<?php

namespace App\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    protected $type;

    public __construct($type)
    {
        $this->type = $type;
    }

    public function transform(User $user)
    {
        if ($this->type)......
    }
}
6年前 评论
liyu001989

你可能想这样

<?php

namespace App\Transformers;

use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    protected $type;

    public __construct($type)
    {
        $this->type = $type;
    }

    public function transform(User $user)
    {
        if ($this->type)......
    }
}
6年前 评论
  • 老师的方法真棒。
  • transform 这个方法的参数是在fractal的源码中写死了的。源码如下:

    
        /**
        * Fire the main transformer.
        *
        * @internal
        *
        * @param TransformerAbstract|callable $transformer
        * @param mixed                        $data
        *
        * @return array
        */
        protected function fireTransformer($transformer, $data)
        {
            $includedData = [];
    
            if (is_callable($transformer)) {
                $transformedData = call_user_func($transformer, $data);
            } else {
                $transformer->setCurrentScope($this);
                $transformedData = $transformer->transform($data); //就是这里。。
            }
    
            if ($this->transformerHasIncludes($transformer)) {
                $includedData = $this->fireIncludedTransformers($transformer, $data);
                $transformedData = $this->manager->getSerializer()->mergeIncludes($transformedData, $includedData);
            }
    
            //Stick only with requested fields
            $transformedData = $this->filterFieldsets($transformedData);
    
            return [$transformedData, $includedData];
        }
5年前 评论

@liyu001989 这种方法可以理解为 依赖注入 对吧!

5年前 评论
liyu001989

@AllenBool 只是给一个类传参数,不是依赖注入啊

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!