Download the PHP package crazycodr/data-transform without Composer
On this page you can find all versions of the php package crazycodr/data-transform. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download crazycodr/data-transform
More information about crazycodr/data-transform
Files in crazycodr/data-transform
Package data-transform
Short Description Used to transform out data from an enumerable source
License MIT
Informations about the package data-transform
CrazyCodr/Data/Transform
This package contains facilities to easily transform live iterated data from any enumerable source.
This class features a transforming iterator accompagnied by different classes that you can use to transform incoming data from any iteratable data-source.
Table of contents
- Installation
- Creating a basic transforming iterator
- Supporting many transformers at once
- Using components outside of the iterator context
- Creating your own testable classes
Installation
To install it, just include this requirement into your composer.json
And then run composer install/update as necessary.
Creating a basic transforming iterator
Creating a transforming iterator requires at least three items:
- A TransformerContainer used to contain the different transformers for your iterator
- A TransformingIterator used to iterate your data and provide transforming features
- A Transformer used to transform the current data into something new
(Note: This code assumes that you have an array based datasource with columns: name, type, sex and birthdate)
(Note: This code assumes that you have a class called Employe that mimics the previous datasource + a new property called age)
Supporting many transformers at once
You can add many transformers at once to the transformer container. The goal being to separate the different transformation aspects.
This example demonstrate that the first transformer creates the employee but the second initializes some other property manually. (Age vs Birthdate) Just note that the second transformer must use the $previous variable to obtain the current state of the transformed object, you don't want to recreate the object again.
Using components outside of the iterator context
You don't need to use a transforming iterator... The ClosureTransformer and TransformerContainer can be used outside of a loop. Build transformers normally using concrete/non-concrete classes and call "transform" with some data.
Creating your own testable classes
The point of this library is not to have to create the iterators and they sub-components each time and be able to test the lot easily. To this end, simply create concrete extensions of your iterators and sub-components and then test them.
It might look extreme but this way you are creating a concrete functional class that can be reused and tested. Note that DataProviders are a great way to test your components but it will look strange to use a DataProvider when testing the iterators.