Download the PHP package selective/transformer without Composer

On this page you can find all versions of the php package selective/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package transformer

selective/transformer

A strictly typed array transformer with dot access and fluent interface. The mapped result can be used for JSON responses and many other things.

Latest Version on Packagist Build Status Coverage Status Quality Score Total Downloads

Table of Contents

Requirements

Installation

Introduction

This Transformer component provides functionality to map, cast and loop array values from an array or object to another array.

Converting complex data with simple PHP works by using a lot of type casting, if conditions and looping through the data with foreach(). This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating".

Before: Conditions: 9, Paths: 256, CRAP Score: 9

Click to expand!

After: Conditions: 1, Paths: 1, CRAP Score: 1

Click to expand!

Use Cases

When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output. The Transformer is able to create a “barrier” between source data and output, so schema changes do not affect users.

The Transformer works also very well to put any kind of database resultset (e.g. from PDO) into a new data structure.

The uses cases are not limited.

Dot access

You can copy any data from the source array to any sub-element of the destination array using the dot-syntax.

Object access

It's possible to access the properties of an object using the dot notation.

The result:

Transforming

Transforming arrays

For the sake of simplicity, this example has been put together as though it was one file. In reality, you would spread the manager initiation, data collection and JSON conversion into separate parts of your application.

Sample data:

The result:

Transforming list of arrays

The method toArrays is able to transform a list of arrays.

This can be useful if you want to transform a resultset from a database query, or a response payload from an API.

Example:

The result:

Mapping Rules

Simple mapping rules

Using strings, separated by |, to define a filter chain:

Because lastName is blank but required the result looks like this:

Complex mapping rules

For complexer mapping rules there is a fluent interface available. To create a new rule use the rule method and pass it as 3rd. parameter to the map method.

Because lastName is blank but required the result looks like this:

Filter

Most filters are directly available as method.

Custom Filter

You can also add your own custom filter:

Add a custom filter using a callback:

Define a custom filter using a mapping specific callback:

There are even more filter classes available that can be registered manually:

You can also implement and register your own filter classes as well.

Examples

JSON conversion

You can use your own json component or just the native json_encode function.

Writing JSON to a PSR-7 response object:

PDO resultset conversion

The result:

Similar packages

License

The MIT License (MIT). Please see License File for more information.


All versions of transformer with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package selective/transformer contains the following files

Loading the files please wait ....