Skip to content

Commit 306f454

Browse files
authored
Merge pull request #5 from xzag/feature-refactor
parse params as object feature
2 parents f6bb7f2 + c4bd77c commit 306f454

10 files changed

+384
-600
lines changed

Action.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,6 @@ 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-
10586
/**
10687
* @inheritdoc
10788
*/
@@ -125,33 +106,23 @@ public function runWithParams($params)
125106
try {
126107
$request = new JsonRpcRequest();
127108
$request->paramsPassMethod = $this->paramsPassMethod;
109+
$request->parseAsArray = $this->requestParseAsArray;
128110

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, '');
111+
$request->load(ArrayHelper::toArray($requestData), '');
138112
if ($request->validate()) {
139113
$result = $request->execute();
140114
if (!is_null($request->id)) {
141115
$batchResponse[] = new SuccessResponse($request, $result);
142116
}
143-
}
144-
else {
117+
} else {
145118
foreach ($request->getFirstErrors() as $attribute => $error) {
146119
$request->$attribute = null;
147120
}
148121
throw new InvalidRequestException();
149122
}
150-
}
151-
catch (InvalidRequestException $e) {
123+
} catch (InvalidRequestException $e) {
152124
$batchResponse[] = new ErrorResponse($e, $request ?: null);
153-
}
154-
catch (JsonRpcException $e) {
125+
} catch (JsonRpcException $e) {
155126
// We do not return response to notifications
156127
if ($request && !is_null($request->id)) {
157128
$batchResponse[] = new ErrorResponse($e, $request ?: null);
@@ -160,8 +131,7 @@ public function runWithParams($params)
160131

161132
$this->restoreYiiRequest();
162133
}
163-
}
164-
catch (JsonRpcException $e) {
134+
} catch (JsonRpcException $e) {
165135
return new ErrorResponse($e);
166136
}
167137

Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ class Controller extends \yii\web\Controller
2424
public $paramsPassMethod = self::JSON_RPC_PARAMS_PASS_FUNCARGS;
2525

2626
/**
27-
* @var array Whether JSON parse should parse objects in `params` as associate arrays or objects
27+
* @var array Whether JSON parse should parse objects in `params` as associative arrays or objects
2828
*/
2929
public $requestParseAsArray = true;
30-
30+
3131
public function actions()
3232
{
3333
return [
3434
'index' => [
3535
'class' => Action::class,
3636
'paramsPassMethod' => $this->paramsPassMethod,
37-
'requestParseAsArray' => $this->requestParseAsArray,
37+
'requestParseAsArray' => $this->requestParseAsArray
3838
]
3939
];
4040
}

JsonRpcRequest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class JsonRpcRequest extends Model
2828
public $params = [];
2929

3030
public $paramsPassMethod;
31+
public $parseAsArray;
3132

3233
/**
3334
* @inheritdoc
@@ -166,14 +167,21 @@ public function execute()
166167
if ($this->paramsPassMethod == Controller::JSON_RPC_PARAMS_PASS_BODY) {
167168
$app->request->setBodyParams($this->params);
168169
$app->request->setRawBody(Json::encode($this->params));
169-
$result = $app->runAction($routeParsed, $params);
170+
$result = $app->runAction($routeParsed);
170171
} else {
171172
if (ArrayHelper::isAssociative($this->params)) {
172173
$params += $this->params;
173174
} else {
174175
// allow non-named parameters
175176
$params += $this->bindParamsArray($routeParsed, $this->params);
176177
}
178+
179+
if (is_array($params) && !$this->parseAsArray) {
180+
foreach ($params as $key => $value) {
181+
$params[$key] = Json::decode(Json::encode($value), false);
182+
}
183+
}
184+
177185
$result = $app->runAction($routeParsed, $params);
178186
}
179187
} catch (JsonRpcException $e) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
use \georgique\yii2\jsonrpc\Controller;
6+
7+
class BodyParamsJsonRpcController extends Controller
8+
{
9+
// Disable CSRF validation for JSON-RPC POST requests
10+
public $enableCsrfValidation = false;
11+
12+
public $paramsPassMethod = self::JSON_RPC_PARAMS_PASS_BODY;
13+
}

demo/controllers/DemoController.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ public function actionSomeMethod()
1616
return "Some response";
1717
}
1818

19+
/**
20+
* @param $params
21+
* @return mixed
22+
*/
23+
public function actionEcho(array $params)
24+
{
25+
return [
26+
'params' => $params,
27+
'type' => gettype($params)
28+
];
29+
}
30+
31+
public function actionEchoObject($params)
32+
{
33+
return [
34+
'params' => $params,
35+
'type' => gettype($params)
36+
];
37+
}
38+
39+
public function actionObjectFoo($object)
40+
{
41+
return $object->foo;
42+
}
43+
1944
/**
2045
* @param $foo
2146
* @param $bar
@@ -53,12 +78,30 @@ public function actionSum($a, $b)
5378
return $a + $b;
5479
}
5580

56-
public function actionSumList($params)
81+
/**
82+
* @return mixed
83+
* @throws \yii\base\InvalidConfigException
84+
*/
85+
public function actionSumIntegerList()
5786
{
58-
//var_dump($params);
87+
$params = \Yii::$app->request->getBodyParams();
5988
return array_reduce($params, function ($acc, $item) {
60-
$acc += $item;
89+
$acc += is_int($item) ? $item : 0;
6190
return $acc;
6291
}, 0);
6392
}
93+
94+
/**
95+
* @return string
96+
* @throws \yii\base\InvalidConfigException
97+
*/
98+
public function actionDumpRequest()
99+
{
100+
$output = "Params received: ";
101+
$output_chunks = array();
102+
foreach (\Yii::$app->request->getBodyParams() as $name => $value) {
103+
$output_chunks[] = "$name = $value\n";
104+
}
105+
return $output . implode(', ', $output_chunks) . '.';
106+
}
64107
}

demo/controllers/JsonRpcController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ class JsonRpcController extends Controller
1111

1212
// Disable CSRF validation for JSON-RPC POST requests
1313
public $enableCsrfValidation = false;
14-
15-
public function actionVariadic(... $params)
16-
{
17-
var_dump($params);
18-
}
1914
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
use \georgique\yii2\jsonrpc\Controller;
6+
7+
class ObjectParamsJsonRpcController extends Controller
8+
{
9+
// Disable CSRF validation for JSON-RPC POST requests
10+
public $enableCsrfValidation = false;
11+
12+
public $requestParseAsArray = false;
13+
}

0 commit comments

Comments
 (0)