2008-08-09 06:38:44 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-03 10:51:02 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
|
|
|
|
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 XMLDataFormatter extends DataFormatter {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-08-09 06:38:44 +02:00
|
|
|
* @todo pass this from the API to the data formatter somehow
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $api_base = "api/v1/";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
protected $outputContentType = 'text/xml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
public function supportedExtensions() {
|
2008-08-09 09:03:24 +02:00
|
|
|
return array(
|
|
|
|
'xml'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
public function supportedMimeTypes() {
|
|
|
|
return array(
|
|
|
|
'text/xml',
|
2008-08-11 04:57:59 +02:00
|
|
|
'application/xml',
|
2008-08-09 09:03:24 +02:00
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an XML representation of the given {@link DataObject}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-02-23 06:09:58 +01:00
|
|
|
* @param DataObjectInterface|DataObject $obj
|
|
|
|
* @param array $fields
|
|
|
|
* @return string XML
|
2008-08-09 06:38:44 +02:00
|
|
|
*/
|
2008-09-16 14:10:58 +02:00
|
|
|
public function convertDataObject(DataObjectInterface $obj, $fields = null) {
|
2009-07-17 01:47:12 +02:00
|
|
|
$response = Controller::curr()->getResponse();
|
|
|
|
if($response) {
|
|
|
|
$response->addHeader("Content-Type", "text/xml");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-16 14:10:58 +02:00
|
|
|
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $this->convertDataObjectWithoutHeader($obj, $fields);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2010-12-20 05:51:24 +01:00
|
|
|
|
2008-10-09 16:47:25 +02:00
|
|
|
public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, $relations = null) {
|
2008-08-09 06:38:44 +02:00
|
|
|
$className = $obj->class;
|
|
|
|
$id = $obj->ID;
|
2013-03-21 19:48:54 +01:00
|
|
|
$objHref = Director::absoluteURL($this->config()->api_base . "$obj->class/$obj->ID");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml = "<$className href=\"$objHref.xml\">\n";
|
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
|
2016-06-03 10:51:02 +02:00
|
|
|
if($fields && !in_array($fieldName, $fields)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$fieldObject = $obj->obj($fieldName);
|
|
|
|
$fieldValue = $fieldObject->forTemplate();
|
|
|
|
if(!mb_check_encoding($fieldValue, 'utf-8')) {
|
|
|
|
$fieldValue = "(data is badly encoded)";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:53:26 +02:00
|
|
|
if(is_object($fieldValue) && is_subclass_of($fieldValue, 'Object') && $fieldValue->hasMethod('toXML')) {
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= $fieldValue->toXML();
|
2008-08-09 06:38:44 +02:00
|
|
|
} else {
|
2016-06-03 10:51:02 +02:00
|
|
|
if($fieldObject instanceof DBHTMLText) {
|
2010-12-20 05:51:24 +01:00
|
|
|
// Escape HTML values using CDATA
|
|
|
|
$fieldValue = sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $fieldValue));
|
|
|
|
}
|
|
|
|
$xml .= "<$fieldName>$fieldValue</$fieldName>\n";
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
if($this->relationDepth > 0) {
|
2015-02-25 16:21:33 +01:00
|
|
|
foreach($obj->hasOne() as $relName => $relClass) {
|
2009-05-29 03:04:40 +02:00
|
|
|
if(!singleton($relClass)->stat('api_access')) continue;
|
2014-08-15 08:53:05 +02: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
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
$fieldName = $relName . 'ID';
|
|
|
|
if($obj->$fieldName) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$relClass/" . $obj->$fieldName);
|
2008-08-09 07:57:44 +02:00
|
|
|
} else {
|
2013-03-21 19:48:54 +01:00
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName");
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
$xml .= "<$relName linktype=\"has_one\" href=\"$href.xml\" id=\"" . $obj->$fieldName
|
|
|
|
. "\"></$relName>\n";
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2008-08-09 09:03:24 +02:00
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
foreach($obj->hasMany() 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
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= "<$relName linktype=\"has_many\" href=\"$objHref/$relName.xml\">\n";
|
2008-08-09 07:57:44 +02:00
|
|
|
$items = $obj->$relName();
|
2009-06-04 08:20:32 +02:00
|
|
|
if ($items) {
|
|
|
|
foreach($items as $item) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID");
|
2009-06-04 08:20:32 +02:00
|
|
|
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n";
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= "</$relName>\n";
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-25 16:21:33 +01:00
|
|
|
foreach($obj->manyMany() as $relName => $relClass) {
|
2009-05-29 03:04:40 +02:00
|
|
|
if(!singleton($relClass)->stat('api_access')) continue;
|
2014-08-15 08:53:05 +02: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
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= "<$relName linktype=\"many_many\" href=\"$objHref/$relName.xml\">\n";
|
2008-08-09 07:57:44 +02:00
|
|
|
$items = $obj->$relName();
|
2009-06-04 08:20:32 +02:00
|
|
|
if ($items) {
|
|
|
|
foreach($items as $item) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID");
|
2009-06-04 08:20:32 +02:00
|
|
|
$xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n";
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= "</$relName>\n";
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
$xml .= "</$className>";
|
|
|
|
|
|
|
|
return $xml;
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Generate an XML representation of the given {@link SS_List}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param SS_List $set
|
2016-02-23 06:09:58 +01:00
|
|
|
* @param array $fields
|
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) {
|
2009-02-02 00:49:53 +01:00
|
|
|
Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml");
|
2016-02-23 06:09:58 +01:00
|
|
|
$className = get_class($set);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 02:03:57 +02:00
|
|
|
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
|
$xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n";
|
2008-08-09 06:38:44 +02:00
|
|
|
foreach($set as $item) {
|
2012-10-30 23:34:29 +01:00
|
|
|
$xml .= $this->convertDataObjectWithoutHeader($item, $fields);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
$xml .= "</$className>";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
return $xml;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 09:03:24 +02:00
|
|
|
public function convertStringToArray($strData) {
|
|
|
|
return Convert::xml2array($strData);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|