Last active
December 24, 2015 13:49
-
-
Save Maqsim/6808512 to your computer and use it in GitHub Desktop.
Prettify respone from DB via filters
This file contains 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 | |
public function _prettify_order_response($order) | |
{ | |
// Load dependencies | |
$subjects = $this->subjects; | |
// Init field->filter array | |
$nice_response = array( | |
'id' => 'task_id', | |
'info' => 'task_info', | |
'deadline' => function ($deadline) { | |
$_deadline = new DateTime($deadline); | |
return array( | |
'str' => $deadline, | |
'ts' => $_deadline->getTimestamp() | |
); | |
}, | |
'placement_date' => function ($placement_date) { | |
$_placement_date = new DateTime($placement_date); | |
return array( | |
'str' => $placement_date, | |
'ts' => $_placement_date->getTimestamp() | |
); | |
}, | |
'price' => null, | |
'status' => null, | |
'gnt' => null, | |
'refund' => null, | |
'order_title' => null, | |
'category' => array( | |
'args' => 'cat_id', | |
'func' => function ($cat_id) use ($subjects) { | |
return array( | |
'id' => $cat_id, | |
'subject' => array( | |
'id' => $subjects->get($cat_id, array('parent_id')), | |
) | |
); | |
} | |
), | |
'level' => array( | |
'args' => 'level_id', | |
'func' => function ($level_id) { | |
return array( | |
'id' => $level_id | |
); | |
} | |
), | |
'process_status' => function ($process_status) { | |
return array( | |
'id' => $process_status | |
); | |
} | |
); | |
return prettify($nice_response, $order); | |
} |
This file contains 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 | |
/** | |
* Generate nice response for RESTful API | |
* Magic start here | |
* | |
* Filters: | |
* 1. string - simple replacement | |
* 2. lambda - call a function with $field as argument | |
* 3. array - call a function with $filter['args'] as arguments | |
* 4. NULL - nothing to change | |
* | |
* @param $expected_response | |
* @param $original_response | |
* @param bool $assoc | |
* @return array | |
*/ | |
function prettify($expected_response, $original_response, $assoc = false) | |
{ | |
return array_combine(array_keys($expected_response), array_map(function ($filter, $field) use ($original_response) { | |
switch (gettype($filter)) { | |
case 'string' : | |
return $original_response->$filter; | |
break; | |
case 'object': | |
return $filter($original_response->$field); | |
break; | |
case 'array': | |
return call_user_func_array($filter['func'], array_map(function ($argument) use ($original_response) { | |
$argument = trim($argument); | |
return $original_response->$argument; | |
}, explode(',', $filter['args']))); | |
break; | |
case 'NULL': | |
default: | |
return $original_response->$field; | |
} | |
}, $expected_response, array_keys($expected_response))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment