Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ProcessMaker/Events/UserUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __construct(User $user, array $changes, array $original)
$this->user = $user;
$this->changes = array_diff_key($changes, array_flip($this::REMOVE_KEYS));
$this->original = array_diff_key($original, array_flip($this::REMOVE_KEYS));
$this->changes = ArrayHelper::replaceKeyInArray($changes, 'title', 'job_title');
$this->original = ArrayHelper::replaceKeyInArray($original, 'title', 'job_title');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions ProcessMaker/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ public static function getArrayDifferencesWithFormat(array $changedArray, array

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;
}
}
33 changes: 33 additions & 0 deletions tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,37 @@ public function testGetArrayDifferencesWithFormat()
]
));
}

public function testReplaceKeyInArray()
{
//Case 1: Existing Key
$myArray = [
'name' => 'John',
'title' => 'Musician'
];
$oldKey = 'title';
$newKey = 'job_title';
$this->assertEquals(
[
'name' => 'John',
'job_title' => 'Musician'
],
ArrayHelper::replaceKeyInArray($myArray, $oldKey, $newKey)
);

//Case 2: Not Existing Key
$myArray = [
'name' => 'John',
'title' => 'Musician'
];
$oldKey = 'titles';
$newKey = 'job_title';
$this->assertEquals(
[
'name' => 'John',
'title' => 'Musician'
],
ArrayHelper::replaceKeyInArray($myArray, $oldKey, $newKey)
);
}
}