You are here

class ServicesClientConnectionRestServerFormatter in Services Client 7

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

Hierarchy

Expanded class hierarchy of ServicesClientConnectionRestServerFormatter

File

services_client_connection/plugins/ServicesClientConnectionRestServer.inc, line 152

View source
class ServicesClientConnectionRestServerFormatter {

  /**
   * List of available request formatters
   */
  protected static $request_formatters = array(
    'php' => array(
      'title' => 'PHP Serialized',
      'method' => 'formatSerialize',
      'content_type' => 'application/vnd.php.serialized',
    ),
    'json' => array(
      'title' => 'JSON',
      'method' => 'formatJson',
      'content_type' => 'application/json',
    ),
    'urlencode' => array(
      'title' => 'URL Encode',
      'method' => 'formatUrlEncode',
      'content_type' => 'application/x-www-form-urlencoded',
    ),
    'formdata' => array(
      'title' => 'Multipart Data',
      'method' => 'formatMultipart',
      'content_type' => 'multipart/form-data',
    ),
  );

  /**
   * Get list of available formatters
   */
  public static function getFormatters() {
    $output = array();
    foreach (self::$request_formatters as $id => $info) {
      $output[$id] = $info['title'];
    }
    return $output;
  }

  /**
   * Holds formatter type
   */
  protected $formatter;

  /**
   * Create new instance of formatter
   *
   * @param $formatter
   */
  public function __construct($formatter) {
    $this->formatter = $formatter;
  }

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

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

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

  /**
   * Format data for remote site.
   *
   * @param array $data
   * @param ServicesClientConnectionHttpRequest $request
   */
  public function format(&$request) {
    $request->data_raw = $request->data;
    if ($request->http_method != 'GET') {

      // Format data
      if (!empty($request->data) && ($method = $this
        ->getFormatterInfo('method'))) {
        $request->data = call_user_func_array(array(
          $this,
          $method,
        ), array(
          $request->data,
          $request,
        ));
      }
      elseif (empty($request->data)) {
        $request->data = '';
      }
    }

    // Add header Content-Type
    if ($content_type = $this
      ->getFormatterInfo('content_type')) {
      $request->http_headers['Content-Type'] = $content_type;
    }
  }

  /**
   * Serialize data
   */
  protected function formatSerialize($data) {
    return serialize($data);
  }

  /**
   * JSON encode
   */
  protected function formatJson($data) {
    return json_encode($data);
  }

  /**
   * URL Encode
   */
  protected function formatUrlEncode($data) {
    return http_build_query($data, NULL, '&');
  }

  /**
   * Prepare request for multipart data
   */
  protected function formatMultipart($data) {
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesClientConnectionRestServerFormatter::$formatter protected property Holds formatter type
ServicesClientConnectionRestServerFormatter::$request_formatters protected static property List of available request formatters
ServicesClientConnectionRestServerFormatter::format public function Format data for remote site.
ServicesClientConnectionRestServerFormatter::formatJson protected function JSON encode
ServicesClientConnectionRestServerFormatter::formatMultipart protected function Prepare request for multipart data
ServicesClientConnectionRestServerFormatter::formatSerialize protected function Serialize data
ServicesClientConnectionRestServerFormatter::formatUrlEncode protected function URL Encode
ServicesClientConnectionRestServerFormatter::getFormatterInfo protected function Get info about current formatter
ServicesClientConnectionRestServerFormatter::getFormatters public static function Get list of available formatters
ServicesClientConnectionRestServerFormatter::__construct public function Create new instance of formatter