Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataObject.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework;
7 
15 class DataObject implements \ArrayAccess
16 {
22  protected $_data = [];
23 
29  protected static $_underscoreCache = [];
30 
39  public function __construct(array $data = [])
40  {
41  $this->_data = $data;
42  }
43 
52  public function addData(array $arr)
53  {
54  foreach ($arr as $index => $value) {
55  $this->setData($index, $value);
56  }
57  return $this;
58  }
59 
72  public function setData($key, $value = null)
73  {
74  if ($key === (array)$key) {
75  $this->_data = $key;
76  } else {
77  $this->_data[$key] = $value;
78  }
79  return $this;
80  }
81 
88  public function unsetData($key = null)
89  {
90  if ($key === null) {
91  $this->setData([]);
92  } elseif (is_string($key)) {
93  if (isset($this->_data[$key]) || array_key_exists($key, $this->_data)) {
94  unset($this->_data[$key]);
95  }
96  } elseif ($key === (array)$key) {
97  foreach ($key as $element) {
98  $this->unsetData($element);
99  }
100  }
101  return $this;
102  }
103 
119  public function getData($key = '', $index = null)
120  {
121  if ('' === $key) {
122  return $this->_data;
123  }
124 
125  /* process a/b/c key as ['a']['b']['c'] */
126  if (strpos($key, '/') !== false) {
127  $data = $this->getDataByPath($key);
128  } else {
129  $data = $this->_getData($key);
130  }
131 
132  if ($index !== null) {
133  if ($data === (array)$data) {
134  $data = isset($data[$index]) ? $data[$index] : null;
135  } elseif (is_string($data)) {
136  $data = explode(PHP_EOL, $data);
137  $data = isset($data[$index]) ? $data[$index] : null;
138  } elseif ($data instanceof \Magento\Framework\DataObject) {
139  $data = $data->getData($index);
140  } else {
141  $data = null;
142  }
143  }
144  return $data;
145  }
146 
155  public function getDataByPath($path)
156  {
157  $keys = explode('/', $path);
158 
160  foreach ($keys as $key) {
161  if ((array)$data === $data && isset($data[$key])) {
162  $data = $data[$key];
163  } elseif ($data instanceof \Magento\Framework\DataObject) {
164  $data = $data->getDataByKey($key);
165  } else {
166  return null;
167  }
168  }
169  return $data;
170  }
171 
178  public function getDataByKey($key)
179  {
180  return $this->_getData($key);
181  }
182 
189  protected function _getData($key)
190  {
191  if (isset($this->_data[$key])) {
192  return $this->_data[$key];
193  }
194  return null;
195  }
196 
204  public function setDataUsingMethod($key, $args = [])
205  {
206  $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
207  $this->{$method}($args);
208  return $this;
209  }
210 
218  public function getDataUsingMethod($key, $args = null)
219  {
220  $method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
221  return $this->{$method}($args);
222  }
223 
231  public function hasData($key = '')
232  {
233  if (empty($key) || !is_string($key)) {
234  return !empty($this->_data);
235  }
236  return array_key_exists($key, $this->_data);
237  }
238 
245  public function toArray(array $keys = [])
246  {
247  if (empty($keys)) {
248  return $this->_data;
249  }
250 
251  $result = [];
252  foreach ($keys as $key) {
253  if (isset($this->_data[$key])) {
254  $result[$key] = $this->_data[$key];
255  } else {
256  $result[$key] = null;
257  }
258  }
259  return $result;
260  }
261 
268  public function convertToArray(array $keys = [])
269  {
270  return $this->toArray($keys);
271  }
272 
282  public function toXml(array $keys = [], $rootName = 'item', $addOpenTag = false, $addCdata = true)
283  {
284  $xml = '';
285  $data = $this->toArray($keys);
286  foreach ($data as $fieldName => $fieldValue) {
287  if ($addCdata === true) {
288  $fieldValue = "<![CDATA[{$fieldValue}]]>";
289  } else {
290  $fieldValue = str_replace(
291  ['&', '"', "'", '<', '>'],
292  ['&amp;', '&quot;', '&apos;', '&lt;', '&gt;'],
293  $fieldValue
294  );
295  }
296  $xml .= "<{$fieldName}>{$fieldValue}</{$fieldName}>\n";
297  }
298  if ($rootName) {
299  $xml = "<{$rootName}>\n{$xml}</{$rootName}>\n";
300  }
301  if ($addOpenTag) {
302  $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $xml;
303  }
304  return $xml;
305  }
306 
316  public function convertToXml(
317  array $arrAttributes = [],
318  $rootName = 'item',
319  $addOpenTag = false,
320  $addCdata = true
321  ) {
322  return $this->toXml($arrAttributes, $rootName, $addOpenTag, $addCdata);
323  }
324 
332  public function toJson(array $keys = [])
333  {
334  $data = $this->toArray($keys);
335  return \Magento\Framework\Serialize\JsonConverter::convert($data);
336  }
337 
345  public function convertToJson(array $keys = [])
346  {
347  return $this->toJson($keys);
348  }
349 
358  public function toString($format = '')
359  {
360  if (empty($format)) {
361  $result = implode(', ', $this->getData());
362  } else {
363  preg_match_all('/\{\{([a-z0-9_]+)\}\}/is', $format, $matches);
364  foreach ($matches[1] as $var) {
365  $format = str_replace('{{' . $var . '}}', $this->getData($var), $format);
366  }
367  $result = $format;
368  }
369  return $result;
370  }
371 
380  public function __call($method, $args)
381  {
382  switch (substr($method, 0, 3)) {
383  case 'get':
384  $key = $this->_underscore(substr($method, 3));
385  $index = isset($args[0]) ? $args[0] : null;
386  return $this->getData($key, $index);
387  case 'set':
388  $key = $this->_underscore(substr($method, 3));
389  $value = isset($args[0]) ? $args[0] : null;
390  return $this->setData($key, $value);
391  case 'uns':
392  $key = $this->_underscore(substr($method, 3));
393  return $this->unsetData($key);
394  case 'has':
395  $key = $this->_underscore(substr($method, 3));
396  return isset($this->_data[$key]);
397  }
398  throw new \Magento\Framework\Exception\LocalizedException(
399  new \Magento\Framework\Phrase('Invalid method %1::%2', [get_class($this), $method])
400  );
401  }
402 
408  public function isEmpty()
409  {
410  if (empty($this->_data)) {
411  return true;
412  }
413  return false;
414  }
415 
425  protected function _underscore($name)
426  {
427  if (isset(self::$_underscoreCache[$name])) {
428  return self::$_underscoreCache[$name];
429  }
430  $result = strtolower(trim(preg_replace('/([A-Z]|[0-9]+)/', "_$1", $name), '_'));
431  self::$_underscoreCache[$name] = $result;
432  return $result;
433  }
434 
446  public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
447  {
448  $data = [];
449  if (empty($keys)) {
450  $keys = array_keys($this->_data);
451  }
452 
453  foreach ($this->_data as $key => $value) {
454  if (in_array($key, $keys)) {
455  $data[] = $key . $valueSeparator . $quote . $value . $quote;
456  }
457  }
458  $res = implode($fieldSeparator, $data);
459  return $res;
460  }
461 
469  public function debug($data = null, &$objects = [])
470  {
471  if ($data === null) {
472  $hash = spl_object_hash($this);
473  if (!empty($objects[$hash])) {
474  return '*** RECURSION ***';
475  }
476  $objects[$hash] = true;
477  $data = $this->getData();
478  }
479  $debug = [];
480  foreach ($data as $key => $value) {
481  if (is_scalar($value)) {
482  $debug[$key] = $value;
483  } elseif (is_array($value)) {
484  $debug[$key] = $this->debug($value, $objects);
485  } elseif ($value instanceof \Magento\Framework\DataObject) {
486  $debug[$key . ' (' . get_class($value) . ')'] = $value->debug(null, $objects);
487  }
488  }
489  return $debug;
490  }
491 
500  public function offsetSet($offset, $value)
501  {
502  $this->_data[$offset] = $value;
503  }
504 
512  public function offsetExists($offset)
513  {
514  return isset($this->_data[$offset]) || array_key_exists($offset, $this->_data);
515  }
516 
524  public function offsetUnset($offset)
525  {
526  unset($this->_data[$offset]);
527  }
528 
536  public function offsetGet($offset)
537  {
538  if (isset($this->_data[$offset])) {
539  return $this->_data[$offset];
540  }
541  return null;
542  }
543 }
convertToJson(array $keys=[])
Definition: DataObject.php:345
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
offsetSet($offset, $value)
Definition: DataObject.php:500
$quote
debug($data=null, &$objects=[])
Definition: DataObject.php:469
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
serialize($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
Definition: DataObject.php:446
convertToXml(array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
Definition: DataObject.php:316
$method
Definition: info.phtml:13
convertToArray(array $keys=[])
Definition: DataObject.php:268
setData($key, $value=null)
Definition: DataObject.php:72
__construct(array $data=[])
Definition: DataObject.php:39
$debug
Definition: router.php:22
setDataUsingMethod($key, $args=[])
Definition: DataObject.php:204
$index
Definition: list.phtml:44
toXml(array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
Definition: DataObject.php:282
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12