summaryrefslogtreecommitdiffstats
path: root/system/classes/response.php
blob: a4b737e00f8d42eecf214e2c444ebf37aa0cc528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

/**
 * Handles the response that is sent back to the client.
 * @package Core
 */
class Response
{

	/**
	 * Headers for the response
	 * @var array
	 * @access public
	 */
	public $headers = array(
		'Content-Type: text/html; charset=utf-8'
	);

	/**
	 * Response body
	 * @var string
	 * @access public
	 */
	public $body;

	/**
	 * Add header to the response
	 *
	 * @param string $header Header content
	 * @return void
	 * @access public
	 */
	public function add_header($header)
	{
		$this->headers[] = $header;
	}

	/**
	 * Add redirection header
	 *
	 * @param string $url URL to redirect the client to
	 * @return void
	 * @access public
	 */
	public function redirect($url)
	{
		$this->add_header("Location: $url");
	}

	/**
	 * Sends headers to the client
	 *
	 * @return Response Same Response object, for method chaining
	 * @access public
	 */
	public function send_headers()
	{
		foreach ($this->headers as $header)
			header($header);
		return $this;
	}

	/**
	 * Send response body to the client
	 *
	 * @return object Same Response object, for method chaining
	 * @access public
	 */
	public function send_body()
	{
		echo $this->body;
		return $this;
	}

}