Skip to content

Instantly share code, notes, and snippets.

@thepsion5
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepsion5/11008147 to your computer and use it in GitHub Desktop.
Save thepsion5/11008147 to your computer and use it in GitHub Desktop.
Simple example of a transformer class
<?php
namespace Infrastructure\Database;
use DateTime,
Domain\Person\PersonEntity as Person,
Domain\Person\Name,
Domain\Person\Email;
class ExamplePersonDomainObjectTransformer extends AbstractTransformer
{
public function transform(array $data)
{
$name = new Name($data['first'], $data['last'], $data['suffix']);
$email = new Email($data['email_address']);
$person = new Person($name, $email, new DateTime($timestamp));
return $person;
}
}
<?php
namespace Acme\Precincts\Transformers;
use Acme\Support\Transformers\AbstractTransformer;
class PrecinctApiTransformer extends AbstractTransformer
{
/**
* Fields that should be converted into individual district objects
* @var array
*/
protected $districtFields = array(
'congressional' => 'US House',
'tn_senate' => 'TN Senate',
'tn_house' => 'TN House',
'judicial' => 'Judicial',
'city' => 'Municipal',
'school' => 'School',
'ward' => 'Ward',
'special_school' => 'Special School'
);
/**
* @param \Acme\Precincts\Precinct $precinct
* @return array
*/
public function transform($precinct)
{
//TODO: Add tests
$transformed = array();
foreach($this->districtFields as $attribute => $label) {
if($precinct->$attribute) {
$transformed[] = array(
'name' => $label,
'value' => (string) $precinct->$attribute
);
}
}
return $transformed;
}
}
<?php
namespace Acme\Support\Transformers;
abstract class AbstractTransformer implements Transformer
{
/**
* Transforms a single item
* @param mixed $item The item to transform
* @return mixed $item The transformed item
*/
public abstract function transform($item);
public function transformCollection($collection)
{
$transformed = array();
foreach($collection as $item) {
$transformed[] = $this->transform($item);
}
return $transformed;
}
/**
* Iterates through an array and converts all null fields to empty strings
* @param array $values
* @return array
*/
protected function convertNullToEmptyString(array $values)
{
foreach($values as $key => $value) {
if($values[$key] === null) {
$values[$key] = '';
}
}
return $values;
}
/**
* Converts a generic US numeric phone number into a properly-formatted one
* Assumes extensions are formatted (primary phone number)x####
* @param string $phone The original phone number
*/
protected function transformPhoneNumber($phone)
{
$phone = preg_replace('@[^\dx]@', '', $phone);
if(0 == (int) $phone || 'NULL' == $phone) {
$converted = null;
} else {
$converted = array();
$converted[] = '(';
$converted[] = substr($phone, 0, 3);
$converted[] = ') ';
$converted[] = substr($phone, 3, 3);
$converted[] = '-';
$converted[] = substr($phone, 6);
$converted = implode('', $converted);
}
return $converted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment