> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/transformer/usage.md).

# Usage

## Transform a result list

The common flow is to fetch records in a service and map them through a transformer.

```php
use Quantum\Transformer\Contracts\TransformerInterface;

class PostTransformer implements TransformerInterface
{
    public function transform($item): array
    {
        return [
            'uuid' => $item->uuid,
            'title' => $item->title,
            'updated_at' => $item->updated_at,
        ];
    }
}

$payload = transform($posts, new PostTransformer());
```

This keeps response shaping separate from query or domain logic.

## Reuse one transformer instance across a collection

```php
$transformer = new PostTransformer();

$list = transform($posts, $transformer);
$featured = transform($featuredPosts, $transformer);
```

The package passes the same transformer object through the full collection mapping. That fits stateless transformers well and also works for transformers that carry shared formatting context.

## Choose the right path for one record

The package focuses on collection mapping. For one record, call the transformer directly when that reads better in your code.

```php
$item = $posts[0];
$payload = (new PostTransformer())->transform($item);
```

If you want to keep the same collection-shaped flow, wrap the item and read the first transformed value.

```php
$payload = transform([$item], new PostTransformer())[0];
```

## Preserve or reindex keys intentionally

`transform()` uses PHP array mapping semantics, so associative keys stay attached to their transformed values.

```php
$byUuid = [
    'a1' => $postA,
    'b2' => $postB,
];

$transformed = transform($byUuid, new PostTransformer());
// keys remain: a1, b2
```

If you want sequential numeric keys, normalize the input first.

```php
$transformed = transform(array_values($byUuid), new PostTransformer());
```

## Convert iterables before mapping

The package works with arrays. When your data source returns another iterable type, convert it before calling the helper.

```php
$payload = transform(iterator_to_array($items), new PostTransformer());
```

## Keep output shape predictable

The interface accepts any return value, so the clearest application code comes from returning one consistent structure for each item in the same collection.

## Empty collections flow through cleanly

An empty input array returns an empty array, which makes the helper a good fit for response pipelines that already expect a collection result.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/transformer/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
