mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #5081 from tractorcow/pulls/4.0/fix-deprecated-api
BUG Fix deprecated API usage in DataFormatter API and DataObjectTest
This commit is contained in:
commit
f088b07b1c
@ -97,11 +97,13 @@ abstract class DataFormatter extends Object {
|
|||||||
}
|
}
|
||||||
arsort($sortedClasses);
|
arsort($sortedClasses);
|
||||||
foreach($sortedClasses as $className => $priority) {
|
foreach($sortedClasses as $className => $priority) {
|
||||||
|
/** @var DataFormatter $formatter */
|
||||||
$formatter = new $className();
|
$formatter = new $className();
|
||||||
if(in_array($extension, $formatter->supportedExtensions())) {
|
if(in_array($extension, $formatter->supportedExtensions())) {
|
||||||
return $formatter;
|
return $formatter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,11 +135,13 @@ abstract class DataFormatter extends Object {
|
|||||||
}
|
}
|
||||||
arsort($sortedClasses);
|
arsort($sortedClasses);
|
||||||
foreach($sortedClasses as $className => $priority) {
|
foreach($sortedClasses as $className => $priority) {
|
||||||
|
/** @var DataFormatter $formatter */
|
||||||
$formatter = new $className();
|
$formatter = new $className();
|
||||||
if(in_array($mimeType, $formatter->supportedMimeTypes())) {
|
if(in_array($mimeType, $formatter->supportedMimeTypes())) {
|
||||||
return $formatter;
|
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 Allow for custom getters on the processed object (currently filtered through inheritedDatabaseFields)
|
||||||
* @todo Field level permission checks
|
* @todo Field level permission checks
|
||||||
*
|
*
|
||||||
* @param DataObject $obj
|
* @param DataObjectInterface|DataObject $obj
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getFieldsForObj($obj) {
|
protected function getFieldsForObj($obj) {
|
||||||
@ -253,7 +257,7 @@ abstract class DataFormatter extends Object {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// by default, all database fields are selected
|
// by default, all database fields are selected
|
||||||
$dbFields = $obj->inheritedDatabaseFields();
|
$dbFields = $obj->db();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_array($this->customAddFields)) {
|
if(is_array($this->customAddFields)) {
|
||||||
@ -266,7 +270,7 @@ abstract class DataFormatter extends Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add default required fields
|
// add default required fields
|
||||||
$dbFields = array_merge($dbFields, array('ID'=>'Int'));
|
$dbFields = array_merge($dbFields, array('ID' => 'Int'));
|
||||||
|
|
||||||
if(is_array($this->removeFields)) {
|
if(is_array($this->removeFields)) {
|
||||||
$dbFields = array_diff_key($dbFields, array_combine($this->removeFields,$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 an array of the extensions that this data formatter supports
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
abstract public function supportedExtensions();
|
abstract public function supportedExtensions();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get supported mime types
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
abstract public function supportedMimeTypes();
|
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);
|
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);
|
abstract public function convertDataObjectSet(SS_List $set);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $strData HTTP Payload as string
|
* @param string $strData HTTP Payload as string
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function convertStringToArray($strData) {
|
abstract public function convertStringToArray($strData);
|
||||||
user_error('DataFormatter::convertStringToArray not implemented on subclass', E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
* @subpackage formatters
|
* @subpackage formatters
|
||||||
*/
|
*/
|
||||||
class JSONDataFormatter extends DataFormatter {
|
class JSONDataFormatter extends DataFormatter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @config
|
* @config
|
||||||
* @todo pass this from the API to the data formatter somehow
|
* @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}.
|
* Generate a JSON representation of the given {@link DataObject}.
|
||||||
*
|
*
|
||||||
* @param DataObject $obj The object
|
* @param DataObjectInterface $obj The object
|
||||||
* @param Array $fields If supplied, only fields in the list will be returned
|
* @param array $fields If supplied, only fields in the list will be returned
|
||||||
* @param $relations Not used
|
* @param array $relations Not used
|
||||||
* @return String JSON
|
* @return String JSON
|
||||||
*/
|
*/
|
||||||
public function convertDataObject(DataObjectInterface $obj, $fields = null, $relations = null) {
|
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
|
* 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
|
* 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 { ... }.
|
* JSON object notation { ... }.
|
||||||
* @param DataObjectInterface $obj
|
* @param DataObjectInterface|DataObject $obj
|
||||||
* @param $fields
|
* @param array $fields
|
||||||
* @param $relations
|
* @param array $relations
|
||||||
* @return EmptyJSONObject
|
* @return stdClass
|
||||||
*/
|
*/
|
||||||
public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields = null, $relations = null) {
|
public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields = null, $relations = null) {
|
||||||
$className = $obj->class;
|
$className = $obj->class;
|
||||||
@ -132,6 +133,7 @@ class JSONDataFormatter extends DataFormatter {
|
|||||||
* Generate a JSON representation of the given {@link SS_List}.
|
* Generate a JSON representation of the given {@link SS_List}.
|
||||||
*
|
*
|
||||||
* @param SS_List $set
|
* @param SS_List $set
|
||||||
|
* @param array $fields
|
||||||
* @return String XML
|
* @return String XML
|
||||||
*/
|
*/
|
||||||
public function convertDataObjectSet(SS_List $set, $fields = null) {
|
public function convertDataObjectSet(SS_List $set, $fields = null) {
|
||||||
@ -146,7 +148,7 @@ class JSONDataFormatter extends DataFormatter {
|
|||||||
"items" => $items
|
"items" => $items
|
||||||
));
|
));
|
||||||
|
|
||||||
return Convert::array2json($serobj);
|
return Convert::raw2json($serobj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function convertStringToArray($strData) {
|
public function convertStringToArray($strData) {
|
||||||
|
@ -29,9 +29,9 @@ class XMLDataFormatter extends DataFormatter {
|
|||||||
/**
|
/**
|
||||||
* Generate an XML representation of the given {@link DataObject}.
|
* Generate an XML representation of the given {@link DataObject}.
|
||||||
*
|
*
|
||||||
* @param DataObject $obj
|
* @param DataObjectInterface|DataObject $obj
|
||||||
* @param $includeHeader Include <?xml ...?> header (Default: true)
|
* @param array $fields
|
||||||
* @return String XML
|
* @return string XML
|
||||||
*/
|
*/
|
||||||
public function convertDataObject(DataObjectInterface $obj, $fields = null) {
|
public function convertDataObject(DataObjectInterface $obj, $fields = null) {
|
||||||
$response = Controller::curr()->getResponse();
|
$response = Controller::curr()->getResponse();
|
||||||
@ -131,11 +131,12 @@ class XMLDataFormatter extends DataFormatter {
|
|||||||
* Generate an XML representation of the given {@link SS_List}.
|
* Generate an XML representation of the given {@link SS_List}.
|
||||||
*
|
*
|
||||||
* @param SS_List $set
|
* @param SS_List $set
|
||||||
|
* @param array $fields
|
||||||
* @return String XML
|
* @return String XML
|
||||||
*/
|
*/
|
||||||
public function convertDataObjectSet(SS_List $set, $fields = null) {
|
public function convertDataObjectSet(SS_List $set, $fields = null) {
|
||||||
Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml");
|
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||||
$xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n";
|
$xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n";
|
||||||
|
@ -837,7 +837,7 @@ class DataObjectTest extends SapphireTest {
|
|||||||
'HasOneRelationshipID',
|
'HasOneRelationshipID',
|
||||||
'ExtendedHasOneRelationshipID'
|
'ExtendedHasOneRelationshipID'
|
||||||
),
|
),
|
||||||
array_keys($teamInstance->inheritedDatabaseFields()),
|
array_keys($teamInstance->db()),
|
||||||
'inheritedDatabaseFields() contains all fields defined on instance: base, extended and foreign keys'
|
'inheritedDatabaseFields() contains all fields defined on instance: base, extended and foreign keys'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ class ArrayData extends ViewableData {
|
|||||||
* Converts an associative array to a simple object
|
* Converts an associative array to a simple object
|
||||||
*
|
*
|
||||||
* @param array
|
* @param array
|
||||||
* @return obj $obj
|
* @return stdClass $obj
|
||||||
*/
|
*/
|
||||||
public static function array_to_object($arr = null) {
|
public static function array_to_object($arr = null) {
|
||||||
$obj = new stdClass();
|
$obj = new stdClass();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user