commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / vendor / civicrm / civicrm-cxn-rpc / src / Message.php
1 <?php
2
3 /*
4 * This file is part of the civicrm-cxn-rpc package.
5 *
6 * Copyright (c) CiviCRM LLC <info@civicrm.org>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this package.
10 */
11
12 namespace Civi\Cxn\Rpc;
13
14 abstract class Message {
15
16 protected $code = 200;
17 protected $headers = array();
18 protected $data;
19
20 public function __construct($data) {
21 $this->data = $data;
22 }
23
24 /**
25 * @return string
26 * Encoded message.
27 */
28 abstract public function encode();
29
30 /**
31 * @return int
32 */
33 public function getCode() {
34 return $this->code;
35 }
36
37 /**
38 * @param int $code
39 * @return static
40 */
41 public function setCode($code) {
42 $this->code = $code;
43 return $this;
44 }
45
46 /**
47 * @return mixed
48 */
49 public function getData() {
50 return $this->data;
51 }
52
53 /**
54 * @param mixed $data
55 * @return static
56 */
57 public function setData($data) {
58 $this->data = $data;
59 return $this;
60 }
61
62 /**
63 * @return array
64 */
65 public function getHeaders() {
66 return $this->headers;
67 }
68
69 /**
70 * @param array $headers
71 * @return static
72 */
73 public function setHeaders($headers) {
74 $this->headers = $headers;
75 return $this;
76 }
77
78 /**
79 * Extract the necessary parts to return this
80 * message as an HTTP response.
81 *
82 * @return array
83 * array($headers, $blob, $code)
84 */
85 public function toHttp() {
86 return array($this->headers, $this->encode(), $this->code);
87 }
88
89 /**
90 * Send this message immediately.
91 */
92 public function send() {
93 list ($headers, $blob, $code) = $this->toHttp();
94 header('Content-Type: ' . Constants::MIME_TYPE);
95 header("X-PHP-Response-Code: $code", TRUE, $code);
96 foreach ($headers as $n => $v) {
97 header("$n: $v");
98 }
99 echo $blob;
100 }
101
102 /**
103 * Convert this message a Symfony "Response" object.
104 *
105 * @return \Symfony\Component\HttpFoundation\Response
106 */
107 public function toSymfonyResponse() {
108 $headers = array_merge(
109 array('Content-Type' => Constants::MIME_TYPE),
110 $this->getHeaders()
111 );
112 return new \Symfony\Component\HttpFoundation\Response(
113 $this->encode(),
114 $this->code,
115 $headers
116 );
117 }
118
119 }