1: <?php
2:
3:
4: namespace Racoon\Api\Response\Generate;
5:
6:
7: use Racoon\Api\Request;
8:
9: class DetailedResponse implements GeneratorInterface
10: {
11:
12: 13: 14:
15: protected $request;
16:
17: 18: 19:
20: protected $httpResponseCode;
21:
22: 23: 24:
25: protected $showSchema;
26:
27: 28: 29:
30: protected $showTimeElapsed;
31:
32: 33: 34:
35: protected $showReceived;
36:
37: public function __construct()
38: {
39: $this->setShowSchema(true);
40: $this->setShowTimeElapsed(true);
41: $this->setShowReceived(true);
42: }
43:
44:
45: 46: 47: 48:
49: public function setRequest(Request $request)
50: {
51: $this->request = $request;
52: }
53:
54:
55: 56: 57:
58: public function getRequest()
59: {
60: return $this->request;
61: }
62:
63: 64: 65:
66: public function generate()
67: {
68: $this->httpResponseCode = 200;
69:
70: $displayException = $this->getRequest()->getDisplayException();
71:
72: $response = new \stdClass();
73: $response->success = (! is_object($displayException));
74: $response->message = $this->getRequest()->getResponseMessage();
75: if (is_object($displayException)) {
76: $response->message = $displayException->getMessage();
77: $this->httpResponseCode = $displayException->getCode();
78: if ($this->httpResponseCode == 0) {
79: $this->httpResponseCode = 500;
80: }
81: }
82: if ($this->shouldShowSchema()) {
83: if (is_object($this->getRequest()->getSchema())) {
84: $response->schema = $this->getRequest()->getSchema()->getDefinition();
85: } else {
86: $response->schema = null;
87: }
88: }
89: if ($this->shouldShowReceived()) {
90: $response->received = $this->request->getFullRequestData();
91: }
92: if ($this->shouldShowTimeElapsed()) {
93: $response->time_elapsed = number_format($this->request->getElapsedTime(true), 3);
94: }
95: $response->response = $this->getRequest()->getControllerResponse();
96:
97: return $response;
98: }
99:
100:
101: 102: 103:
104: public function getHttpResponseCode()
105: {
106: return $this->httpResponseCode;
107: }
108:
109:
110: 111: 112:
113: public function shouldShowSchema()
114: {
115: return $this->showSchema;
116: }
117:
118:
119: 120: 121:
122: public function setShowSchema($showSchema)
123: {
124: $this->showSchema = $showSchema;
125: }
126:
127:
128: 129: 130:
131: public function shouldShowTimeElapsed()
132: {
133: return $this->showTimeElapsed;
134: }
135:
136:
137: 138: 139:
140: public function setShowTimeElapsed($showTimeElapsed)
141: {
142: $this->showTimeElapsed = $showTimeElapsed;
143: }
144:
145:
146: 147: 148:
149: public function shouldShowReceived()
150: {
151: return $this->showReceived;
152: }
153:
154:
155: 156: 157:
158: public function setShowReceived($showReceived)
159: {
160: $this->showReceived = $showReceived;
161: }
162: }