Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Mapper.php
Go to the documentation of this file.
1 <?php
11 
16 class Mapper
17 {
45  public static function &accumulateByMap($from, $to, array $map, array $defaults = [])
46  {
47  $get = 'getData';
48  if (is_array(
49  $from
50  ) && isset(
51  $from[0]
52  ) && is_object(
53  $from[0]
54  ) && isset(
55  $from[1]
56  ) && is_string(
57  $from[1]
58  ) && is_callable(
59  $from
60  )
61  ) {
62  list($from, $get) = $from;
63  }
64  $fromIsArray = is_array($from);
65  $fromIsVO = $from instanceof \Magento\Framework\DataObject;
66 
67  $set = 'setData';
68  if (is_array(
69  $to
70  ) && isset(
71  $to[0]
72  ) && is_object(
73  $to[0]
74  ) && isset(
75  $to[1]
76  ) && is_string(
77  $to[1]
78  ) && is_callable(
79  $to
80  )
81  ) {
82  list($to, $set) = $to;
83  }
84  $toIsArray = is_array($to);
85  $toIsVO = $to instanceof \Magento\Framework\DataObject;
86 
87  foreach ($map as $keyFrom => $keyTo) {
88  if (!is_string($keyFrom)) {
89  $keyFrom = $keyTo;
90  }
91  if ($fromIsArray) {
92  if (array_key_exists($keyFrom, $from)) {
93  if ($toIsArray) {
94  $to[$keyTo] = $from[$keyFrom];
95  } elseif ($toIsVO) {
96  $to->{$set}($keyTo, $from[$keyFrom]);
97  }
98  }
99  } elseif ($fromIsVO) {
100  // get value if (any) value is found as in magic data or a non-empty value with declared getter
101  $value = null;
102  if ($shouldGet = $from->hasData($keyFrom)) {
103  $value = $from->{$get}($keyFrom);
104  } elseif (method_exists($from, $get)) {
105  $value = $from->{$get}($keyFrom);
106  if ($value) {
107  $shouldGet = true;
108  }
109  }
110  if ($shouldGet) {
111  if ($toIsArray) {
112  $to[$keyTo] = $value;
113  } elseif ($toIsVO) {
114  $to->{$set}($keyTo, $value);
115  }
116  }
117  }
118  }
119  foreach ($defaults as $keyTo => $value) {
120  if ($toIsArray) {
121  if (!isset($to[$keyTo])) {
122  $to[$keyTo] = $value;
123  }
124  } elseif ($toIsVO) {
125  if (!$to->hasData($keyTo)) {
126  $to->{$set}($keyTo, $value);
127  }
128  }
129  }
130  return $to;
131  }
132 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
static & accumulateByMap($from, $to, array $map, array $defaults=[])
Definition: Mapper.php:45