-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathArrayHelper.php
More file actions
92 lines (80 loc) · 2.68 KB
/
ArrayHelper.php
File metadata and controls
92 lines (80 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace ProcessMaker\Helpers;
use ProcessMaker\Traits\FormatSecurityLogChanges;
use stdClass;
class ArrayHelper
{
use FormatSecurityLogChanges;
/**
* This method parse and stdClass Object to an Associative Array
* @param stdClass $parameter
* @return array
*/
public static function stdClassToArray(stdClass $parameter): array
{
$arrayConverted = json_decode(json_encode($parameter), true);
return $arrayConverted;
}
/**
* This method compares the keys of two arrays(new Values, old values) and adds (+) and (-) prefix
* to the keys which are different between the arrays with "formatChanges" Trait function.
* The method returns an array with different keys values, and new values only with (+) prefix.
* @param array $changedArray
* @param array $originalArray
* @return array
*/
public static function getArrayDifferencesWithFormat(array $changedArray, array $originalArray): array
{
$arrayDiff = [];
$displayChanges = self::customArrayDiffAssoc($changedArray, $originalArray);
$displayOriginal = self::customArrayDiffAssoc($originalArray, $changedArray);
$arrayHelper = new self();
$arrayDiff = $arrayHelper->formatChanges($displayChanges, $displayOriginal);
return $arrayDiff;
}
/**
* This method replace an old key with a new key in Array given.
* @param array $array
* @param string $oldKey
* @param string $newKey
* @return array
*/
public static function replaceKeyInArray(array $array, string $oldKey, string $newKey)
{
if (array_key_exists($oldKey, $array)) {
$array[$newKey] = $array[$oldKey];
unset($array[$oldKey]);
}
return $array;
}
/**
* This method is analogous to the function array_diff_assoc, with the difference
* that it supports the comparison of array-type elements in the content of the
* compared arrays.
*
* @param array $array1
* @param array $arrays
* @return array
*/
public static function customArrayDiffAssoc(array $array1, ...$arrays): array
{
if (empty($arrays) === 0) {
return $array1;
}
$diff = [];
foreach ($array1 as $key => $value) {
$found = false;
foreach ($arrays as $array) {
$sw = is_array($array) && isset($array[$key]) && $array[$key] === $value;
if ($sw) {
$found = true;
break;
}
}
if (!$found) {
$diff[$key] = $value;
}
}
return $diff;
}
}