abstract class RestWSBaseFormat in RESTful Web Services 7
Same name and namespace in other branches
- 7.2 restws.formats.inc \RestWSBaseFormat
A base for all simple formats that are just serializing/unserializing an array of property values.
Hierarchy
- class \RestWSBaseFormat implements RestWSFormatInterface
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RestWSBaseFormat:: |
protected | property | ||
RestWSBaseFormat:: |
protected | property | ||
RestWSBaseFormat:: |
public | function |
Creates a new resource. Overrides RestWSFormatInterface:: |
2 |
RestWSBaseFormat:: |
public | function |
Deletes a resource. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public static | function | Gets a simple PHP array using URI references for some wrapped data. | |
RestWSBaseFormat:: |
public | function |
Returns the short name of this format. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public static | function | ||
RestWSBaseFormat:: |
public | function |
Returns the mime type of this format, e.g. 'application/json' or
'application/xml'. Overrides RestWSFormatInterface:: |
|
RestWSBaseFormat:: |
public | function |
Updates a resource. Overrides RestWSFormatInterface:: |
1 |
RestWSBaseFormat:: |
public | function |
Gets the representation of a resource. Overrides RestWSFormatInterface:: |
2 |
RestWSBaseFormat:: |
public | function | 1 |