2008-08-09 06:38:44 +02:00
|
|
|
<?php
|
2009-03-22 23:59:14 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage formatters
|
|
|
|
*/
|
2008-08-09 06:38:44 +02:00
|
|
|
class JSONDataFormatter extends DataFormatter {
|
|
|
|
/**
|
|
|
|
* @todo pass this from the API to the data formatter somehow
|
|
|
|
*/
|
|
|
|
static $api_base = "api/v1/";
|
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
protected $outputContentType = 'application/json';
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
public function supportedExtensions() {
|
2008-08-09 09:03:24 +02:00
|
|
|
return array(
|
|
|
|
'json',
|
|
|
|
'js'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supportedMimeTypes() {
|
|
|
|
return array(
|
|
|
|
'application/json',
|
|
|
|
'text/x-json'
|
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-22 22:23:57 +01:00
|
|
|
* Generate a JSON representation of the given {@link DataObject}.
|
|
|
|
*
|
|
|
|
* @param DataObject $obj The object
|
|
|
|
* @param Array $fields If supplied, only fields in the list will be returned
|
|
|
|
* @param $relations Not used
|
|
|
|
* @return String JSON
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2008-10-09 16:47:25 +02:00
|
|
|
public function convertDataObject(DataObjectInterface $obj, $fields = null, $relations = null) {
|
2011-03-22 22:23:57 +01:00
|
|
|
return Convert::array2json($this->convertDataObjectToJSONObject($obj, $fields, $relations));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function to do the conversion of a single data object. It builds an empty object and dynamically
|
|
|
|
* adds the properties it needs to it. If it's done as a nested array, json_encode or equivalent won't use
|
|
|
|
* JSON object notation { ... }.
|
|
|
|
* @param DataObjectInterface $obj
|
|
|
|
* @param $fields
|
|
|
|
* @param $relations
|
|
|
|
* @return EmptyJSONObject
|
|
|
|
*/
|
|
|
|
public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields = null, $relations = null) {
|
2008-08-09 06:38:44 +02:00
|
|
|
$className = $obj->class;
|
|
|
|
$id = $obj->ID;
|
2011-03-22 22:23:57 +01:00
|
|
|
|
|
|
|
$serobj = ArrayData::array_to_object();
|
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
foreach($this->getFieldsForObj($obj) as $fieldName => $fieldType) {
|
2008-09-16 14:10:58 +02:00
|
|
|
// Field filtering
|
|
|
|
if($fields && !in_array($fieldName, $fields)) continue;
|
|
|
|
|
2012-09-17 03:17:54 +02:00
|
|
|
$fieldValue = $obj->obj($fieldName)->forTemplate();
|
2011-03-22 22:23:57 +01:00
|
|
|
$serobj->$fieldName = $fieldValue;
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
if($this->relationDepth > 0) {
|
|
|
|
foreach($obj->has_one() as $relName => $relClass) {
|
2009-05-29 03:04:40 +02:00
|
|
|
if(!singleton($relClass)->stat('api_access')) continue;
|
|
|
|
|
2008-09-16 14:10:58 +02:00
|
|
|
// Field filtering
|
|
|
|
if($fields && !in_array($relName, $fields)) continue;
|
2008-10-09 16:47:25 +02:00
|
|
|
if($this->customRelations && !in_array($relName, $this->customRelations)) continue;
|
2008-09-16 14:10:58 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
$fieldName = $relName . 'ID';
|
|
|
|
if($obj->$fieldName) {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/" . $obj->$fieldName);
|
|
|
|
} else {
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName");
|
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
$serobj->$relName = ArrayData::array_to_object(array("className" => $relClass, "href" => "$href.json", "id" => $obj->$fieldName));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
foreach($obj->has_many() as $relName => $relClass) {
|
2009-05-29 03:04:40 +02:00
|
|
|
if(!singleton($relClass)->stat('api_access')) continue;
|
2011-03-22 22:23:57 +01:00
|
|
|
|
2008-09-16 14:10:58 +02:00
|
|
|
// Field filtering
|
|
|
|
if($fields && !in_array($relName, $fields)) continue;
|
2008-10-09 16:47:25 +02:00
|
|
|
if($this->customRelations && !in_array($relName, $this->customRelations)) continue;
|
2008-09-16 14:10:58 +02:00
|
|
|
|
2011-03-22 22:23:57 +01:00
|
|
|
$innerParts = array();
|
2008-08-09 07:57:44 +02:00
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2011-03-22 22:23:57 +01:00
|
|
|
$innerParts[] = ArrayData::array_to_object(array("className" => $relClass, "href" => "$href.json", "id" => $obj->$fieldName));
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
$serobj->$relName = $innerParts;
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
|
|
|
|
foreach($obj->many_many() as $relName => $relClass) {
|
2009-05-29 03:04:40 +02:00
|
|
|
if(!singleton($relClass)->stat('api_access')) continue;
|
|
|
|
|
2008-09-16 14:10:58 +02:00
|
|
|
// Field filtering
|
|
|
|
if($fields && !in_array($relName, $fields)) continue;
|
2008-10-09 16:47:25 +02:00
|
|
|
if($this->customRelations && !in_array($relName, $this->customRelations)) continue;
|
2008-09-16 14:10:58 +02:00
|
|
|
|
2011-03-22 22:23:57 +01:00
|
|
|
$innerParts = array();
|
2008-08-09 07:57:44 +02:00
|
|
|
$items = $obj->$relName();
|
|
|
|
foreach($items as $item) {
|
|
|
|
//$href = Director::absoluteURL(self::$api_base . "$className/$id/$relName/$item->ID");
|
|
|
|
$href = Director::absoluteURL(self::$api_base . "$relClass/$item->ID");
|
2011-03-22 22:23:57 +01:00
|
|
|
$innerParts[] = ArrayData::array_to_object(array("className" => $relClass, "href" => "$href.json", "id" => $obj->$fieldName));
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
$serobj->$relName = $innerParts;
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 22:23:57 +01:00
|
|
|
return $serobj;
|
|
|
|
}
|
2008-08-09 06:38:44 +02:00
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Generate a JSON representation of the given {@link SS_List}.
|
2008-08-09 06:38:44 +02:00
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param SS_List $set
|
2008-08-09 06:38:44 +02:00
|
|
|
* @return String XML
|
|
|
|
*/
|
2011-05-02 09:14:05 +02:00
|
|
|
public function convertDataObjectSet(SS_List $set, $fields = null) {
|
2011-03-22 22:23:57 +01:00
|
|
|
$items = array();
|
|
|
|
foreach ($set as $do) $items[] = $this->convertDataObjectToJSONObject($do, $fields);
|
|
|
|
|
|
|
|
$serobj = ArrayData::array_to_object(array(
|
|
|
|
"totalSize" => (is_numeric($this->totalSize)) ? $this->totalSize : null,
|
|
|
|
"items" => $items
|
|
|
|
));
|
2008-08-11 02:03:57 +02:00
|
|
|
|
2011-03-22 22:23:57 +01:00
|
|
|
return Convert::array2json($serobj);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2008-08-09 09:03:24 +02:00
|
|
|
|
|
|
|
public function convertStringToArray($strData) {
|
|
|
|
return Convert::json2array($strData);
|
|
|
|
}
|
2011-03-22 22:23:57 +01:00
|
|
|
|
|
|
|
}
|