我可以使用转换器来转换来自 API 而不是来自数据库的数据吗?

Can I use transformers to transform data coming from API rather than from database?

我一直在使用 laravel 构建我的 API。我使用转换器来转换来自模型对象的数据。

现在,我收到来自 API 的响应而不是数据库,作为数据源,我想将该数据转换回用户,但我无法这样做。

我的控制器

 public function rocByName(Request $request)
    {
        try {
            $this->roc_by_name_validator->with( $request->all() )->passesOrFail();
            $company_name = $request->input('company_name');
            $result = $this->my_service->getDetailsByName($company_name); //$result has the response object from the API which I want to transform and give it as a response.

             return $this->response->collection($result,new OnboardingTransformer()); //Tried using tranformer like this
        }
        catch (ValidatorException $e) {

            dd($e);

        }

    }

我的变形金刚

<?php

namespace Modules\Onboarding\Transformers;

use League\Fractal\TransformerAbstract;
use App\Entities\OnboardingEntity;  //I dont have an entity since the response is coming from an API!! What will give here?

/**
 * Class OnboardingTransformerTransformer
 * @package namespace App\Transformers;
 */
class OnboardingTransformer extends TransformerAbstract
{

    /**
     * Transform the \OnboardingTransformer entity
     * @param \OnboardingTransformer $model
     *
     * @return array
     */
    public function transform(OnboardingEntity $data_source) 
    {
        return [
            'company_name'         => $data_source->company_name,
        ];
    }
}

这里的 OnboardingEntity 理想情况下是指来自数据库的数据。这里我不是从数据库中获取数据,而是我的数据来自 API 源。我该怎么做。我在这里有点困惑。有人可以提供解决方案吗?

$result 有以下响应

[
    [
        {
            "companyID": "U72400MHTC293037",
            "companyName": "pay pvt LIMITED"
        },
        {
            "companyID": "U74900HR2016PT853",
            "companyName": "dddd PRIVATE LIMITED"
        }
    ]
]

$this->response->collection 是为了获取对象的集合,而不是数组。然后所有这些对象都将转换为您想要转换 OnboardingEntity 对象的转换器。因此,首先您应该将输入数组转换为对象集合。我上面的例子(你应该把它改成你自己的输入数组)

$data = json_decode('[
    [
        {
            "companyID": "U72400MHTC293037",
            "companyName": "pay pvt LIMITED"
        },
        {
            "companyID": "U74900HR2016PT853",
            "companyName": "dddd PRIVATE LIMITED"
        }
   ]
]');  

$data = collect( array_map( function($ob){
    return (new OnboardingEntity($ob));
}, $data[0]));

然后将这个 OnboardingEntity 对象集合传递给 $this->response->collection 方法,就像这里 $this->response->collection($data,new TestTransformer());

您可能希望将通用数据结构发送到 Fractal,因为数据来源不同。数组是最适合您的类型。

从 Eloquent(DB) 获取数据时请考虑这一点:

 $result = $yourModel->get(); // This will return you with a collection object.

在将此对象传递给 fractal 之前将其转换为数组。

 $this->response->collection($result->toArray(),new OnboardingTransformer());

first 或单个模型对象的情况下。在调用 toArray() 之前检查是否为 null。

 $result = $yourModel->first();
 if($result){
      $result = $result->toArray(); 
 }
 // Fractal itself can handle null 

现在是第二种情况,其中数据来自 API 或文件等外部来源。

 $result = $this->my_service->getDetailsByName($company_name);
 // Try converting your response object to array from within

您可以使用 json_decode(<Body of response>, true) 执行此操作。然后将这个数组传递给 Fractal.

为什么是数组?
因为数据源可以是任何东西,从数据库到文件,从缓存到 APIs。格式可以是 JSON 或 XML。将所有这些转换为数组内置于 PHP.