To enable parsing for JSON requests you can configure [[Request::parsers]] using this class: php 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', ] ]
Since: 2.0
Author: Dan Schmidt (danschmidt5189@gmail.com)
Inheritance: implements yii\web\RequestParserInterface
示例#1
0
 /**
  * Parse resource object into the input data to populates the model
  * @inheritdoc
  */
 public function parse($rawBody, $contentType)
 {
     $array = parent::parse($rawBody, $contentType);
     if ($type = ArrayHelper::getValue($array, 'data.type')) {
         $formName = call_user_func($this->formNameCallback, $type);
         if ($attributes = ArrayHelper::getValue($array, 'data.attributes')) {
             $result[$formName] = $attributes;
         } elseif ($id = ArrayHelper::getValue($array, 'data.id')) {
             $result[$formName] = ['id' => $id, 'type' => $type];
         }
         if ($relationships = ArrayHelper::getValue($array, 'data.relationships')) {
             foreach ($relationships as $name => $relationship) {
                 if (isset($relationship[0])) {
                     foreach ($relationship as $item) {
                         if (isset($item['type']) && isset($item['id'])) {
                             $formName = call_user_func($this->formNameCallback, $item['type']);
                             $result[$name][$formName][] = $item;
                         }
                     }
                 } elseif (isset($relationship['type']) && isset($relationship['id'])) {
                     $formName = call_user_func($this->formNameCallback, $relationship['type']);
                     $result[$name][$formName] = $relationship;
                 }
             }
         }
     } else {
         $data = ArrayHelper::getValue($array, 'data', []);
         foreach ($data as $relationLink) {
             if (isset($relationLink['type']) && isset($relationLink['id'])) {
                 $formName = call_user_func($this->formNameCallback, $relationLink['type']);
                 $result[$formName][] = $relationLink;
             }
         }
     }
     return isset($result) ? $result : $array;
 }
 /**
  * Get the guid array of blames. it may check all guids if valid before return.
  * @param boolean $checkValid determines whether checking the blame is valid.
  * @return array all guids in json format.
  */
 public function getBlameGuids($checkValid = false)
 {
     $multiBlamesAttribute = $this->multiBlamesAttribute;
     if ($multiBlamesAttribute === false) {
         return [];
     }
     $jsonParser = new JsonParser();
     $guids = $jsonParser->parse($this->{$multiBlamesAttribute}, true);
     if ($checkValid) {
         $guids = $this->unsetInvalidBlames($guids);
     }
     return $guids;
 }