mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-08 01:14:34 +02:00
Merge tags and bool fields of parts
This commit is contained in:
parent
e7b766906d
commit
478d5e2a3a
3 changed files with 112 additions and 1 deletions
|
@ -153,6 +153,54 @@ trait EntityMergerHelperTrait
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an OR operation on the boolean values from the target and the other entity for the given field.
|
||||
* This effectively means that the value is true, if it is true in at least one of the entities.
|
||||
* @param object $target
|
||||
* @param object $other
|
||||
* @param string $field
|
||||
* @return object
|
||||
*/
|
||||
protected function useTrueValue(object $target, object $other, string $field): object
|
||||
{
|
||||
return $this->useCallback(
|
||||
function (bool $target_value, bool $other_value): bool {
|
||||
return $target_value || $other_value;
|
||||
},
|
||||
$target,
|
||||
$other,
|
||||
$field
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a merge of comma separated lists from the target and the other entity for the given field.
|
||||
* The values are merged and duplicates are removed.
|
||||
* @param object $target
|
||||
* @param object $other
|
||||
* @param string $field
|
||||
* @return object
|
||||
*/
|
||||
protected function mergeTags(object $target, object $other, string $field, string $separator = ','): object
|
||||
{
|
||||
return $this->useCallback(
|
||||
function (string $t, string $o) use ($separator): string {
|
||||
//Explode the strings into arrays
|
||||
$t_array = explode($separator, $t);
|
||||
$o_array = explode($separator, $o);
|
||||
|
||||
//Merge the arrays and remove duplicates
|
||||
$tmp = array_unique(array_merge($t_array, $o_array));
|
||||
|
||||
//Implode the array back to a string
|
||||
return implode($separator, $tmp);
|
||||
},
|
||||
$target,
|
||||
$other,
|
||||
$field
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge the collections from the target and the other entity for the given field and put all items into the target collection.
|
||||
* @param object $target
|
||||
|
|
|
@ -55,7 +55,12 @@ class PartMerger implements EntityMergerInterface
|
|||
//We assume that the higher value is the correct one for minimum instock
|
||||
$this->useLargerValue($target, $other, 'minamount');
|
||||
|
||||
//We assume that a part needs review and is a favorite if one of the parts is
|
||||
$this->useTrueValue($target, $other, 'needs_review');
|
||||
$this->useTrueValue($target, $other, 'favorite');
|
||||
|
||||
//Merge the tags using the tag merger
|
||||
$this->mergeTags($target, $other, 'tags');
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue