LogoLogo
Frontend
  • Introduction
  • Getting started
  • About
  • Retrieving data
    • Introduction
    • Making a request
    • Property paths
    • Data providers
    • HTTP data provider
    • GraphQL data provider
    • Queries
    • GraphQL queries
    • Query Manager
    • Custom query classes
    • Bulk queries
  • Changing data
    • Introduction
    • Transforming and mapping data
    • Accessing properties
    • Transforming data
    • Available transformers
    • Mapping data
  • Advanced usage
    • Validation
    • Caching
    • Data History
    • Events
    • Testing API requests
Powered by GitBook
On this page
  • Transforming single values
  • Transforming all values
  • Transforming data
  • What happens if data cannot be transformed?

Was this helpful?

  1. Changing data

Transforming data

PreviousAccessing propertiesNextAvailable transformers

Last updated 2 years ago

Was this helpful?

Basic concepts of transformers are explained below. Transformers are often used with , instructions for independant usage appears below. Also see full details on all .

Transformers come in three types:

Transforming single values

Single value transformers are used to transform an individual value as it is accessed from data (array or object). This transformer can be used when setting up .

You need to pass the property path to the property you want to retrieve via a value transformer (e.g. '[date]'). See for details on how to define a property path.

For example, this returns a DateTime object from $data['date']:

use Strata\Data\Transform\Value\DateTimeValue;

$data = [
    'date' => '2021-04-05T11:09:15+00:00'
];

$valueTransformer = new DateTimeValue('[date]');

if ($valueTransformer->isReadable($data)) {
    /** @var \DateTime $date */
    $date = $valueTransformer->getValue($data);
}

Transforming all values

A value transformer loops through all data applying the transformation to each value.

For example this sets all empty values to null to help return predictable data:

use Strata\Data\Transform\AllValues\SetEmptyToNull;

$transform = new SetEmptyToNull();
$data = $transform->transform($data);

Transforming data

A data transformer acts on data as a whole.

For example this maps data values stored in $data['item']['category'] to local values:

use Strata\Data\Transform\Data\MapValues;

// New value => old values
$mapping = [
    'casual'    => ['T-Shirts', 'Jeans'],
    'outdoor'   => ['Scarfs', 'Coats'],
];

$transform = new MapValues('[item][category]', $mapping);
$data = $transform->transform($data);

What happens if data cannot be transformed?

Single value transformers have the isReadable() method to test whether that value can be accessed. If a single value transformer cannot be transformed it returns null.

If all values or data transformers cannot transform data they skip over it. These transformers contain a canTransform() method which is used to check the data value is of the correct type to be transformed. This helps avoid unwanted errors.

If a data transformers implements the NotTransformedInterface interface then you have access to two methods to help determine what data was not transformed. This can be useful, for example, if you map values and want to access any old values that did not map to new values.

if ($transform->hasNotTransformed()) {
    $notTransformed = $transform->getNotTransformed();
}

See available .

See available .

See available .

mapping
available transformers
Single values
All values
Data
mapping fields
how to write property paths
single value transformers
all value transformers
data transformers