Created
October 26, 2018 08:19
-
-
Save teknosains/e4e868578f0e3740487bf8f2f08cccab to your computer and use it in GitHub Desktop.
Codeigniter Response Library
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 if (!defined('BASEPATH')) { exit('No direct script access allowed'); } | |
| /** | |
| * Response utility | |
| * | |
| * @author budy | |
| */ | |
| class Response | |
| { | |
| private $obj = null; | |
| public function __construct() | |
| { | |
| $this->obj = & get_instance(); | |
| } | |
| /** | |
| * This method will set numerical value to float/int | |
| * while keeping the zeroleading numerical string | |
| * Example: 09178181 will stay so, | |
| * This is a replacement for json constant JSON_NUMERIC_CHECK | |
| * that remove leading zero on a numeric string | |
| * @param array|object $data | |
| * @return array | |
| */ | |
| public function keepZeroLeading($data) | |
| { | |
| // make sure if its an object, convert it to Array first | |
| $data = json_decode(json_encode((object) $data), true); | |
| try { | |
| array_walk_recursive($data, function (&$value, $key) { | |
| if (is_string($value) && is_numeric($value)) { | |
| // check if value starts with 0 or + or just 0 | |
| // example: 0819, +62871 etc | |
| if (preg_match('/^(\+|0)/', $value)) { | |
| //if its not a float/decimal | |
| if (strpos($value, ".") === false) { | |
| if ($value === '0') { | |
| $value = (float)$value; | |
| } else { | |
| $value = (string)$value; | |
| } | |
| } else { | |
| $value = (float)$value; | |
| } | |
| } else { | |
| //if has letters | |
| if (preg_match("/[a-z]/i", $value)) { | |
| $value = (string)$value; | |
| } else { | |
| //if . found, its a float | |
| if (strpos($value, ".") === false) { | |
| $value = (int)$value; | |
| } else { | |
| $value = (float)$value; | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| } catch (Exception $e) { | |
| //.. | |
| } | |
| return $data; | |
| } | |
| /** | |
| * @param array $data | |
| * @param $zeroleading, | |
| * @return output | |
| */ | |
| public function json($data, $zeroleading = true) | |
| { | |
| if ($zeroleading) { | |
| $data = $this->keepZeroLeading($data); | |
| } | |
| // its encouraged we upgrade the PHP version to 7+ | |
| // or atleast version 5.6.6 | |
| if (version_compare(PHP_VERSION, '5.6.6') >= 0) { | |
| $output = json_encode($data, JSON_PRESERVE_ZERO_FRACTION); | |
| } else { | |
| $output = json_encode($data); | |
| } | |
| return $this->obj->output | |
| ->set_content_type('application/json', 'utf-8') | |
| ->set_status_header(200) | |
| ->set_output($output); | |
| } | |
| /** | |
| * Forbidden response | |
| */ | |
| public function forbidden() | |
| { | |
| $output = json_encode([ | |
| 'status' => false, | |
| 'message' => 'Not authorized', | |
| 'error' => 'You are not authorized' | |
| ]); | |
| return $this->obj->output | |
| ->set_content_type('application/json', 'utf-8') | |
| ->set_status_header(200) | |
| ->set_output($output); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment