BUG Fix deprecated API usage in DataFormatter API and DataObjectTest

PHPDoc Cleanup
API DataFormatter::convertStringToArray is now abstract
This commit is contained in:
Damian Mooyman 2016-02-23 18:09:58 +13:00
parent 85c2bd7c95
commit 0ee1564896
5 changed files with 42 additions and 25 deletions

View File

@ -97,11 +97,13 @@ abstract class DataFormatter extends Object {
}
arsort($sortedClasses);
foreach($sortedClasses as $className => $priority) {
/** @var DataFormatter $formatter */
$formatter = new $className();
if(in_array($extension, $formatter->supportedExtensions())) {
return $formatter;
}
}
return null;
}
/**
@ -133,11 +135,13 @@ abstract class DataFormatter extends Object {
}
arsort($sortedClasses);
foreach($sortedClasses as $className => $priority) {
/** @var DataFormatter $formatter */
$formatter = new $className();
if(in_array($mimeType, $formatter->supportedMimeTypes())) {
return $formatter;
}
}
return null;
}
/**
@ -237,7 +241,7 @@ abstract class DataFormatter extends Object {
* @todo Allow for custom getters on the processed object (currently filtered through inheritedDatabaseFields)
* @todo Field level permission checks
*
* @param DataObject $obj
* @param DataObjectInterface|DataObject $obj
* @return array
*/
protected function getFieldsForObj($obj) {
@ -253,7 +257,7 @@ abstract class DataFormatter extends Object {
}
} else {
// by default, all database fields are selected
$dbFields = $obj->inheritedDatabaseFields();
$dbFields = $obj->db();
}
if(is_array($this->customAddFields)) {
@ -266,7 +270,7 @@ abstract class DataFormatter extends Object {
}
// add default required fields
$dbFields = array_merge($dbFields, array('ID'=>'Int'));
$dbFields = array_merge($dbFields, array('ID' => 'Int'));
if(is_array($this->removeFields)) {
$dbFields = array_diff_key($dbFields, array_combine($this->removeFields,$this->removeFields));
@ -277,27 +281,37 @@ abstract class DataFormatter extends Object {
/**
* Return an array of the extensions that this data formatter supports
*
* @return array
*/
abstract public function supportedExtensions();
/**
* Get supported mime types
*
* @return array
*/
abstract public function supportedMimeTypes();
/**
* Convert a single data object to this format. Return a string.
* Convert a single data object to this format.
*
* @param DataObjectInterface $do
* @return string
*/
abstract public function convertDataObject(DataObjectInterface $do);
/**
* Convert a data object set to this format. Return a string.
* Convert a data object set to this format.
*
* @param SS_List $set
* @return string
*/
abstract public function convertDataObjectSet(SS_List $set);
/**
* @param string $strData HTTP Payload as string
* @return array
*/
public function convertStringToArray($strData) {
user_error('DataFormatter::convertStringToArray not implemented on subclass', E_USER_ERROR);
}
abstract public function convertStringToArray($strData);
}

View File

@ -4,6 +4,7 @@
* @subpackage formatters
*/
class JSONDataFormatter extends DataFormatter {
/**
* @config
* @todo pass this from the API to the data formatter somehow
@ -29,23 +30,23 @@ class JSONDataFormatter extends DataFormatter {
/**
* 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
* @param DataObjectInterface $obj The object
* @param array $fields If supplied, only fields in the list will be returned
* @param array $relations Not used
* @return String JSON
*/
public function convertDataObject(DataObjectInterface $obj, $fields = null, $relations = null) {
return Convert::array2json($this->convertDataObjectToJSONObject($obj, $fields, $relations));
return Convert::raw2json($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
* @param DataObjectInterface|DataObject $obj
* @param array $fields
* @param array $relations
* @return stdClass
*/
public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields = null, $relations = null) {
$className = $obj->class;
@ -132,6 +133,7 @@ class JSONDataFormatter extends DataFormatter {
* Generate a JSON representation of the given {@link SS_List}.
*
* @param SS_List $set
* @param array $fields
* @return String XML
*/
public function convertDataObjectSet(SS_List $set, $fields = null) {
@ -146,7 +148,7 @@ class JSONDataFormatter extends DataFormatter {
"items" => $items
));
return Convert::array2json($serobj);
return Convert::raw2json($serobj);
}
public function convertStringToArray($strData) {

View File

@ -29,9 +29,9 @@ class XMLDataFormatter extends DataFormatter {
/**
* Generate an XML representation of the given {@link DataObject}.
*
* @param DataObject $obj
* @param $includeHeader Include <?xml ...?> header (Default: true)
* @return String XML
* @param DataObjectInterface|DataObject $obj
* @param array $fields
* @return string XML
*/
public function convertDataObject(DataObjectInterface $obj, $fields = null) {
$response = Controller::curr()->getResponse();
@ -131,11 +131,12 @@ class XMLDataFormatter extends DataFormatter {
* Generate an XML representation of the given {@link SS_List}.
*
* @param SS_List $set
* @param array $fields
* @return String XML
*/
public function convertDataObjectSet(SS_List $set, $fields = null) {
Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml");
$className = $set->class;
$className = get_class($set);
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n";

View File

@ -837,7 +837,7 @@ class DataObjectTest extends SapphireTest {
'HasOneRelationshipID',
'ExtendedHasOneRelationshipID'
),
array_keys($teamInstance->inheritedDatabaseFields()),
array_keys($teamInstance->db()),
'inheritedDatabaseFields() contains all fields defined on instance: base, extended and foreign keys'
);

View File

@ -93,7 +93,7 @@ class ArrayData extends ViewableData {
* Converts an associative array to a simple object
*
* @param array
* @return obj $obj
* @return stdClass $obj
*/
public static function array_to_object($arr = null) {
$obj = new stdClass();