-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceResponse.php
119 lines (90 loc) · 2.62 KB
/
ServiceResponse.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace AC\WebServicesBundle;
use JMS\Serializer\Context;
/**
* Note that this does not extend the HttpFoundation Response for a reason, so there is some duplicate functionality. This Response type allows
* the bundle to implement custom response logic for all api requests - primarily for the purposes of serializing response data.
*
* @package ACWebServicesBundle
* @author Evan Villemez
*/
class ServiceResponse
{
protected $statusCode;
protected $responseData;
protected $responseHeaders;
protected $templates = array();
protected $templateKey = null;
protected $serializationContext;
public function __construct($data, $code = 200, $headers = array(), Context $serializationContext = null)
{
$this->responseData = $data;
$this->statusCode = $code;
$this->responseHeaders = $headers;
$this->serializationContext = $serializationContext;
}
public static function create($data, $code = 200, $headers = array(), Context $serializationContext = null)
{
return new static($data, $code, $headers, $serializationContext);
}
public function getResponseData()
{
return $this->responseData;
}
public function setResponseData($data)
{
$this->responseData = $data;
return $this;
}
public function getResponseCode()
{
return $this->statusCode;
}
public function getResponseHeaders()
{
return $this->responseHeaders;
}
public function setResponseHeaders(array $array)
{
$this->responseHeaders = $array;
return $this;
}
public function setResponseHeader($key, $val)
{
$this->responseHeaders[$key] = $val;
return $this;
}
public function setSerializationContext(Context $ctx)
{
$this->serializationContext = $ctx;
return $this;
}
public function getSerializationContext()
{
return $this->serializationContext;
}
public function getTemplateForFormat($format)
{
return isset($this->templates[$format]) ? $this->templates[$format] : false;
}
public function setTemplateForFormat($template, $formats)
{
foreach ((array) $formats as $format) {
$this->templates[$format] = $template;
}
return $this;
}
public function hasTemplateForFormat($format)
{
return isset($this->templates[$format]);
}
public function setTemplateKey($key)
{
$this->templateKey = $key;
return $this;
}
public function getTemplateKey()
{
return $this->templateKey;
}
}