You are here

abstract class RestWSBaseFormat in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 restws.formats.inc \RestWSBaseFormat

A base for all simple formats that are just serializing/unserializing an array of property values.

Hierarchy

Expanded class hierarchy of RestWSBaseFormat

File

./restws.formats.inc, line 81
RESTful web services module formats.

View source
abstract class RestWSBaseFormat implements RestWSFormatInterface {
  protected $formatName;
  protected $formatInfo;
  public function __construct($name, $info) {
    $this->formatName = $name;
    $this->formatInfo = $info;
  }

  /**
   * Gets the representation of a resource.
   */
  public function viewResource($resourceController, $id) {
    $values = self::getData($resourceController
      ->wrapper($id));
    return $this
      ->serialize($values);
  }

  /**
   * Creates a new resource.
   */
  public function createResource($resourceController, $data) {
    $values = $this
      ->unserialize($data);
    $id = $resourceController
      ->create($values);
    $ref = self::getResourceReference($resourceController
      ->resource(), $id);
    return $this
      ->serialize($ref);
  }

  /**
   * Updates a resource.
   */
  public function updateResource($resourceController, $id, $data) {
    $values = $this
      ->unserialize($data);
    $resourceController
      ->update($id, $values);

    // Return an empty representation.
    return $this
      ->serialize(array());
  }

  /**
   * Deletes a resource.
   */
  public function deleteResource($resourceController, $id) {
    $resourceController
      ->delete($id);

    // Return an empty representation.
    return $this
      ->serialize(array());
  }
  public function mimeType() {
    return $this->formatInfo['mime type'];
  }
  public function getName() {
    return $this->formatName;
  }

  /**
   * Gets a simple PHP array using URI references for some wrapped data.
   */
  public static function getData($wrapper) {
    $data = array();
    $filtered = restws_property_access_filter($wrapper);
    foreach ($filtered as $name => $property) {
      try {
        if ($property instanceof EntityDrupalWrapper) {

          // For referenced entities only return the URI.
          if ($id = $property
            ->getIdentifier()) {
            $data[$name] = self::getResourceReference($property
              ->type(), $id);
          }
        }
        elseif ($property instanceof EntityValueWrapper) {
          $data[$name] = $property
            ->value();
        }
        elseif ($property instanceof EntityListWrapper || $property instanceof EntityStructureWrapper) {
          $data[$name] = self::getData($property);
        }
      } catch (EntityMetadataWrapperException $e) {

        // A property causes problems - ignore that.
      }
    }
    return $data;
  }
  public static function getResourceReference($resource, $id) {
    return array(
      'uri' => restws_resource_uri($resource, $id),
      'id' => $id,
      'resource' => $resource,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RestWSBaseFormat::$formatInfo protected property
RestWSBaseFormat::$formatName protected property
RestWSBaseFormat::createResource public function Creates a new resource. Overrides RestWSFormatInterface::createResource 2
RestWSBaseFormat::deleteResource public function Deletes a resource. Overrides RestWSFormatInterface::deleteResource
RestWSBaseFormat::getData public static function Gets a simple PHP array using URI references for some wrapped data.
RestWSBaseFormat::getName public function Returns the short name of this format. Overrides RestWSFormatInterface::getName
RestWSBaseFormat::getResourceReference public static function
RestWSBaseFormat::mimeType public function Returns the mime type of this format, e.g. 'application/json' or 'application/xml'. Overrides RestWSFormatInterface::mimeType
RestWSBaseFormat::updateResource public function Updates a resource. Overrides RestWSFormatInterface::updateResource 1
RestWSBaseFormat::viewResource public function Gets the representation of a resource. Overrides RestWSFormatInterface::viewResource 2
RestWSBaseFormat::__construct public function 1