Update 2024-09-16 19:29:10

This commit is contained in:
Robot 2024-09-16 19:29:37 +02:00
parent 3efc0de510
commit d1e605dcd3
Signed by untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
3 changed files with 86 additions and 6 deletions

View File

@ -34,6 +34,7 @@ class UsersSubform << (F,LightGreen) >> #RoyalBlue {
- extractUserDetails(array $item, ?User $user) : array - extractUserDetails(array $item, ?User $user) : array
- assignUserGroups($details, ?User $user, ...) : void - assignUserGroups($details, ?User $user, ...) : void
- saveUserDetails(array $details, int $userId) : int - saveUserDetails(array $details, int $userId) : int
- isMultipleSets(array $array) : bool
} }
note right of UsersSubform::__construct note right of UsersSubform::__construct
@ -60,6 +61,7 @@ note right of UsersSubform::get
string $linkKey string $linkKey
string $field string $field
array $get array $get
bool $multi = true
end note end note
note left of UsersSubform::set note left of UsersSubform::set
@ -127,6 +129,7 @@ and sets them by association with a specified key and an incrementing integer.
array $items array $items
array $keySet array $keySet
string $field string $field
bool $multi
end note end note
note right of UsersSubform::process note right of UsersSubform::process
@ -190,6 +193,13 @@ note right of UsersSubform::saveUserDetails
since: 5.0.2 since: 5.0.2
return: int return: int
end note end note
note left of UsersSubform::isMultipleSets
Function to determine if the array consists of multiple data sets (arrays of arrays).
since: 5.0.2
return: bool
end note
@enduml @enduml
``` ```

View File

@ -121,18 +121,20 @@ final class UsersSubform implements GuidInterface, SubformInterface
* @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkKey The link key on which the items where linked in the child table.
* @param string $field The parent field name of the subform in the parent view. * @param string $field The parent field name of the subform in the parent view.
* @param array $get The array get:set of the keys of each row in the subform. * @param array $get The array get:set of the keys of each row in the subform.
* @param bool $multi The switch to return a multiple set.
* *
* @return array|null The subform * @return array|null The subform
* @since 3.2.2 * @since 3.2.2
*/ */
public function get(string $linkValue, string $linkKey, string $field, array $get): ?array public function get(string $linkValue, string $linkKey, string $field, array $get, bool $multi = true): ?array
{ {
if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null) if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null)
{ {
return $this->converter( return $this->converter(
$this->getUsersDetails($items), $this->getUsersDetails($items),
$get, $get,
$field $field,
$multi
); );
} }
@ -303,11 +305,12 @@ final class UsersSubform implements GuidInterface, SubformInterface
* @param array $items Array of objects or arrays to be filtered. * @param array $items Array of objects or arrays to be filtered.
* @param array $keySet Array of keys to retain in each item. * @param array $keySet Array of keys to retain in each item.
* @param string $field The field prefix for the resulting associative array. * @param string $field The field prefix for the resulting associative array.
* @param bool $multi The switch to return a multiple set.
* *
* @return array Array of filtered arrays set by association. * @return array Array of filtered arrays set by association.
* @since 3.2.2 * @since 3.2.2
*/ */
private function converter(array $items, array $keySet, string $field): array private function converter(array $items, array $keySet, string $field, bool $multi): array
{ {
/** /**
* Filters keys for a single item and converts it to an array. * Filters keys for a single item and converts it to an array.
@ -333,6 +336,10 @@ final class UsersSubform implements GuidInterface, SubformInterface
$result = []; $result = [];
foreach ($items as $index => $item) foreach ($items as $index => $item)
{ {
if (!$multi)
{
return $filterKeys($item, $keySet);
}
$filteredArray = $filterKeys($item, $keySet); $filteredArray = $filterKeys($item, $keySet);
$result[$field . $index] = $filteredArray; $result[$field . $index] = $filteredArray;
} }
@ -354,6 +361,11 @@ final class UsersSubform implements GuidInterface, SubformInterface
private function process($items, string $indexKey, string $linkKey, string $linkValue): array private function process($items, string $indexKey, string $linkKey, string $linkValue): array
{ {
$items = is_array($items) ? $items : []; $items = is_array($items) ? $items : [];
if ($items !== [] && !$this->isMultipleSets($items))
{
$items = [$items];
}
foreach ($items as $n => &$item) foreach ($items as $n => &$item)
{ {
$value = $item[$indexKey] ?? ''; $value = $item[$indexKey] ?? '';
@ -574,6 +586,29 @@ final class UsersSubform implements GuidInterface, SubformInterface
} }
return 0; return 0;
}
/**
* Function to determine if the array consists of multiple data sets (arrays of arrays).
*
* @param array $array The input array to be checked.
*
* @return bool True if the array contains only arrays (multiple data sets), false otherwise.
* @since 5.0.2
*/
private function isMultipleSets(array $array): bool
{
foreach ($array as $element)
{
// As soon as we find a non-array element, return false
if (!is_array($element))
{
return false;
}
}
// If all elements are arrays, return true
return true;
} }
} }

View File

@ -89,18 +89,20 @@
* @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkKey The link key on which the items where linked in the child table.
* @param string $field The parent field name of the subform in the parent view. * @param string $field The parent field name of the subform in the parent view.
* @param array $get The array get:set of the keys of each row in the subform. * @param array $get The array get:set of the keys of each row in the subform.
* @param bool $multi The switch to return a multiple set.
* *
* @return array|null The subform * @return array|null The subform
* @since 3.2.2 * @since 3.2.2
*/ */
public function get(string $linkValue, string $linkKey, string $field, array $get): ?array public function get(string $linkValue, string $linkKey, string $field, array $get, bool $multi = true): ?array
{ {
if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null) if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null)
{ {
return $this->converter( return $this->converter(
$this->getUsersDetails($items), $this->getUsersDetails($items),
$get, $get,
$field $field,
$multi
); );
} }
@ -271,11 +273,12 @@
* @param array $items Array of objects or arrays to be filtered. * @param array $items Array of objects or arrays to be filtered.
* @param array $keySet Array of keys to retain in each item. * @param array $keySet Array of keys to retain in each item.
* @param string $field The field prefix for the resulting associative array. * @param string $field The field prefix for the resulting associative array.
* @param bool $multi The switch to return a multiple set.
* *
* @return array Array of filtered arrays set by association. * @return array Array of filtered arrays set by association.
* @since 3.2.2 * @since 3.2.2
*/ */
private function converter(array $items, array $keySet, string $field): array private function converter(array $items, array $keySet, string $field, bool $multi): array
{ {
/** /**
* Filters keys for a single item and converts it to an array. * Filters keys for a single item and converts it to an array.
@ -301,6 +304,10 @@
$result = []; $result = [];
foreach ($items as $index => $item) foreach ($items as $index => $item)
{ {
if (!$multi)
{
return $filterKeys($item, $keySet);
}
$filteredArray = $filterKeys($item, $keySet); $filteredArray = $filterKeys($item, $keySet);
$result[$field . $index] = $filteredArray; $result[$field . $index] = $filteredArray;
} }
@ -322,6 +329,11 @@
private function process($items, string $indexKey, string $linkKey, string $linkValue): array private function process($items, string $indexKey, string $linkKey, string $linkValue): array
{ {
$items = is_array($items) ? $items : []; $items = is_array($items) ? $items : [];
if ($items !== [] && !$this->isMultipleSets($items))
{
$items = [$items];
}
foreach ($items as $n => &$item) foreach ($items as $n => &$item)
{ {
$value = $item[$indexKey] ?? ''; $value = $item[$indexKey] ?? '';
@ -542,4 +554,27 @@
} }
return 0; return 0;
}
/**
* Function to determine if the array consists of multiple data sets (arrays of arrays).
*
* @param array $array The input array to be checked.
*
* @return bool True if the array contains only arrays (multiple data sets), false otherwise.
* @since 5.0.2
*/
private function isMultipleSets(array $array): bool
{
foreach ($array as $element)
{
// As soon as we find a non-array element, return false
if (!is_array($element))
{
return false;
}
}
// If all elements are arrays, return true
return true;
} }