Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

What are transformers and normalizers?

What exactly are these two concepts in coding and when/how they should be used? What are their differences and use cases? Are they used interchangeably or have clearly different applications?

0 likes
1 reply
LaryAI's avatar
Level 58

Transformers and normalizers are concepts used in programming, particularly in the context of data processing and API development. They serve to convert data from one format to another and to ensure that data adheres to a certain structure or set of constraints.

Transformers:

A transformer is a design pattern used to take an input and convert it into a different output format while keeping the original data unchanged. This is particularly useful when you need to display or use data in a different way than it is stored. For example, in an API, you might use a transformer to convert internal data structures into a format that is more suitable for JSON responses.

Transformers are often used in the following scenarios:

  • API development, to transform internal data models to API-friendly structures.
  • Data export, to transform data into a format like CSV, JSON, or XML.
  • Data presentation, to adapt raw data into a format suitable for user interfaces.

Here's a simple example of a transformer in PHP:

class UserTransformer {
    public function transform(User $user) {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
            // You might want to exclude sensitive information like passwords
        ];
    }
}

Normalizers:

Normalizers, on the other hand, are used to convert data to a standardized format or structure. This is particularly useful for ensuring that incoming data adheres to a certain format before it is processed by your application. Normalizers can be used to sanitize and validate data.

Use cases for normalizers include:

  • Data import, to ensure that incoming data matches the expected format.
  • Form submission handling, to normalize data before validation and storage.
  • Data integration, to standardize data from various sources to a common format.

Here's a simple example of a normalizer in PHP:

class UserDataNormalizer {
    public function normalize(array $data) {
        return [
            'name' => trim($data['name']),
            'email' => strtolower(trim($data['email'])),
            // Additional normalization like removing special characters, etc.
        ];
    }
}

Differences and Use Cases:

Transformers and normalizers are not used interchangeably; they serve different purposes:

  • Transformers are used for output, to convert internal data into a different representation.
  • Normalizers are used for input, to ensure that incoming data is in a consistent and expected format.

In practice, you might use a normalizer when you receive data (e.g., from a user form or an external API) to prepare it for your system. After processing the data (e.g., storing it in a database), you might use a transformer to convert the data into a format suitable for returning to an API client or displaying in a user interface.

Both transformers and normalizers help in keeping the core business logic of your application separate from the data presentation and data input handling, which is a good practice for maintaining clean and maintainable code.

1 like

Please or to participate in this conversation.