PHP code example of crazycodr / data-transform

1. Go to this page and download the library: Download crazycodr/data-transform 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/ */

    

crazycodr / data-transform example snippets


//Create an employee transformer
$employeeTransformer = new ClosureTransformer(function($data, $key, $previous){ 
	$result = new Employee();
	$result->setName($data['name']);
	$result->setType($data['type']);
	$result->setSex($data['sex']);
	$result->setBirthdate($data['birthdate']);
	return $result;
});
$iterator = new TransformingIterator(new TransformerContainer(), $data);
$iterator->addTransformer($employeeTransformer);

//Iterate our data source and automatically only get males
foreach($iterator as $employee)
{
	echo 'Employee: '.$employee->getName().'<br>';
}

//Create an employee transformer
$employeeTransformer = new ClosureTransformer(function($data, $key, $previous){ 
	$result = new Employee();
	$result->setName($data['name']);
	$result->setType($data['type']);
	$result->setSex($data['sex']);
	$result->setBirthdate($data['birthdate']);
	return $result;
});
$ageCalculatorTransformer = new ClosureTransformer(function($data, $key, $previous){ 
	$birthday = new DateTime($previous->getBirthday());
	$interval = $birthday->diff(new DateTime());
	$previous->setAge($interval->y);
	return $previous;
});
$iterator = new TransformingIterator(new TransformerContainer(), $data);
$iterator->addTransformer($employeeTransformer);
$iterator->addTransformer($ageCalculatorTransformer);

//Iterate our data source and automatically only get males
foreach($iterator as $employee)
{
	echo 'Employee: '.$employee->getName().' is '.$employee->getAge().' years old<br>';
}

//See code in previous snipet
$container = new TransformerContainer();
$container->addTransformer($employeeTransformer);
$container->addTransformer($ageCalculatorTransformer);

$result = $container->transform($data, null);

class EmployeeTransformer extends ClosureTransformer
{
	public function __construct()
	{
		parent::__construct(function($data, $key, $previous){ 
			$result = new Employee();
			$result->setName($data['name']);
			$result->setType($data['type']);
			$result->setSex($data['sex']);
			$result->setBirthdate($data['birthdate']);
			return $result;
		});
	}
}

class EmployeeTransformingIterator extends TransformingIterator
{
	public function __construct($data)
	{
		parent::__construct(new TransformerContainer(), $data);
		$this->addTransformer(new EmployeeTransformer());
	}
}

class EmployeeTransformerTest extends PHPUnit_Framework_TestCase
{

	/**
	* @dataProvider employeeTransformerDataProvider
	*/
	public function testTransform($data)
	{
		$transformer = new EmployeeTransformer();
		$this->assertEquals($data['expected'], $transformer->transform($data['testdata'], null));
	}
	
	public function employeeTransformerDataProvider()
	{
		//To contain the results returned by the data provider
		$result = array();
		
		//Item 1
		$emp = new Employee();
		$emp->setName('John doe');
		$emp->setBirthdate('1768-01-01');
		$emp->setSex('male');
		$result[] = array(
			'expected' => $emp,
			'testdata' => array('name' => 'John doe', 'birthdate' => '1768-01-01', 'sex' => 'male'),
		);
		
		//Item 2
		$emp = new Employee();
		$emp->setName('Jane doe');
		$emp->setBirthdate('1768-01-01');
		$emp->setSex('female');
		$result[] = array(
			'expected' => $emp,
			'testdata' => array('name' => 'Jane doe', 'birthdate' => '1768-01-01', 'sex' => 'female'),
		);
		
		return $results;
		
	}
	
}