You are here

class ServicesClientConnectionRestServerParser in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_client_connection/plugins/ServicesClientConnectionRestServer.inc \ServicesClientConnectionRestServerParser

Hierarchy

Expanded class hierarchy of ServicesClientConnectionRestServerParser

File

services_client_connection/plugins/ServicesClientConnectionRestServer.inc, line 280

View source
class ServicesClientConnectionRestServerParser {

  /**
   * Definition of supported parsers
   */
  private static $parsers = array(
    'json' => array(
      'title' => 'JSON',
      'accept' => 'application/json',
      'method' => 'parseJson',
    ),
    'php' => array(
      'title' => 'PHP serialized',
      'accept' => 'application/vnd.php.serialized',
      'method' => 'parseSerialized',
    ),
    'xml' => array(
      'title' => 'XML',
      'accept' => 'application/xml',
      'method' => 'parseXml',
    ),
  );

  /**
   * Get list of supported parsers.
   *
   * @return type
   */
  public static function getParsers() {
    $output = array();
    foreach (self::$parsers as $id => $info) {
      $output[$id] = $info['title'];
    }
    return $output;
  }

  /**
   * Parser type
   */
  private $parser;

  /**
   * Create a new reponse parser
   *
   * @param $parser
   */
  public function __construct($parser) {
    $this->parser = $parser;
  }

  /**
   * Parse retrieved data
   *
   * @param string $data
   */
  public function parse($data) {
    if ($method = $this
      ->getParserInfo('method')) {
      return call_user_func(array(
        $this,
        $method,
      ), $data);
    }
  }

  /**
   * Prepare request
   *
   * @param ServicesClientConnectionHttpRequest $request
   */
  public function prepareRequest(&$request) {
    if ($accept = $this
      ->getParserInfo('accept')) {
      $request->http_headers['Accept'] = $accept;
    }
  }

  /**
   * Parse JSON response
   */
  protected function parseJson($data) {
    $parsed = json_decode($data, TRUE);

    // Return array as result of services
    if (is_object($parsed)) {
      $parsed = (array) $parsed;
    }
    return $parsed;
  }

  /**
   * Parse serialized PHP
   */
  protected function parseSerialized($data) {
    return (array) unserialize($data);
  }

  /**
   * Parse XML
   */
  protected function parseXml($data) {
    return (array) simplexml_load_string($data);
  }

  /**
   * Get info about current formatter
   *
   * @param string $property
   *   Optionally property can be specified.
   */
  protected function getParserInfo($property = NULL) {

    // Get formatter info
    $parser = isset(self::$parsers[$this->parser]) ? self::$parsers[$this->parser] : NULL;

    // If defined property try to retrieve property from formatter info
    if (!empty($property) && !empty($parser)) {
      return isset($parser[$property]) ? $parser[$property] : NULL;
    }
    return $parser;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesClientConnectionRestServerParser::$parser private property Parser type
ServicesClientConnectionRestServerParser::$parsers private static property Definition of supported parsers
ServicesClientConnectionRestServerParser::getParserInfo protected function Get info about current formatter
ServicesClientConnectionRestServerParser::getParsers public static function Get list of supported parsers.
ServicesClientConnectionRestServerParser::parse public function Parse retrieved data
ServicesClientConnectionRestServerParser::parseJson protected function Parse JSON response
ServicesClientConnectionRestServerParser::parseSerialized protected function Parse serialized PHP
ServicesClientConnectionRestServerParser::parseXml protected function Parse XML
ServicesClientConnectionRestServerParser::prepareRequest public function Prepare request
ServicesClientConnectionRestServerParser::__construct public function Create a new reponse parser