Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Created November 14, 2017 05:47
Show Gist options
  • Select an option

  • Save im4aLL/6f1e192cab57761c76d6be2c2588e152 to your computer and use it in GitHub Desktop.

Select an option

Save im4aLL/6f1e192cab57761c76d6be2c2588e152 to your computer and use it in GitHub Desktop.
Design pattern - template method
<?php
abstract class Transformer {
protected function responseJson($array)
{
return json_encode($array);
}
abstract public function transform($item);
}
class UserTransformer extends Transformer {
public function transform($item)
{
return $this->responseJson(['name' => $item['name']]);
}
}
class ProductTransformer extends transformer {
public function transform($item)
{
return $this->responseJson(['name' => $item['product_name']]);
}
}
$userTransformer = new UserTransformer();
print_r($userTransformer->transform(['id' => 1, 'name' => 'im4aLL']));
$productTransformer = new ProductTransformer();
print_r($productTransformer->transform(['id' => 1, 'product_name' => 'Sample product']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment