1. Introduction

In general, API services allow other developers to retrieve datasets in well-defined and documented structure. This structure usually differs from the database tables structure thus we need to set up and use Data Transformers. We also want to hide our database column names under other attributes for security issues.

2. Data source

Let’s say we have a database table containing company workers. The simplified definition would looks as follows:

Column

Type

id

int

name

varchar

surname

varchar

email

varchar

phone

varchar

password

varchar

country_id

smallint

status

enum(‘active’,’inactive’)

role

int

updated_at

timestamp

It would be very quick and easy to output table contents into an XML/JSON structure and send it back to the end user. But is this really what we want to accomplish? Our goal is to allow the API user to retrieve a list of workers with basic information like personal number, name, surname and country. We don’t want to output workers` email, phone, password etc. In this case, of course, we can prepare a SQL query, which will select the desired 4 columns only. But this code would become single-use-case code and if other functionality residing inside the API would require workers` emails we would have to prepare another query or parametrize it in a different way. What is more, we would have to use column aliases that I don’t think we wanted to do.

Assuming we have all columns retrieved (nope, I don’t mean that we must use ‘SELECT *’ query!), we may start thinking about transforming the workers collection into the final dataset structure.

3. Transforming

I don’t want to teach programming in this article. I will provide a very simple but meaningful example. The code snippet contains two classes written in PHP. Classes are responsible for data transforming only (Single responsibility principle – Wikipedia).

<?php
namespace API\Transformers;

abstract class Transformer
{
 /**
 * @param Collection $items
 * @return array
 */
 public function transformCollection(Collection $items)
 {
 return array_map([$this, 'transform'], $items);
 }

 /**
 * @param array $item
 * @return array
 */
 public abstract function transform(array $item);
}

?>


 <?php
namespace API\Transformers\Workers;

use API\Transformers\Transformer;

class WorkerTransformer extends Transformer
{
 /**
 * @param array $user
 * @return array
 */
 public function transform($user)
 {
 return [
 'personal_nr' => $user['id'],
 'name' => $user['name'],
 'surname' => $user['surname'],
 'country' => $user['country_id'],
 ];
 }
}

?>

Why do I need abstraction here? I want to transform other resources in the exact same way. But let’s concentrate on the classes usage now. There it is:

1. $transformer = new WorkerTransformer();
2. $workers = $repository->all();
3. var_dump($transformer->transformCollection($workers));
4. var_dump($transformer->transform($workers->first()));

In the first line we create the Worker Transformer. Next line is for $workers collection retrieval from Database interface encapsulated under $repository implementation. The last two lines outputs the transformed data, both for collection of workers and a single one. We may want to JSON-encode them instead of dumping to achieve an operating, simple company-workers API.

STATSCORE_RekrutacjaIT_EN_600x400