You are here

public function FormatterJsonApi::parseBody in RESTful 7.2

Parses the body string into the common format.

Parameters

string $body: The string sent from the consumer.

Return value

array The parsed object following the expected structure.

Throws

\Drupal\restful\Exception\ServerConfigurationException

\Drupal\restful\Exception\BadRequestException

Overrides Formatter::parseBody

File

src/Plugin/formatter/FormatterJsonApi.php, line 625
Contains \Drupal\restful\Plugin\formatter\FormatterJsonApi.

Class

FormatterJsonApi
Class FormatterJsonApi @package Drupal\restful\Plugin\formatter

Namespace

Drupal\restful\Plugin\formatter

Code

public function parseBody($body) {
  if (!($decoded_json = drupal_json_decode($body))) {
    throw new BadRequestException(sprintf('Invalid JSON provided: %s.', $body));
  }
  if (empty($decoded_json['data'])) {
    throw new BadRequestException(sprintf('Invalid JSON provided: %s.', $body));
  }
  $data = $decoded_json['data'];
  $includes = empty($decoded_json['included']) ? array() : $decoded_json['included'];

  // It's always weird to deal with lists of items vs a single item.
  $single_item = !ResourceFieldBase::isArrayNumeric($data);

  // Make sure we're always dealing with a list of items.
  $data = $single_item ? array(
    $data,
  ) : $data;
  $output = array();
  foreach ($data as $item) {
    $output[] = $this::restructureItem($item, $includes);
  }
  return $single_item ? reset($output) : $output;
}