Skip to content

Commit f6bb7f2

Browse files
committed
Injected new feature about how request params are handled - as an array or as an object.
1 parent fb487a2 commit f6bb7f2

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

Action.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ protected function restoreYiiRequest()
8383
return $this;
8484
}
8585

86+
/**
87+
* Recursively converts object to an associative array.
88+
* @param $obj
89+
* @return array
90+
*/
91+
protected function objToAssoc($obj) {
92+
$result = (array) $obj;
93+
94+
if (is_array($result)) {
95+
foreach ($result as $key => $value) {
96+
if (is_object($value) || is_array($value)) {
97+
$result[$key] = $this->objToAssoc($value);
98+
}
99+
}
100+
}
101+
102+
return $result;
103+
}
104+
86105
/**
87106
* @inheritdoc
88107
*/
@@ -97,7 +116,7 @@ public function runWithParams($params)
97116
if (is_array($batchRequestData)) {
98117
$isBatch = true;
99118
} else {
100-
// for simple processing
119+
// For simple processing
101120
$batchRequestData = [$batchRequestData];
102121
}
103122

@@ -106,32 +125,43 @@ public function runWithParams($params)
106125
try {
107126
$request = new JsonRpcRequest();
108127
$request->paramsPassMethod = $this->paramsPassMethod;
109-
$request->load(ArrayHelper::toArray($requestData), '');
110-
// $batchResponse[] = $request->params;
111-
// continue;
128+
129+
// Handling params
130+
$params = ($this->requestParseAsArray)
131+
? (isset($request->params) ? $this->objToAssoc($request->params) : [])
132+
: (isset($request->params) ? $request->params : new \stdClass());
133+
134+
$requestData = ArrayHelper::toArray($requestData);
135+
$requestData['params'] = $params;
136+
137+
$request->load($requestData, '');
112138
if ($request->validate()) {
113139
$result = $request->execute();
114140
if (!is_null($request->id)) {
115141
$batchResponse[] = new SuccessResponse($request, $result);
116142
}
117-
} else {
143+
}
144+
else {
118145
foreach ($request->getFirstErrors() as $attribute => $error) {
119146
$request->$attribute = null;
120147
}
121148
throw new InvalidRequestException();
122149
}
123-
} catch (InvalidRequestException $e) {
150+
}
151+
catch (InvalidRequestException $e) {
124152
$batchResponse[] = new ErrorResponse($e, $request ?: null);
125-
} catch (JsonRpcException $e) {
126-
// we do not return response to notifications!
153+
}
154+
catch (JsonRpcException $e) {
155+
// We do not return response to notifications
127156
if ($request && !is_null($request->id)) {
128157
$batchResponse[] = new ErrorResponse($e, $request ?: null);
129158
}
130159
}
131160

132161
$this->restoreYiiRequest();
133162
}
134-
} catch (JsonRpcException $e) {
163+
}
164+
catch (JsonRpcException $e) {
135165
return new ErrorResponse($e);
136166
}
137167

0 commit comments

Comments
 (0)