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

5.8k Views Asked by At

I have been using laravel to build my APIs. I use transformers to tranform data from model object.

Now instead of database, I have a response coming from an API as the data source and I want to transform that data back to a user, but I am unable to do so.

My Controller

 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);

        }

    }

My Transformer

<?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,
        ];
    }
}

Here the OnboardingEntity refers to data coming from database ideally. Here I am not fetching data from database, instead my data is from an API source. How do I go about it. I am little consfused here. Can someone give a solution?

$result has the following response

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

There are 2 best solutions below

3
On BEST ANSWER

$this->response->collection is meant to get a collection of objects, not the array. Then all of this objects is going to transformer that is transform OnboardingEntity objects as you want. So first you should transform your input array into collection of objects. The example how i did it above (you should change it to your own input array)

$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]));

And then pass this collection of OnboardingEntity objects to $this->response->collection method, like here $this->response->collection($data,new TestTransformer());

0
On

You may want to send common data structure to Fractal as sources of data are different. Array is best possible type for you.

Consider this when you are fetching data from Eloquent(DB):

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

Before passing this object to fractal convert it to array.

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

In case of first or single model object. check for null before calling toArray().

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

Now for second scenario where data is coming from external source like API or file.

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

You can do this with json_decode(<Body of response>, true). And then pass this array to Fractal.

Why Array?
Because source of data could be anything from Database to File, from Cache to APIs. Format could be JSON or XML. Converting all these to array comes built in PHP.