Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Static Public Member Functions
Mapper Class Reference

Static Public Member Functions

static & accumulateByMap ($from, $to, array $map, array $defaults=[])
 

Detailed Description

@SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 16 of file Mapper.php.

Member Function Documentation

◆ accumulateByMap()

static& accumulateByMap (   $from,
  $to,
array  $map,
array  $defaults = [] 
)
static

Convert data from source to target item using map array

Will get or set data with generic or magic, or specified Magento Object methods, or with array keys from or to \Magento\Framework\DataObject or array :)

Map must either be associative array of keys from=>to or a numeric array of keys, assuming from = to

Defaults must be assoc array of keys => values. Target will get default, if the value is not present in source If the source has getter defined instead of magic method, the value will be taken only if not empty

Callbacks explanation (when $from or $to is not array): for $from: <\Magento\Framework\DataObject> => $from->getData($key) (default) array(<\Magento\Framework\DataObject>, <method>) => $from->$method($key) for $to (makes sense only for \Magento\Framework\DataObject): <\Magento\Framework\DataObject> => $from->setData($key, <from>) array(<\Magento\Framework\DataObject>, <method>) => $from->$method($key, <from>)

Parameters
array | \Magento\Framework\DataObject | callback$from
array | \Magento\Framework\DataObject | callback$to
array$map
array$defaults
Returns
array|object

Definition at line 45 of file Mapper.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16

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