Skip to content

Commit 5e7e143

Browse files
author
Christoph Singer
committed
Merge pull request #6 from shadypierre/master
Return an array instead of a StdClass object
2 parents f259241 + dbce756 commit 5e7e143

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

Controller/JsonRpcController.php

+28-14
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,30 @@ public function __construct($container, $config)
7171
public function execute(Request $httprequest)
7272
{
7373
$json = $httprequest->getContent();
74-
$request = json_decode($json);
74+
$request = json_decode($json, true);
75+
7576
if ($request === null) {
7677
return $this->getErrorResponse(self::PARSE_ERROR, null);
77-
} elseif (!(isset($request->jsonrpc) && isset($request->method) && $request->jsonrpc == '2.0')) {
78-
return $this->getErrorResponse(self::INVALID_REQUEST, $request->id);
78+
} elseif (!(isset($request['jsonrpc']) && isset($request['method']) && $request['jsonrpc'] == '2.0')) {
79+
return $this->getErrorResponse(self::INVALID_REQUEST, $request['id']);
7980
}
80-
if (!in_array($request->method, array_keys($this->config['functions']))) {
81-
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id);
81+
82+
if (!in_array($request['method'], array_keys($this->config['functions']))) {
83+
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']);
8284
}
83-
$service = $this->container->get($this->config['functions'][$request->method]['service']);
84-
$method = $this->config['functions'][$request->method]['method'];
85-
$params = (isset($request->params) ? $request->params : array());
85+
86+
$service = $this->container->get($this->config['functions'][$request['method']]['service']);
87+
$method = $this->config['functions'][$request['method']]['method'];
88+
$params = (isset($request['params']) ? $request['params'] : array());
89+
8690
if (is_callable(array($service, $method))) {
8791
$r = new \ReflectionMethod($service, $method);
92+
8893
if (is_array($params)) {
8994
if (!(count($params) >= $r->getNumberOfRequiredParameters()
9095
&& count($params) <= $r->getNumberOfParameters())
9196
) {
92-
return $this->getErrorResponse(self::INVALID_PARAMS, $request->id,
97+
return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'],
9398
sprintf('Number of given parameters (%d) does not match the number of expected parameters (%d required, %d total)',
9499
count($params), $r->getNumberOfRequiredParameters(), $r->getNumberOfParameters()));
95100
}
@@ -100,7 +105,7 @@ public function execute(Request $httprequest)
100105
/* @var \ReflectionParameter $rp */
101106
$name = $rp->name;
102107
if (!isset($params->$name) && !$rp->isOptional()) {
103-
return $this->getErrorResponse(self::INVALID_PARAMS, $request->id,
108+
return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'],
104109
sprintf('Parameter %s is missing', $name));
105110
}
106111
if (isset($params->$name)) {
@@ -111,23 +116,26 @@ public function execute(Request $httprequest)
111116
}
112117
$params = $newparams;
113118
}
119+
114120
try {
115121
$result = call_user_func_array(array($service, $method), $params);
116122
} catch (\Exception $e) {
117-
return $this->getErrorResponse(self::INTERNAL_ERROR, $request->id, $e->getMessage());
123+
return $this->getErrorResponse(self::INTERNAL_ERROR, $request['id'], $e->getMessage());
118124
}
125+
119126
$response = array('jsonrpc' => '2.0');
120127
$response['result'] = $result;
121-
$response['id'] = (isset($request->id) ? $request->id : null);
128+
$response['id'] = (isset($request['id']) ? $request['id'] : null);
122129

123130
if ($this->container->has('jms_serializer')) {
124131
$response = $this->container->get('jms_serializer')->serialize($response, 'json', $this->serializationContext);
125132
} else {
126133
$response = json_encode($response);
127134
}
135+
128136
return new Response($response, 200, array('Content-Type' => 'application/json'));
129137
} else {
130-
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id);
138+
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']);
131139
}
132140
}
133141

@@ -151,15 +159,21 @@ protected function getError($code)
151159
$message = 'Internal error';
152160
break;
153161
}
162+
154163
return array('code' => $code, 'message' => $message);
155164
}
156165

157166
protected function getErrorResponse($code, $id, $data = null)
158167
{
159168
$response = array('jsonrpc' => '2.0');
160169
$response['error'] = $this->getError($code);
161-
if ($data != null) $response['error']['data'] = $data;
170+
171+
if ($data != null) {
172+
$response['error']['data'] = $data;
173+
}
174+
162175
$response['id'] = $id;
176+
163177
return new Response(json_encode($response), 200, array('Content-Type' => 'application/json'));
164178
}
165179

0 commit comments

Comments
 (0)