Set Response Format into CI_Output

@param string Response format
505992
public function setFormat($format)
    {
        $this->_format = $format;
        // Use formatter content type if exists
        if (isset($this->contentTypes[$this->_format])) {
            $this->ci->output
                ->set_content_type($this->contentTypes[$this->_format]);
        }

        return $this;
    }
453224
public function setApiQueryStringRequestFormat($format)
    {
        $this->apiQueryStringRequestFormat = $format;
        return $this->apiQueryStringRequestFormat === $format ? true : false;
    }
929780
public function setFormat($format)
    {
        $this->format = $format;
        if ($this->format === self::FORMAT_UNIX) {
            $this->newline = "\n";
        } elseif ($this->format === self::FORMAT_DOS) {
            $this->newline = "\r\n";
        }
    }
424559
protected function setOutput()
    {
        // Send the output.
        $this->registry->response->setOutput($this->registry->load->view($this->getLocation() . '_form', $this->data));
    }
730550
protected function getOutputResponse(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) : ResponseInterface {
        ob_start();
        switch ($this->determineOutputType()) {
            case self::TYPE_JSON:
                $this->renderJSON($request, $response);
                break;
            case self::TYPE_XML:
                $this->renderXML($request, $response);
                break;
            case self::TYPE_PLAIN:
                $this->renderPlainText($request, $response);
                break;
            default:
                $this->renderHTML($request, $response);
                break;
        }

        $output = ob_get_clean();
        $body = new RequestBody();
        // write handler
        $body->write($output);
        return $response->withStatus(404)->withBody($body);
    }
1293586
private function format_response($response, $context, $responseformat, $answerid, $options) {

        $convertstudentresponse = file_rewrite_pluginfile_urls($response, 'pluginfile.php',
            $context->id, 'mod_lesson', 'page_responses', $answerid);

        return format_text($convertstudentresponse, $responseformat, $options);
    }
724618
public function execute($response) {
        global $CFG, $DB;

        $params = $this->parse_template();
        $contextid = $params['context_id'];
        $itemid = $params['item_id'];
        if ($response->get_request_method() === 'GET') {
            $contenttype = $response->get_accept();
        } else {
            $contenttype = $response->get_content_type();
        }
        // We will receive typeid when working with LTI 1.x, if not then we are in LTI 2.
        $typeid = optional_param('type_id', null, PARAM_ALPHANUM);
        if (is_null($typeid)) {
            if (!$this->check_tool_proxy(null, $response->get_request_data())) {
                $response->set_code(403);
                $response->set_reason("Invalid tool proxy specified.");
                return;
            }
        } else {
            switch ($response->get_request_method()) {
                case self::HTTP_GET:
                    if (!$this->check_type($typeid, $contextid, 'LineItem.item:get', $response->get_request_data())) {
                        $response->set_code(403);
                        $response->set_reason("This resource does not support GET requests.");
                        return;
                    }
                    break;
                case self::HTTP_PUT:
                    if (!$this->check_type($typeid, $contextid, 'LineItem.item:put', $response->get_request_data())) {
                        $response->set_code(403);
                        $response->set_reason("This resource does not support PUT requests.");
                        return;
                    }
                    break;
                case self::HTTP_DELETE:
                    if (!$this->check_type($typeid, $contextid, 'LineItem.item:delete', $response->get_request_data())) {
                        $response->set_code(403);
                        $response->set_reason("This resource does not support DELETE requests.");
                        return;
                    }
                    break;
                default:  // Should not be possible.
                    $response->set_code(405);
                    return;
            }
        }
        if (empty($contextid) || (!empty($contenttype) && !in_array($contenttype, $this->formats))) {
            $response->set_code(400);
            $response->set_reason("Invalid request made.");
            return;
        }
        if (!$DB->record_exists('course', array('id' => $contextid))) {
            $response->set_code(404);
            $response->set_reason("Not Found: Course $contextid doesn't exist.");
            return;
        }
        if (!$DB->record_exists('grade_items', array('id' => $itemid))) {
            $response->set_code(404);
            $response->set_reason("Not Found: Grade item $itemid doesn't exist.");
            return;
        }
        $item = $this->get_service()->get_lineitem($contextid, $itemid, $typeid);
        if ($item === false) {
            $response->set_code(403);
            $response->set_reason("Line item does not exist.");
            return;
        }
        require_once($CFG->libdir.'/gradelib.php');
        switch ($response->get_request_method()) {
            case 'GET':
                $this->get_request($response, $item, $typeid);
                break;
            case 'PUT':
                $json = $this->process_put_request($response->get_request_data(), $item, $typeid);
                $response->set_body($json);
                $response->set_code(200);
                break;
            case 'DELETE':
                $this->process_delete_request($item);
                $response->set_code(204);
                break;
            default:  // Should not be possible.
                $response->set_code(405);
                $response->set_reason("Invalid request method specified.");
                return;
        }
    }
352491
public function finalize($output=null)
	{
	    $response = $this->getResponse();
	    if ($output instanceof ResponseInterface) {
	        $response = $output;
	    } elseif (is_string($output) && !empty($output) && $response->getBody()->isWritable()) {
	        if (Config::getCommon('response.outputBuffering') === 'prepend') {
	            // prepend output buffer content
	            $body = new Body(fopen('php://temp', 'r+'));
	            $body->write($output . $response->getBody());
	            $response = $response->withBody($body);
	        } else {
	            // append output buffer content
	            $response->getBody()->write($output);
	        }
	    }
	    
	    // stop PHP sending a Content-Type automatically
	    ini_set('default_mimetype', '');
	    
	    if ($this->isEmptyResponse($response)) {
	        return $response->withoutHeader('Content-Type')->withoutHeader('Content-Length');
	    }
	    
	    if (!$response->hasHeader('Content-Type')) {
	        $response = $response->withHeader('Content-Type', 'text/html; charset=UTF-8');
	    }
	    
	    // Add Content-Length header if `addContentLengthHeader` is set
	    if (Config::getCommon('response.addContentLengthHeader')) {
	        if (ob_get_length() > 0) {
	            throw new \RuntimeException("Unexpected data in output buffer. " .
	                "Maybe you have characters before an opening <?php tag?");
	        }
	        
	        $size = $response->getBody()->getSize();
	        if ($size !== null && !$response->hasHeader('Content-Length')) {
	            $response = $response->withHeader('Content-Length', (string) $size);
	        }
	    }
	    
	    return $response;
	}
1333320
private function _buildResponse($response)
    {
        $sections = explode("\r\n\r\n", $response, 2);
        $body = isset($sections[1]) ? $sections[1] : NULL;
        $headers = array();
        $headerlines = explode("\n", $sections[0]);

        // process status
        list($http, $code, $message) = explode(' ', $headerlines[0], 3);

        // process headers
        foreach (array_slice($headerlines, 1) as $headerline) {
            $headers[] = HeaderField::fromString($headerline);
        }

        $response = new Response($code, $headers, $body);

        return $response;
    }
1295162
public function setFormat(string $format)
    {
        $allowed = [
            'html',
            'xml',
            'json',
            'file'
        ];
        
        if (! in_array(strtolower($format), $allowed)) {
            Throw new RouterException(sprintf('Your format "%s" is not an allowed format. Use one of these formats %s', $format, implode(', ', $allowed)));
        }
        
        $this->format = $format;
    }
344870
protected function setVersionHeader(string $output, Response $response)
    {
        $crawler = $this->getCrawler($this->createResponseWithLowerCaseContent($output));
        $node    = $crawler->filter('head > meta[http-equiv="x-pjax-version"]');

        if ($node->count()) {
            $response->getHeaders()->set('x-pjax-version', $node->attr('content'));
        }

        return $this;
    }
1032891
public function setFormat($format)
    {
        $this->request->setFormat($format);
        $this->baseRenderingContext->getTemplatePaths()->setFormat($format);
    }
875518
private function wrapResponse(Response $response, $result = null, string $output = ''): Response
    {
        if ($result instanceof Response) {
            if (!empty($output) && $result->getBody()->isWritable()) {
                $result->getBody()->write($output);
            }

            return $result;
        }

        if (is_array($result) || $result instanceof \JsonSerializable) {
            $response = $this->writeJson($response, $result);
        } else {
            $response->getBody()->write($result);
        }

        //Always glue buffered output
        $response->getBody()->write($output);

        return $response;
    }
1028350
public function setOutputFormat($outputFormat)
    {
        $allowedFormats = array(
            'xml/rdf',
            'text/simple',
            'text/microformats',
            'application/json',
            'text/n3'
        );

        if (!in_array(strtolower($outputFormat), $allowedFormats)) {
            throw new \InvalidArgumentException(
                "Illegal output format, allowed output formats are " . implode(', ', $allowedFormats)
            );
        }
        $this->outputFormat = $outputFormat;

        return $this;
    }
1230521
private function formatResponse($response)
    {
        $this->last_response = $response;

        if (!empty($response['body'])) {
            return json_decode($response['body'], true);
        }

        return false;
    }
983726
private function parseResponse($response) {
        $response = trim($response);
        $this->responseStatus = (int) substr($response, 0, 3);
        $this->response = substr($response, 4);
    }
778714
public function format($response)
    {
        $response->getHeaders()->set('Content-Type', 'text');
        //$response->setDownloadHeaders('error.pdf', 'application/pdf');
        //$response->setStatusCode(200);
        if ($response->data === null) {
            return;
        }

        echo '<pre>';
        var_export($response->data);

        /*
        $renderer = new \mPDF(
            'pl-x', // mode
            'A4', // format
            0, // font-size
            '', // font
            12, // margin-left
            12, // margin-right
            5, // margin-top
            5, // margin-bottom
            2, // margin-header
            2, // margin-footer
            'P' // orientation
        );
        $renderer->useSubstitutions = true;
        $renderer->simpleTables = false;
        if ($response->data !== null) {
            @$renderer->WriteHTML(\yii\helpers\Html::encode(var_export($response->data, true)));
        }
        $response->content = $renderer->Output('print', 'S');
        */
    }