Last active
August 29, 2015 14:00
-
-
Save thepsion5/11008147 to your computer and use it in GitHub Desktop.
Simple example of a transformer class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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