Skip to content

Commit

Permalink
Make Multivalued replace much better
Browse files Browse the repository at this point in the history
Much better logic for replacement for my Favorite nerds.

- e.g if you pass now as "find" two Subjects and BOTH exist in the source (but other too) we will replace those two only...with whatever you pass. If you try to match e.g 3, all 3 match and your replacement is one, we will remove the 3 and push the new one at the end.

I think @aksm @alliomeria @karomabiles this is the way. Will showcase if anyone wants to see this tomorrow (please smile)

D
  • Loading branch information
DiegoPino committed Aug 10, 2022
1 parent 8ae62ab commit 521f52c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,47 @@ public function execute($entity = NULL) {
$patched = TRUE;
}
else {
// Now if original is indexed we do not know if the replacement is a value or also indexed
// So try first like original is indexed but each member is an array and compare...
foreach ($fullvaluesmodified[$key] as &$item) {
if ($item == $decoded_jsonfind[$key]) {
// Exact Array to Array 1:1 match
$item = $decoded_jsonreplace[$key];
$patched = TRUE;
}
}
// We are not going to do selective a few versus another?
// We can!
if (!$patched && is_array($decoded_jsonfind[$key]) && !StrawberryfieldJsonHelper::arrayIsMultiSimple($decoded_jsonfind[$key])) {
// Still we need to be sure ALL things to be searched for exist. A single difference means no patching
// So we traverse differently here
// Only if all needles are in the haystack
// we do the actual replacement.
$all_found = [];
$fullvaluesmodified_for_key_stashed = $fullvaluesmodified[$key];
foreach ($decoded_jsonfind[$key] as $item) {
// And to be sure we make a STRICT comparison
$found = array_search($item, $fullvaluesmodified[$key], TRUE);
if ($found !== FALSE) {
// Since we do not know if the Match/search for are more items than the replacements we will delete the found and add any new ones at the end.
unset($fullvaluesmodified[$key][$found]);
$patched = TRUE;
}
else {
$patched = FALSE;
break;
}
}
// If after this we decide we could not patch
// We return the original value.
if (!$patched) {
$fullvaluesmodified[$key] = $fullvaluesmodified_for_key_stashed;
}
else {
// Here we merge the already stripped from the Match pattern source with the replacements
$fullvaluesmodified[$key] = array_values(array_merge($fullvaluesmodified[$key], $decoded_jsonreplace[$key]));
}
}
}
}
else {
Expand Down

0 comments on commit 521f52c

Please sign in to comment.