* @copyright Copyright (c) 2022, Robert Strutts. * @license https://mit-license.org/ */ namespace tts; final class json { public static function do_json_headers(int $status_code = 200) { if (!headers_sent()) { header($_SERVER['SERVER_PROTOCOL'] . " " . $status_code); header('Content-Type: application/json; charset=utf-8', true, intval($status_code)); } } public static function output(array $data, string $misc = '') { $number_of_rows = \bs_tts\common::get_count($data); self::do_json_headers(); $dt = new \DateTime(); $ds = $dt->format('Y-m-d h:i:s A'); echo "{ \r\n \"records_total\": {$number_of_rows}, \r\n"; echo "\"date_stamp\": \"{$ds}\", \r\n"; if (! empty($misc) ) { echo $misc; } if ($number_of_rows > 0) { echo '"data":['; echo json_encode($data); echo ']}'; // end JSON object } else { echo '"data":[]}'; // No users!!! } } }