input = $input; } /** * Output the data to the client */ public function __destruct() { // Output the headers $this->_output_headers(); // Echo the response echo $this->type_wrapper; } /** * Add a header to be output * * @param mixed $header * @return void */ public function set_header($header) { if (is_array($header)) { array_merge($this->headers, $header); } else { $this->headers[] = $header; } } /** * Set the data to be output to the endpoint * * @param string $type - The datatype to send * @param mixed $data * @return void */ public function set_data($type = 'json', $data) { // Get the appropriate output format for the client // And set the data $this->get_accepted_type($type, $data); } // -------------------------------------------------------------------------- // ! Private helper methods // -------------------------------------------------------------------------- /** * Get the type more accepted for output * * @param mixed $types * @param mixed $data * @return void */ protected function get_accepted_type($types, $data) { $types = (array) $types; $types = array_map('strtoupper', $types); $headers = $this->input->header_array(); $accept = array_flip($headers['accept']); $type_map = []; $accepted = []; $classes = []; foreach($types as $t) { $type_class = "Sleepy\\Type\\{$t}"; $classes[$type_class] = new $type_class($data); $mime = $classes[$type_class]->get_mime(); $type_map[$mime] = $type_class; } foreach($accept as $type => $q) { if (array_key_exists($type, $type_map)) { $accepted[$q] = $type; } } krsort($accepted); $class = $type_map[current($accepted)]; $this->type_wrapper = $classes[$class]; // Make sure to set the content-type header if (empty($this->headers)) { $mime = $this->type_wrapper->get_mime(); $this->set_header("Content-type: {$mime}"); } } /** * Set the applicable response headers * * @return void */ protected function _output_headers() { foreach($this->headers as $name => $val) { if (is_numeric($name)) { $output_header = $val; } else { $output_header = implode(": ", [$name, $val]); } @header($output_header); } } } // End of core/Output.php