Download the PHP package ofelix03/transformer without Composer
On this page you can find all versions of the php package ofelix03/transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ofelix03/transformer
More information about ofelix03/transformer
Files in ofelix03/transformer
Package transformer
Short Description A simple associative (i.e. a key:value pair) data transformer which transforms the key's of an array data to some other specified keys. It also supports casting of data key's corresponding value to a specified PHP data type
License MIT
Informations about the package transformer
Transformer
A simple associative (i.e. a key:value pair) data transformer which transforms the keys of an array data to some other specified keys. It also supports casting of data values to a specified type (e.g integer, boolean, string, \DateTime e.t.c)
Visit [https://ofelix03.github.io/transformer/]()
What This Package Seeks To Achieve
- Streamlining the process of transforming data keys.
- Reducing cluttering of application controller and business logic with data normalization activities such as transformation of data keys.
- Upholding the DRY principle by employing classes for different data keys transformation.
Codes To Support The Package's Claims
Let first start with a code snippet that tries to present what we might normally do without this package.
Now let's try to use Transformer package to streamline and remove the clutter in the above code snippet, even keeping our code DRY in the process.
Installation
-
Using composer
NB: Make sure to at the top of the the file you want to use the transformer package in. Exampe: Assuming I'm using this package in a file named
main.php
, this is what mymain.php
file would look like: -
Using github clone You can also clone the github repository for this package
Simply follow the laid out steps below. Make sure you already have git environment set up on your machine. You can checkout how to do so on Git's official site
-
Step 1
Open your terminal and run the
git clone
command below: -
Step 2
Copy the php files inside directory to any location in your app directory structure and require them in this order:
-
Usage
Other API's on \Ofelix03\Transformer\Tranformer
-
Tranformer::isStrict(): bool
This checks whether the transformation should be done in strict mode or not. Returns boolean (TRUE|FALSE). In strict mode, the library checks if the
$reqKeys
is equal in length to the$morphKeys
and throws an exception if they are not. -
Transformer::setStrict($mode = false)
This allows you to set the mode for the transformation. The default mode is
FALSE
if no argument is passed. -
Transformer::isTransformed(): bool
Checks whether the data (or payload) has already been transformed, this help save time, not transforming data that has already been transformed but instead getting the transformed data with
Transformer::getMorphedData()
. -
Transformer::setRequestKeys(array $reqKeys = [])
This method allows you to define the $request keys after you've already created an instance of
Ofelix03\Transformer\Transformer
class. NB: If this method is to be called, it should be called before callingTransformer::transform()
else a run-time exception is thrown. -
Transformer::setMorphKeys(array $morphKeys = [])
This method allows you to set the keys that are to replace the $request keys during the transformation
-
Transformer::setRequestPayload(array $data)
This is used to set the data that needs to be transformed. This can be used to override the request data set during the construction of the transformer object. NB: Call this method before invoking
Tranformer::transform()
-
Transformer::transform($reqPayload = [], $strictMode = false): array
This is the method that does the magic -- transforming keys to other speicified keys. And also perform type casting if necessary.
-
$reqPayload This argument is optional. It's the data upon which the transformation is applied on, using the $requestKeys and $morphKeys definitions
- $strictMode
This second argument indicates the mode used for the transformation. It's optional. Remember this can also be set with the
Transformer::setStrict()
as discussed earlier.
-
-
Transformer::getMorphedData(): array
This method is called after invoking to get the transformed data (data with it's keys morphed into other keys).
Casting
The following are the types currently supported for casting data.
- Integer (int)
- String (string)
- Array (array)
- Boolean (bool)
- DateTime
Contributing
You can help improve this docs by sending me a pull request and hopefully I will merge it in. Also, you spotted an error (syntax or logic error) ? I will be glad to recieve a pull request of a fix of that error. But I would love it if you first open an issue and hopefully if it's not already in the pipes of resolution by me or someone else, I will gladly assign it to you.