Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xml.php
Go to the documentation of this file.
1 <?php
9 
10 class Xml implements \Magento\Framework\Webapi\Rest\Response\RendererInterface
11 {
15  const MIME_TYPE = 'application/xml';
16 
20  const XML_ROOT_NODE = 'response';
21 
25  const DEFAULT_ENTITY_ITEM_NAME = 'item';
26 
30  protected $_xmlGenerator;
31 
37  public function __construct(\Magento\Framework\Xml\Generator $xmlGenerator)
38  {
39  $this->_xmlGenerator = $xmlGenerator;
40  }
41 
47  public function getMimeType()
48  {
49  return self::MIME_TYPE;
50  }
51 
58  public function render($data)
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  }
66 
77  protected function _formatData($data, $isRoot = false)
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  }
101 
108  protected function _formatValue($value)
109  {
110  if (is_bool($value)) {
112  $value = $value ? 'true' : 'false';
113  }
114  $replacementMap = ['&' => '&amp;'];
115  return str_replace(array_keys($replacementMap), array_values($replacementMap), $value);
116  }
117 
126  protected function _prepareKey($key)
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  }
168 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\Xml\Generator $xmlGenerator)
Definition: Xml.php:37
$value
Definition: gender.phtml:16