Typo3XmlSerializer

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).
  • useCDATA [bool] Wrap node value with - if text contains special characters.
  • 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.
Internal

still experimental

Table of Contents

Methods

encode()  : string
encodeWithReturningExceptionAsString()  : string
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.
cleanUpNodeName()  : string
The node name is cleaned so that it contains only alphanumeric characters (plus - and _) and is no longer than 100 characters.
isBinaryValue()  : bool
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.
parseArray()  : string

Methods

encode()

public encode(array<string|int, mixed> $input[, Typo3XmlParserOptions|null $options = null ][, array<string|int, mixed> $additionalOptions = [] ]) : string
Parameters
$input : array<string|int, mixed>

PHP array

$options : Typo3XmlParserOptions|null = null

Apply specific encoding configuration - XML format, namespace prefix and root node name

$additionalOptions : array<string|int, mixed> = []

Apply specific encoding options - for the full array or specific array paths.

Return values
string

XML string

encodeWithReturningExceptionAsString()

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.

public encodeWithReturningExceptionAsString(array<string|int, mixed> $input[, Typo3XmlParserOptions|null $options = null ][, array<string|int, mixed> $additionalOptions = [] ]) : string

In perspective, all uses of this method should be replaced by encode() and the exceptions should be handled locally.

Parameters
$input : array<string|int, mixed>

PHP array

$options : Typo3XmlParserOptions|null = null

Encoding configuration - see encode() for details

$additionalOptions : array<string|int, mixed> = []

Encoding options - see encode() for details

Return values
string

XML or exception

cleanUpNodeName()

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

protected cleanUpNodeName(string $nodeName) : string
Parameters
$nodeName : string
Return values
string

Cleaned node name

isBinaryValue()

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.

protected isBinaryValue(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

parseArray()

protected parseArray(array<string|int, mixed> $input, Typo3XmlParserOptions $options, array<string|int, mixed> $additionalOptions[, int $level = 0 ][, array<string|int, mixed> $stackData = [] ]) : string
Parameters
$input : array<string|int, mixed>
$options : Typo3XmlParserOptions
$additionalOptions : array<string|int, mixed>
$level : int = 0
$stackData : array<string|int, mixed> = []
Return values
string

        
On this page

Search results