Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArrayUtils.php
Go to the documentation of this file.
1 <?php
7 
15 {
23  public function ksortMultibyte(array &$sort, $locale)
24  {
25  if (empty($sort)) {
26  return false;
27  }
28  $oldLocale = setlocale(LC_COLLATE, "0");
29  // use fallback locale if $localeCode is not available
30 
31  if (strpos($locale, '.UTF8') === false) {
32  $locale .= '.UTF8';
33  }
34 
35  setlocale(LC_COLLATE, $locale, 'C.UTF-8', 'en_US.utf8');
36  ksort($sort, SORT_LOCALE_STRING);
37  setlocale(LC_COLLATE, $oldLocale);
38 
39  return $sort;
40  }
41 
64  public function decorateArray($array, $prefix = 'decorated_', $forceSetAll = false)
65  {
66  // check if array or an object to be iterated given
67  if (!(is_array($array) || is_object($array))) {
68  return $array;
69  }
70 
71  $keyIsFirst = "{$prefix}is_first";
72  $keyIsOdd = "{$prefix}is_odd";
73  $keyIsEven = "{$prefix}is_even";
74  $keyIsLast = "{$prefix}is_last";
75 
76  $count = count($array);
77  // this will force Iterator to load
78  $index = 0;
79  $isEven = false;
80  foreach ($array as $key => $element) {
81  if (is_object($element)) {
82  $this->_decorateArrayObject($element, $keyIsFirst, 0 === $index, $forceSetAll || 0 === $index);
83  $this->_decorateArrayObject($element, $keyIsOdd, !$isEven, $forceSetAll || !$isEven);
84  $this->_decorateArrayObject($element, $keyIsEven, $isEven, $forceSetAll || $isEven);
85  $isEven = !$isEven;
86  $index++;
87  $this->_decorateArrayObject(
88  $element,
89  $keyIsLast,
90  $index === $count,
91  $forceSetAll || $index === $count
92  );
93  } elseif (is_array($element)) {
94  if ($forceSetAll || 0 === $index) {
95  $array[$key][$keyIsFirst] = 0 === $index;
96  }
97  if ($forceSetAll || !$isEven) {
98  $array[$key][$keyIsOdd] = !$isEven;
99  }
100  if ($forceSetAll || $isEven) {
101  $array[$key][$keyIsEven] = $isEven;
102  }
103  $isEven = !$isEven;
104  $index++;
105  if ($forceSetAll || $index === $count) {
106  $array[$key][$keyIsLast] = $index === $count;
107  }
108  }
109  }
110  return $array;
111  }
112 
122  private function _decorateArrayObject($element, $key, $value, $isSkipped)
123  {
124  if ($isSkipped && $element instanceof \Magento\Framework\DataObject) {
125  $element->setData($key, $value);
126  }
127  }
128 
156  public function flatten(array $data, $path = '', $separator = '/')
157  {
158  $result = [];
159  $path = $path ? $path . $separator : '';
160 
161  foreach ($data as $key => $value) {
162  $fullPath = $path . $key;
163 
164  if (!is_array($value)) {
165  $result[$fullPath] = $value;
166 
167  continue;
168  }
169 
170  $result = array_merge(
171  $result,
172  $this->flatten($value, $fullPath, $separator)
173  );
174  }
175 
176  return $result;
177  }
178 
187  public function recursiveDiff(array $originalArray, array $newArray)
188  {
189  $diff = [];
190 
191  foreach ($originalArray as $key => $value) {
192  if (array_key_exists($key, $newArray)) {
193  if (is_array($value)) {
194  $valueDiff = $this->recursiveDiff($value, $newArray[$key]);
195  if (count($valueDiff)) {
196  $diff[$key] = $valueDiff;
197  }
198  } else {
199  if ($value != $newArray[$key]) {
200  $diff[$key] = $value;
201  }
202  }
203  } else {
204  $diff[$key] = $value;
205  }
206  }
207 
208  return $diff;
209  }
210 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
ksortMultibyte(array &$sort, $locale)
Definition: ArrayUtils.php:23
recursiveDiff(array $originalArray, array $newArray)
Definition: ArrayUtils.php:187
decorateArray($array, $prefix='decorated_', $forceSetAll=false)
Definition: ArrayUtils.php:64
$index
Definition: list.phtml:44
flatten(array $data, $path='', $separator='/')
Definition: ArrayUtils.php:156
$element
Definition: element.phtml:12