1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace yii\web;
- use yii\base\InvalidArgumentException;
- use yii\helpers\Json;
- class JsonParser implements RequestParserInterface
- {
-
- public $asArray = true;
-
- public $throwException = true;
-
- public function parse($rawBody, $contentType)
- {
- try {
- $parameters = Json::decode($rawBody, $this->asArray);
- return $parameters === null ? [] : $parameters;
- } catch (InvalidArgumentException $e) {
- if ($this->throwException) {
- throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage());
- }
- return [];
- }
- }
- }
|