Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Xml Class Reference
Inheritance diagram for Xml:
RendererInterface

Public Member Functions

 __construct (\Magento\Framework\Xml\Generator $xmlGenerator)
 
 getMimeType ()
 
 render ($data)
 

Data Fields

const MIME_TYPE = 'application/xml'
 
const XML_ROOT_NODE = 'response'
 
const DEFAULT_ENTITY_ITEM_NAME = 'item'
 

Protected Member Functions

 _formatData ($data, $isRoot=false)
 
 _formatValue ($value)
 
 _prepareKey ($key)
 

Protected Attributes

 $_xmlGenerator
 

Detailed Description

Definition at line 10 of file Xml.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Xml\Generator  $xmlGenerator)

Initialize dependencies.

Parameters
\Magento\Framework\Xml\Generator$xmlGenerator

Definition at line 37 of file Xml.php.

38  {
39  $this->_xmlGenerator = $xmlGenerator;
40  }

Member Function Documentation

◆ _formatData()

_formatData (   $data,
  $isRoot = false 
)
protected

Reformat mixed data to multidimensional array.

This method is recursive.

Parameters
array | \Magento\Framework\DataObject$data
bool$isRoot
Returns
array
Exceptions

Definition at line 77 of file Xml.php.

78  {
79  if (!is_array($data) && !is_object($data)) {
80  if ($isRoot) {
81  return $this->_formatValue($data);
82  }
83  } elseif ($data instanceof \Magento\Framework\DataObject) {
84  $data = $data->toArray();
85  } else {
86  $data = (array)$data;
87  }
88  $isAssoc = !preg_match('/^\d+$/', implode(array_keys($data), ''));
89 
90  $formattedData = [];
91  foreach ($data as $key => $value) {
92  $value = is_array($value) || is_object($value) ? $this->_formatData($value) : $this->_formatValue($value);
93  if ($isAssoc) {
94  $formattedData[$this->_prepareKey($key)] = $value;
95  } else {
96  $formattedData[] = $value;
97  }
98  }
99  return $formattedData;
100  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16

◆ _formatValue()

_formatValue (   $value)
protected

Prepare value in contrast with key.

Parameters
string$value
Returns
string

Without the following transformation boolean values are rendered incorrectly

Definition at line 108 of file Xml.php.

109  {
110  if (is_bool($value)) {
112  $value = $value ? 'true' : 'false';
113  }
114  $replacementMap = ['&' => '&'];
115  return str_replace(array_keys($replacementMap), array_values($replacementMap), $value);
116  }
$value
Definition: gender.phtml:16

◆ _prepareKey()

_prepareKey (   $key)
protected

Format array key or field name to be valid array key name.

Replaces characters that are invalid in array key names.

Parameters
string$key
Returns
string

Definition at line 126 of file Xml.php.

127  {
128  $replacementMap = [
129  '!' => '',
130  '"' => '',
131  '#' => '',
132  '$' => '',
133  '%' => '',
134  '&' => '',
135  '\'' => '',
136  '(' => '',
137  ')' => '',
138  '*' => '',
139  '+' => '',
140  ',' => '',
141  '/' => '',
142  ';' => '',
143  '<' => '',
144  '=' => '',
145  '>' => '',
146  '?' => '',
147  '@' => '',
148  '[' => '',
149  '\\' => '',
150  ']' => '',
151  '^' => '',
152  '`' => '',
153  '{' => '',
154  '|' => '',
155  '}' => '',
156  '~' => '',
157  ' ' => '_',
158  ':' => '_',
159  ];
160  $key = str_replace(array_keys($replacementMap), array_values($replacementMap), $key);
161  $key = trim($key, '_');
162  $prohibitedTagPattern = '/^[0-9,.-]/';
163  if (preg_match($prohibitedTagPattern, $key)) {
164  $key = self::DEFAULT_ENTITY_ITEM_NAME . '_' . $key;
165  }
166  return $key;
167  }

◆ getMimeType()

getMimeType ( )

Get XML renderer MIME type.

Returns
string

Implements RendererInterface.

Definition at line 47 of file Xml.php.

◆ render()

render (   $data)

Format object|array to valid XML.

Parameters
object | array | int | string | bool | float | null$data
Returns
string

Wrap response in a single node.

Implements RendererInterface.

Definition at line 58 of file Xml.php.

59  {
60  $formattedData = $this->_formatData($data, true);
62  $formattedData = [self::XML_ROOT_NODE => $formattedData];
63  $this->_xmlGenerator->setIndexedArrayItemName(self::DEFAULT_ENTITY_ITEM_NAME)->arrayToXml($formattedData);
64  return $this->_xmlGenerator->getDom()->saveXML();
65  }

Field Documentation

◆ $_xmlGenerator

$_xmlGenerator
protected

Definition at line 30 of file Xml.php.

◆ DEFAULT_ENTITY_ITEM_NAME

const DEFAULT_ENTITY_ITEM_NAME = 'item'

This value is used to replace numeric keys while formatting data for XML output.

Definition at line 25 of file Xml.php.

◆ MIME_TYPE

const MIME_TYPE = 'application/xml'

Renderer mime type.

Definition at line 15 of file Xml.php.

◆ XML_ROOT_NODE

const XML_ROOT_NODE = 'response'

Root node in XML output.

Definition at line 20 of file Xml.php.


The documentation for this class was generated from the following file: