PHP code example of ofelix03 / transformer

1. Go to this page and download the library: Download ofelix03/transformer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ofelix03 / transformer example snippets


	// An http request payload from a POST request
	$data = array(
			'title' => 'Hey, am a title',
			'description' => 'Hey, am simple description',
			'pub_date' => '2016-05-10 10:05:30',
			'comments_count' => '10',
		);

	// In some projects, where you have received an http request 
	// payload like the above, which needs to be normalized to 
	// match some specific database field names, the snippet below
	// is a representation of what we might do without this package
	// (but with this package could have been done much more fluently without    
	// cluttering our application controller or codebase);

	if (isset($data['title'])) {
		$data['newTitle'] = $data['title']
		unset($data['title']);
	}

	if (isset($data['description'])) {
		$data['text'] = $data['description'];
		unset($data['description']);
	}

	if (isset($data['pub_date'])) {
		$data['published_date'] = $data['pub_date'];
		unset($data['pub_date']);
	}

	if (isset($data['comments_count'])) {
		// Here, we do just type casting, from string to integer
		$data['comments_count'] = (int) $data['comments_count']
	}

	// Now $data contains the transformed keys with their associated data


	// Using the same payload($data) as in the above snippet.

	// Here, we're using composer, hence we'll pull in composer's `vendor/autoload.php` 
	// file to do it magic (autoloading)
	hat tailors our transformation 
	// to our business model. This approach is recommend if we intend to use our 
	// transform the same data set (payload) in different locations in our code base

	// PostTransformer is suppose to implement just 2 public methods 
	// 1. createRequestKeys
	// 2. createMorphKeys
	// Both methods returns an array of key definitions which represent the definitions
	// of 'requestKeys' and 'morphKeys' as we will see in the code snippet below.

	class PostTransformer extends Transformer {
 
		// The returned array contains the keys expected from the request 
		// payload (.i.e $data)
		public function createRequestKeys() {
			return array(
					'title',
					'description',
					'pub_date',
					'comments_count',
				);
		}

		// The returned array contains keys expected to replace the 
		// specified keys in the createRequestKeys() in positional indexing order
		public function createMorphKeys() { 
			return array(
				'newTitle',
				'text',
				'published_date',  
				// This will cast the type of the value to a an integer 
				'comments_count:int' 
				);
		}
	}

	// Time to instantiate our new PostTransformer class, with the http request 
	// payload ($data) we want to transform it keys, and hopefully do some casting
	// on some values that 

				use Ofelix03\Transformer\Transformer;

		// Your code to use the Transformer class goes here.
		// Example:
		$transformedData = (new Transformer())->transform($payload, $reqKeys, $morphKeys);
	

			git clone https:\\www.github.com\ofelix03\transformer.git
		

			

	
	// Assume $data is the request payload that  be an http request payload or data set query from a database
	// or any  data source.
	$data = array(
		'title' => 'Some Post Title',
		'description' => 'Some post description here and there',
		'pub_status' => '1',
		'pub_date' => '20-06-2016 12:30:30',
		'comments_count => '10'
	);

	// Here, $reqKeys list the keys in $data which are going to 
	// be transformed to some other keys defined by the $morphKeys
	$reqKeys = array(
		'description',
		'pub_status',
		'pub_date',
		'comments_count'
	);

	// $morpKeys holds a sequential listing of keys which 
	// are to replace the keys specified in the $reqKeys. 
	// The key listings should be have the same index position has the key it 
	// would replace in the $reqKeys
	$morphKeys = array(
		'text',
		'published_status:bool',
		'published_date',
		'comments_cout:int'
	);

	// Time to transform some keys using the `\Ofelix03\Transformer\Transformer` class 
	// **NB**: Make sure to autoload `\Ofelix03\Transformer\Transformer` class before using it, 
	// else it would not work. 

	$transformer = new \Ofelix03\Transformer\Transformer($data, $reqKeys, $morphKeys);

	$result = $transformer->transform();
	
	var_dump($result);

	// $result should now hold your transformed keys with their corresponding values
	// This is the result of var_dump($result)
	array (5) {
		["title"] => string(15) "Some Post title"
		["description"] => string(36) "Some post description here and there"
		["published_status"] => bool(true)
		["published_date"] => string(19) "20-06-2016 12:30:30"
		["comments_count"] => int(0)
	}