Created
November 14, 2017 05:47
-
-
Save im4aLL/6f1e192cab57761c76d6be2c2588e152 to your computer and use it in GitHub Desktop.
Design pattern - template method
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 | |
| 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