Skip to content

Commit f7f635e

Browse files
committed
+ body params demo controller
+ tests + postman for body params controller + PSR-2 fixes + unused code cleanup
1 parent f6bb7f2 commit f7f635e

7 files changed

+210
-646
lines changed

Action.php

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ class Action extends \yii\base\Action
3333
*/
3434
public $paramsPassMethod;
3535

36-
/**
37-
* @var array Whether JSON parse should parse objects in `params` as associate arrays or objects
38-
*/
39-
public $requestParseAsArray;
40-
4136
/**
4237
* Parses json body.
4338
* @param $rawBody
@@ -83,25 +78,6 @@ protected function restoreYiiRequest()
8378
return $this;
8479
}
8580

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-
10581
/**
10682
* @inheritdoc
10783
*/
@@ -126,32 +102,21 @@ public function runWithParams($params)
126102
$request = new JsonRpcRequest();
127103
$request->paramsPassMethod = $this->paramsPassMethod;
128104

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, '');
105+
$request->load(ArrayHelper::toArray($requestData), '');
138106
if ($request->validate()) {
139107
$result = $request->execute();
140108
if (!is_null($request->id)) {
141109
$batchResponse[] = new SuccessResponse($request, $result);
142110
}
143-
}
144-
else {
111+
} else {
145112
foreach ($request->getFirstErrors() as $attribute => $error) {
146113
$request->$attribute = null;
147114
}
148115
throw new InvalidRequestException();
149116
}
150-
}
151-
catch (InvalidRequestException $e) {
117+
} catch (InvalidRequestException $e) {
152118
$batchResponse[] = new ErrorResponse($e, $request ?: null);
153-
}
154-
catch (JsonRpcException $e) {
119+
} catch (JsonRpcException $e) {
155120
// We do not return response to notifications
156121
if ($request && !is_null($request->id)) {
157122
$batchResponse[] = new ErrorResponse($e, $request ?: null);
@@ -160,8 +125,7 @@ public function runWithParams($params)
160125

161126
$this->restoreYiiRequest();
162127
}
163-
}
164-
catch (JsonRpcException $e) {
128+
} catch (JsonRpcException $e) {
165129
return new ErrorResponse($e);
166130
}
167131

Controller.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@ class Controller extends \yii\web\Controller
2323
*/
2424
public $paramsPassMethod = self::JSON_RPC_PARAMS_PASS_FUNCARGS;
2525

26-
/**
27-
* @var array Whether JSON parse should parse objects in `params` as associate arrays or objects
28-
*/
29-
public $requestParseAsArray = true;
30-
3126
public function actions()
3227
{
3328
return [
3429
'index' => [
3530
'class' => Action::class,
3631
'paramsPassMethod' => $this->paramsPassMethod,
37-
'requestParseAsArray' => $this->requestParseAsArray,
3832
]
3933
];
4034
}
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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,30 @@ public function actionSum($a, $b)
5353
return $a + $b;
5454
}
5555

56-
public function actionSumList($params)
56+
/**
57+
* @return mixed
58+
* @throws \yii\base\InvalidConfigException
59+
*/
60+
public function actionSumIntegerList()
5761
{
58-
//var_dump($params);
62+
$params = \Yii::$app->request->getBodyParams();
5963
return array_reduce($params, function ($acc, $item) {
60-
$acc += $item;
64+
$acc += is_int($item) ? $item : 0;
6165
return $acc;
6266
}, 0);
6367
}
68+
69+
/**
70+
* @return string
71+
* @throws \yii\base\InvalidConfigException
72+
*/
73+
public function actionDumpRequest()
74+
{
75+
$output = "Params received: ";
76+
$output_chunks = array();
77+
foreach (\Yii::$app->request->getBodyParams() as $name => $value) {
78+
$output_chunks[] = "$name = $value\n";
79+
}
80+
return $output . implode(', ', $output_chunks) . '.';
81+
}
6482
}

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
}

0 commit comments

Comments
 (0)