CodeIgniter v4.7 API

BaseTransformer
in package
implements TransformerInterface

AbstractYes

Base class for transforming resources into arrays.

Fulfills common functionality of the TransformerInterface, and provides helper methods for conditional inclusion/exclusion of values.

Supports the following query variables from the request:

  • fields: Comma-separated list of fields to include in the response (e.g., ?fields=id,name,email) If not provided, all fields from toArray() are included.
  • include: Comma-separated list of related resources to include (e.g., ?include=posts,comments) This looks for methods named include{Resource}() on the transformer, and calls them to get the related data, which are added as a new key to the output.

Example:

class UserTransformer extends BaseTransformer { public function toArray(mixed $resource): array { return [ 'id' => $resource['id'], 'name' => $resource['name'], 'email' => $resource['email'], 'created_at' => $resource['created_at'], 'updated_at' => $resource['updated_at'], ]; }

protected function includePosts(): array { $posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll(); return (new PostTransformer())->transformMany($posts); } }

Table of Contents

Interfaces

TransformerInterface
Interface for transforming resources into arrays.

Properties

$resource  : mixed
$fields  : array<int, string>|null
$includes  : array<int, string>|null
$request  : IncomingRequest|null

Methods

__construct()  : mixed
toArray()  : array<string, mixed>
Converts the resource to an array representation.
transform()  : array<string, mixed>
Transforms the given resource into an array using the $this->toArray().
transformMany()  : array<int, array<string, mixed>>
Transforms a collection of resources using $this->transform() on each item.
getAllowedFields()  : array<int, string>|null
Define which fields can be requested via the 'fields' query parameter.
getAllowedIncludes()  : array<int, string>|null
Define which related resources can be included via the 'include' query parameter.
insertIncludes()  : array<string, mixed>
Checks the request for 'include' query variable, and if present, calls the corresponding include{Resource} methods to add related data.
limitFields()  : array<string, mixed>
Limits the given data array to only the fields specified

Properties

Methods

toArray()

Converts the resource to an array representation.

public abstract toArray(mixed $resource) : array<string, mixed>

This is overridden by child classes to define the API-safe resource representation.

Parameters
$resource : mixed

The resource being transformed

Return values
array<string, mixed>

transform()

Transforms the given resource into an array using the $this->toArray().

public transform([array<string|int, mixed>|object|null $resource = null ]) : array<string, mixed>
Parameters
$resource : array<string|int, mixed>|object|null = null
Return values
array<string, mixed>

transformMany()

Transforms a collection of resources using $this->transform() on each item.

public transformMany(array<string|int, mixed> $resources) : array<int, array<string, mixed>>

If the request's 'fields' query variable is set, only those fields will be included in the transformed output.

Parameters
$resources : array<string|int, mixed>
Return values
array<int, array<string, mixed>>

getAllowedFields()

Define which fields can be requested via the 'fields' query parameter.

protected getAllowedFields() : array<int, string>|null

Override in child classes to restrict available fields. Return null to allow all fields from toArray().

Return values
array<int, string>|null

getAllowedIncludes()

Define which related resources can be included via the 'include' query parameter.

protected getAllowedIncludes() : array<int, string>|null

Override in child classes to restrict available includes. Return null to allow all includes that have corresponding methods. Return an empty array to disable all includes.

Return values
array<int, string>|null

insertIncludes()

Checks the request for 'include' query variable, and if present, calls the corresponding include{Resource} methods to add related data.

private insertIncludes(array<string, mixed> $data) : array<string, mixed>
Parameters
$data : array<string, mixed>
Return values
array<string, mixed>

limitFields()

Limits the given data array to only the fields specified

private limitFields(array<string, mixed> $data) : array<string, mixed>
Parameters
$data : array<string, mixed>
Tags
throws
InvalidArgumentException
Return values
array<string, mixed>

        
On this page

Search results