Version: current

# Class DataFormatter

A DataFormatter object handles transformation of data from Sapphire model objects to a particular output format, and vice versa. This is most commonly used in developing RESTful APIs.

  Object
  DataFormatter

### Direct known subclasses

`JSONDataFormatter`, `XMLDataFormatter`

### Indirect known subclasses

`FormEncodedDataFormatter`

Abstract
Package: sapphire\formatters
Located at sapphire/api/DataFormatter.php

## Methods summary

`public static `DataFormatter`` | # `for_extension( string $extension )` Get a DataFormatter object suitable for handling the given file extension. Get a DataFormatter object suitable for handling the given file extension. #### Parameters $extension string $extension #### Returns ``DataFormatter`` DataFormatter
`public static `DataFormatter`` | # `for_extensions( array $extensions )` Get formatter for the first matching extension. Get formatter for the first matching extension. #### Parameters $extensions array $extensions #### Returns ``DataFormatter`` DataFormatter
`public static `DataFormatter`` | # `for_mimetype( string $mimeType )` Get a DataFormatter object suitable for handling the given mimetype. Get a DataFormatter object suitable for handling the given mimetype. #### Parameters $mimeType string $mimeType #### Returns ``DataFormatter`` DataFormatter
`public static `DataFormatter`` | # `for_mimetypes( array $mimetypes )` Get formatter for the first matching mimetype. Useful for HTTP Accept headers which can contain multiple comma-separated mimetypes. Get formatter for the first matching mimetype. Useful for HTTP Accept headers which can contain multiple comma-separated mimetypes. #### Parameters $mimetypes array $mimetypes #### Returns ``DataFormatter`` DataFormatter
`public` | # `setCustomFields( array $fields )` #### Parameters $fields array $fields
`public array` | # `getCustomFields( )` #### Returns `array` array
`public` | # `setCustomAddFields( array $fields )` #### Parameters $fields array $fields
`public` | # `setCustomRelations( array $relations )` #### Parameters $relations array $relations
`public array` | # `getCustomRelations( )` #### Returns `array` array
`public array` | # `getCustomAddFields( )` #### Returns `array` array
`public` | # `setRemoveFields( array $fields )` #### Parameters $fields array $fields
`public array` | # `getRemoveFields( )` #### Returns `array` array
`public` | # `getOutputContentType( )`
`public` | # `setTotalSize( integer $size )` #### Parameters $size int $size
`public integer` | # `getTotalSize( )` #### Returns `integer` int
`protected array` | # `getFieldsForObj( `DataObject` $obj )` Returns all fields on the object which should be shown in the output. Can be customised through `DataFormatter::setCustomFields()`. Returns all fields on the object which should be shown in the output. Can be customised through `DataFormatter::setCustomFields()`. #### Parameters $obj DataObject $obj #### Returns `array` array
`abstract public` | # `supportedExtensions( )` Return an array of the extensions that this data formatter supports Return an array of the extensions that this data formatter supports
`abstract public` | # `supportedMimeTypes( )`
`abstract public` | # `convertDataObject( `DataObjectInterface` $do )` Convert a single data object to this format. Return a string. Convert a single data object to this format. Return a string.
`abstract public` | # `convertDataObjectSet( `DataObjectSet` $set )` Convert a data object set to this format. Return a string. Convert a data object set to this format. Return a string.
`public` | # `convertStringToArray( string $strData )` #### Parameters $strData string $strData HTTP Payload as string

### Methods inherited from Object

`__call()`, `__construct()`, `__toString()`, `__wakeup()`, `addMethodsFrom()`, `addStaticVars()`, `addWrapperMethod()`, `add_extension()`, `add_static_var()`, `allMethodNames()`, `cacheToFile()`, `cacheToFileWithArgs()`, `clearCache()`, `combined_static()`, `create()`, `createMethod()`, `create_from_string()`, `defineMethods()`, `exists()`, `extInstance()`, `extend()`, `getCustomClass()`, `getExtensionInstance()`, `getExtensionInstances()`, `get_extensions()`, `get_static()`, `hasExtension()`, `hasMethod()`, `has_extension()`, `invokeWithExtensions()`, `is_a()`, `loadCache()`, `parentClass()`, `parse_class_spec()`, `remove_extension()`, `sanitiseCachename()`, `saveCache()`, `set_stat()`, `set_static()`, `set_uninherited()`, `stat()`, `strong_create()`, `uninherited()`, `uninherited_static()`, `useCustomClass()`

### Magic methods summary

## Properties summary

`public static integer` | $priority | # Set priority from 0-100. If multiple formatters for the same extension exist, we select the one with highest priority. Set priority from 0-100. If multiple formatters for the same extension exist, we select the one with highest priority.
`public integer` | $relationDepth | # Follow relations for the `DataObject` instances ($has_one, $has_many, $many_many). Set to "0" to disable relation output. Follow relations for the `DataObject` instances ($has_one, $has_many, $many_many). Set to "0" to disable relation output.
`protected array` | $customFields | # Allows overriding of the fields which are rendered for the processed dataobjects. By default, this includes all fields in `DataObject::inheritedDatabaseFields()`. Allows overriding of the fields which are rendered for the processed dataobjects. By default, this includes all fields in `DataObject::inheritedDatabaseFields()`.
`protected array` | $customAddFields | # Allows addition of fields (e.g. custom getters on a DataObject) Allows addition of fields (e.g. custom getters on a DataObject)
`protected array` | $customRelations | # Allows to limit or add relations. Only use in combination with `DataFormatter::$relationDepth`. By default, all relations will be shown. Allows to limit or add relations. Only use in combination with `DataFormatter::$relationDepth`. By default, all relations will be shown.
`protected array` | $removeFields | # Fields which should be expicitly excluded from the export. Comes in handy for field-level permissions. Will overrule both `DataFormatter::$customAddFields` and `DataFormatter::$customFields` Fields which should be expicitly excluded from the export. Comes in handy for field-level permissions. Will overrule both `DataFormatter::$customAddFields` and `DataFormatter::$customFields`
`protected string` | $outputContentType | # Specifies the mimetype in which all strings returned from the convert*() methods should be used, e.g. "text/xml". Specifies the mimetype in which all strings returned from the convert*() methods should be used, e.g. "text/xml".
`protected integer` | $totalSize | # Used to set totalSize properties on the output of `DataFormatter::convertDataObjectSet()`, shows the total number of records without the "limit" and "offset" GET parameters. Useful to implement pagination. Used to set totalSize properties on the output of `DataFormatter::convertDataObjectSet()`, shows the total number of records without the "limit" and "offset" GET parameters. Useful to implement pagination.

### Properties inherited from Object

`$class`, `$extension_instances`, `$extensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -