Download the PHP package lneicelis/transformer without Composer
On this page you can find all versions of the php package lneicelis/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lneicelis/transformer
More information about lneicelis/transformer
Files in lneicelis/transformer
Package transformer
Short Description API layer transformation component
License MIT
Informations about the package transformer
transformer
Goals
- Define clear, unambiguous and transparent interface between client and server
- Ensure output is always serializable (JSON, XML, YML, etc)
- Data integrity and consistent data schema across whole domain (single definition of resource transformation)
- Better separation between front-end and back-end responsibilities
- Ease of transformers testability (a transformer is always responsible for single resource transformation)
- Data security (resource property access control)
- Extendability and ability to support various use cases (data normalization, graphQL schema, etc)
Usage
For the sake of simplicity we wont use any namespaces and will be instantiating transformer instance manually. In real world applications you would use DI container for instantiating all transformer instance.
Setup
Transformer definition
Most basic transformer must define source it can transform. Transform method should return scalar value or associative array representing resource object.
Registering transformer into TransformerRegistry
Using transformer
Advanced usage
Nesting
Resource object properties or values returned from resource accessors are not always scalar values in real world app but instead other resources. In order to get only scalar values we might need to inject transformers into transformers to eventually have only scalar values that can be serialized. However that usually leads to increased complexity, difficult testing, limited scalability and bloated transformers.
Transformer package allows you not to worry about nested objects transformation. As long as resources returned from transform method has a transformer registered in transformer registry eventual result of transform method will be serializable array.
Example:
Lazy transformation (LazyPropertiesPipe)
There are cases when you do not need all available properties all the time. e.g.:
- You need granular control of when and what properties are returned from different endpoints.
- Some properties transformation is expensive (e.g. require additional DB queries);
- You want to save bandwidth by returning only relevant properties to the front-end;
In such cases we can leverage LazyPropertiesPipe that uses schema to add properties on demand. This can be especially useful when using tools like GraphQL by offloading responsibility of specifying schema to the client.
Access control (AccessControlPipe)
Since one transformer defines transformation schema for one resource an issue occurs when consumer of the resource data has different data access scope.
For example, the same resource might be used by regular user and by admin user, however admin user should be able to access more properties of the resource that a regular user.
Keeping API flexible, transparent and still allowing client to specify schema requires access control mechanism at transformation level. AccessControlPipe can be used in transformation pipeline to specify access control for whole data or individual properties.
Example: