‪TYPO3CMS  ‪main
TYPO3\CMS\Core\Serializer\Typo3XmlSerializer Class Reference

Public Member Functions

string encodeWithReturningExceptionAsString (array $input, Typo3XmlParserOptions $options=null, array $additionalOptions=[])
 
string encode (array $input, Typo3XmlParserOptions $options=null, array $additionalOptions=[])
 

Protected Member Functions

 parseArray (array $input, Typo3XmlParserOptions $options, array $additionalOptions, int $level=0, array $stackData=[])
 
string cleanUpNodeName (string $nodeName)
 
bool isBinaryValue (mixed $value)
 

Detailed Description

Encodes PHP array to XML string.

A dedicated set of entry properties is stored in XML during conversion:

  • ‪XML node attribute "index" stores original entry key if XML node name differs from entry key
  • ‪XML node attribute "type" stores entry value type ("bool", "int", "double", ...)
  • ‪XML node attribute "base64" specifies if entry value is binary (for example an image) These attributes are interpreted during decoding of the XML string with XmlDecoder::decode().

Specific encoding configuration can be set by $additionalOptions - for the full array or array paths. For example

$input = [
'numeric' => [
'value1',
'value2'
],
'numeric-n-index' => [
'value1',
'value2'
],
'nested' => [
'node1' => 'value1',
'node2' => [
'node' => 'value'
]
]
];
$additionalOptions = [
'useIndexTagForNum' => 'numbered-index'
'alt_options' => [
'/numeric-n-index' => [
'useNindex' => true
],
'/nested' => [
'useIndexTagForAssoc' => 'nested-outer',
'clearStackPath' => true,
'alt_options' => [
'/nested-outer' => [
'useIndexTagForAssoc' => 'nested-inner'
]
]
]
]
];

=>

<phparray>
<numeric type="array">
<numbered-index index="0">value1</numbered-index>
<numbered-index index="1">value2</numbered-index>
</numeric>
<numeric-n-index type="array">
<n0>value1</n0>
<n1>value2</n1>
</numeric-n-index>
<nested type="array">
<nested-outer index="node1">value1</nested-outer>
<nested-outer index="node2" type="array">
<nested-inner index="node">value</nested-inner>
</nested-outer>
</nested>
</phparray>

Available options are:

  • ‪grandParentTagMap[grandParentTagName/parentTagName] [string] Convert array key X to XML node name "{grandParentTagMap}" with node attribute "index=X"
    • ‪if grand-parent is "{grandParentTagName}" and parent node is "{parentTagName}".
  • ‪parentTagMap[parentTagName:_IS_NUM] [string] Convert array key X to XML node name "{parentTagMap}" with node attribute "index=X"
    • ‪if parent node is "{parentTagName}" and current node is number-indexed.
  • ‪parentTagMap[parentTagName:nodeName] [string] Convert array key X to XML node name "{parentTagMap}" with node attribute "index=X"
    • ‪if parent node is "{parentTagName}" and current node is "{nodeName}".
  • ‪parentTagMap[parentTagName] [string] Convert array key X to XML node name "{parentTagMap}" with node attribute "index=X"
    • ‪if parent node is "{parentTagName}".
  • ‪useNindex [bool] Convert number-indexed array key X to XML node name "nX".
  • ‪useIndexTagForNum [string] Convert number-indexed array key X to XML node name "{useIndexTagForNum}" with node attribute "index=X".
  • ‪useIndexTagForAssoc [string] Convert associative array key X to XML node name "{useIndexTagForAssoc}" with node attribute "index=X".
  • ‪disableTypeAttrib [bool|int] Disable node attribute "type" for all value types (true = disable for all except arrays, 2 = disable for all).
  • ‪alt_options[/.../nodeName] [array] Set new options for specific array path.
  • ‪clearStackPath [bool] Resetting internal counter when descending the array hierarchy: Allows using relative array path in nested "alt_options" instead of absolute path.

still experimental

Definition at line 120 of file Typo3XmlSerializer.php.

Member Function Documentation

◆ cleanUpNodeName()

string TYPO3\CMS\Core\Serializer\Typo3XmlSerializer::cleanUpNodeName ( string  $nodeName)
protected

The node name is cleaned so that it contains only alphanumeric characters (plus - and _) and is no longer than 100 characters.

Parameters
string$nodeName
Returns
‪string Cleaned node name

Definition at line 306 of file Typo3XmlSerializer.php.

Referenced by TYPO3\CMS\Core\Serializer\Typo3XmlSerializer\parseArray().

◆ encode()

string TYPO3\CMS\Core\Serializer\Typo3XmlSerializer::encode ( array  $input,
Typo3XmlParserOptions  $options = null,
array  $additionalOptions = [] 
)
Parameters
array$input‪PHP array
Typo3XmlParserOptions | null$options‪Apply specific encoding configuration - XML format, namespace prefix and root node name
array$additionalOptions‪Apply specific encoding options - for the full array or specific array paths.
Returns
‪string XML string

Definition at line 151 of file Typo3XmlSerializer.php.

References TYPO3\CMS\Core\Serializer\Typo3XmlSerializer\parseArray().

Referenced by TYPO3\CMS\Core\Serializer\Typo3XmlSerializer\encodeWithReturningExceptionAsString().

◆ encodeWithReturningExceptionAsString()

string TYPO3\CMS\Core\Serializer\Typo3XmlSerializer::encodeWithReturningExceptionAsString ( array  $input,
Typo3XmlParserOptions  $options = null,
array  $additionalOptions = [] 
)

This method serves as a wrapper for encode() and is used to replace GeneralUtility::array2xml(), which returns an exception as a string instead of throwing it. In perspective, all uses of this method should be replaced by encode() and the exceptions should be handled locally.

Parameters
array$input‪PHP array
Typo3XmlParserOptions | null$options‪Encoding configuration - see encode() for details
array$additionalOptions‪Encoding options - see encode() for details
Returns
‪string XML or exception

Definition at line 133 of file Typo3XmlSerializer.php.

References TYPO3\CMS\Core\Serializer\Typo3XmlSerializer\encode().

◆ isBinaryValue()

bool TYPO3\CMS\Core\Serializer\Typo3XmlSerializer::isBinaryValue ( mixed  $value)
protected

Is $value the content of a binary file, for example an image? If so, this value must be stored in a binary-safe manner so that it can be decoded correctly later.

Parameters
mixed$value
Returns
‪bool

Definition at line 318 of file Typo3XmlSerializer.php.

Referenced by TYPO3\CMS\Core\Serializer\Typo3XmlSerializer\parseArray().

◆ parseArray()