123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?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)
- {
-
- if (strpos($contentType, 'application/javascript') !== false) {
- $rawBody = preg_filter('/(^[^{]+|[^}]+$)/', '', $rawBody);
- }
- 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 [];
- }
- }
- }
|