diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md index 2ad6e91..3046e41 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md @@ -34,6 +34,7 @@ class UsersSubform << (F,LightGreen) >> #RoyalBlue { - extractUserDetails(array $item, ?User $user) : array - assignUserGroups($details, ?User $user, ...) : void - saveUserDetails(array $details, int $userId) : int + - isMultipleSets(array $array) : bool } note right of UsersSubform::__construct @@ -60,6 +61,7 @@ note right of UsersSubform::get string $linkKey string $field array $get + bool $multi = true end note 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 $keySet string $field + bool $multi end note note right of UsersSubform::process @@ -190,6 +193,13 @@ note right of UsersSubform::saveUserDetails since: 5.0.2 return: int 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 ``` diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php index 841a3a7..93cdd90 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php @@ -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 $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 bool $multi The switch to return a multiple set. * * @return array|null The subform * @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) { return $this->converter( $this->getUsersDetails($items), $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 $keySet Array of keys to retain in each item. * @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. * @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. @@ -333,6 +336,10 @@ final class UsersSubform implements GuidInterface, SubformInterface $result = []; foreach ($items as $index => $item) { + if (!$multi) + { + return $filterKeys($item, $keySet); + } $filteredArray = $filterKeys($item, $keySet); $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 { $items = is_array($items) ? $items : []; + if ($items !== [] && !$this->isMultipleSets($items)) + { + $items = [$items]; + } + foreach ($items as $n => &$item) { $value = $item[$indexKey] ?? ''; @@ -574,6 +586,29 @@ final class UsersSubform implements GuidInterface, SubformInterface } 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; } } diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power index feca9f9..c09c887 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power @@ -89,18 +89,20 @@ * @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 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 * @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) { return $this->converter( $this->getUsersDetails($items), $get, - $field + $field, + $multi ); } @@ -271,11 +273,12 @@ * @param array $items Array of objects or arrays to be filtered. * @param array $keySet Array of keys to retain in each item. * @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. * @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. @@ -301,6 +304,10 @@ $result = []; foreach ($items as $index => $item) { + if (!$multi) + { + return $filterKeys($item, $keySet); + } $filteredArray = $filterKeys($item, $keySet); $result[$field . $index] = $filteredArray; } @@ -322,6 +329,11 @@ private function process($items, string $indexKey, string $linkKey, string $linkValue): array { $items = is_array($items) ? $items : []; + if ($items !== [] && !$this->isMultipleSets($items)) + { + $items = [$items]; + } + foreach ($items as $n => &$item) { $value = $item[$indexKey] ?? ''; @@ -542,4 +554,27 @@ } 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; } \ No newline at end of file