You are here

public function ServicesParserXML::parse in Services 7.3

Overrides ServicesParserInterface::parse

File

servers/rest_server/includes/ServicesParser.inc, line 21

Class

ServicesParserXML

Code

public function parse(ServicesContextInterface $context) {

  // get/hold the old error state
  $old_error_state = libxml_use_internal_errors(1);

  // clear all libxml errors
  libxml_clear_errors();

  // get a now SimpleXmlElement object from the XML string
  $xml_data = simplexml_load_string($context
    ->getRequestBody());

  // if $xml_data is Null then we expect errors
  if (!$xml_data) {

    // build an error message string
    $message = '';
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
      $message .= t('Line @line, Col @column: @message', array(
        '@line' => $error->line,
        '@column' => $error->column,
        '@message' => $error->message,
      )) . "\n\n";
    }

    // clear all libxml errors and restore the old error state
    libxml_clear_errors();
    libxml_use_internal_errors($old_error_state);

    // throw an error
    services_error($message, 406);
  }

  // whew, no errors, restore the old error state
  libxml_use_internal_errors($old_error_state);

  // unmarshal the SimpleXmlElement, and return the resulting array
  $php_array = $this
    ->unmarshalXML($xml_data, NULL);
  return (array) $php_array;
}