diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md index f388fcf..8ac77c9 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md @@ -14,7 +14,7 @@ interface SubformInterface #Lavender { + table(string $table) : self + get(string $linkValue, string $linkKey, ...) : ?array - + set(array $items, string $indexKey, ...) : bool + + set(mixed $items, string $indexKey, ...) : bool + getTable() : string } @@ -35,7 +35,7 @@ note right of SubformInterface::get string $linkValue string $linkKey string $field - array $set + array $get end note note right of SubformInterface::set @@ -45,7 +45,7 @@ note right of SubformInterface::set return: bool arguments: - array $items + mixed $items string $indexKey string $linkKey string $linkValue diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php index ce42642..f80eea6 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php @@ -35,17 +35,17 @@ interface SubformInterface * @param string $linkValue The value of the link key in 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 array $set The array SET of the keys of each row in the subform. + * @param array $get The array SET of the keys of each row in the subform. * * @return array|null The subform * @since 3.2.2 */ - public function get(string $linkValue, string $linkKey, string $field, array $set): ?array; + public function get(string $linkValue, string $linkKey, string $field, array $get): ?array; /** * Set a subform items * - * @param array $items The list of items from the subform to set + * @param mixed $items The list of items from the subform to set * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete. * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -53,7 +53,7 @@ interface SubformInterface * @return bool * @since 3.2.2 */ - public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool; + public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool; /** * Get the current active table diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power index 85b5618..7d53f29 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power @@ -14,17 +14,17 @@ * @param string $linkValue The value of the link key in 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 array $set The array SET of the keys of each row in the subform. + * @param array $get The array SET of the keys of each row in the subform. * * @return array|null The subform * @since 3.2.2 */ - public function get(string $linkValue, string $linkKey, string $field, array $set): ?array; + public function get(string $linkValue, string $linkKey, string $field, array $get): ?array; /** * Set a subform items * - * @param array $items The list of items from the subform to set + * @param mixed $items The list of items from the subform to set * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete. * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -32,7 +32,7 @@ * @return bool * @since 3.2.2 */ - public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool; + public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool; /** * Get the current active table diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md b/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md index 6a115a3..d217e8e 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md @@ -17,11 +17,11 @@ class Subform << (F,LightGreen) >> #RoyalBlue { + __construct(Items $items, ?string $table = null) + table(string $table) : self + get(string $linkValue, string $linkKey, ...) : ?array - + set(array $items, string $indexKey, ...) : bool + + set(mixed $items, string $indexKey, ...) : bool + getTable() : string - purge(array $items, string $indexKey, ...) : void - converter(array $items, array $keySet, ...) : array - - process(array $items, string $indexKey, ...) : array + - process(mixed $items, string $indexKey, ...) : array - setGuid(string $key, bool $trim = true) : string } @@ -58,7 +58,7 @@ note left of Subform::set return: bool arguments: - array $items + mixed $items string $indexKey string $linkKey string $linkValue @@ -104,7 +104,7 @@ note left of Subform::process return: array arguments: - array $items + mixed $items string $indexKey string $linkKey string $linkValue diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php index 7aaeefe..a005218 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php @@ -94,7 +94,7 @@ final class Subform implements SubformInterface /** * Set a subform items * - * @param array $items The list of items from the subform to set + * @param mixed $items The list of items from the subform to set * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete. * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -102,12 +102,17 @@ final class Subform implements SubformInterface * @return bool * @since 3.2.2 */ - public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool + public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool { $items = $this->process($items, $indexKey, $linkKey, $linkValue); $this->purge($items, $indexKey, $linkKey, $linkValue); + if (empty($items)) + { + return true; // nothing to set (already purged) + } + return $this->items->table($this->getTable())->set( $items, $indexKey ); @@ -142,10 +147,19 @@ final class Subform implements SubformInterface if ($currentIndexValues !== null) { - // Extract the index values from the items array - $activeIndexValues = array_values(array_map(function($item) use ($indexKey) { - return $item[$indexKey] ?? null; - }, $items)); + // Check if the items array is empty + if (empty($items)) + { + // Set activeIndexValues to an empty array if items is empty + $activeIndexValues = []; + } + else + { + // Extract the index values from the items array + $activeIndexValues = array_values(array_map(function($item) use ($indexKey) { + return $item[$indexKey] ?? null; + }, $items)); + } // Find the index values that are no longer in the items array $inactiveIndexValues = array_diff($currentIndexValues, $activeIndexValues); @@ -205,7 +219,7 @@ final class Subform implements SubformInterface /** * Processes an array of arrays based on the specified key. * - * @param array $items Array of arrays to be processed. + * @param mixed $items Array of arrays to be processed. * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -213,8 +227,9 @@ final class Subform implements SubformInterface * @return array The processed array of arrays. * @since 3.2.2 */ - private function process(array $items, string $indexKey, string $linkKey, string $linkValue): array + private function process($items, string $indexKey, string $linkKey, string $linkValue): array { + $items = is_array($items) ? $items : []; foreach ($items as &$item) { $value = $item[$indexKey] ?? ''; diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power index c2dbcbb..9394b02 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power @@ -69,7 +69,7 @@ /** * Set a subform items * - * @param array $items The list of items from the subform to set + * @param mixed $items The list of items from the subform to set * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete. * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -77,12 +77,17 @@ * @return bool * @since 3.2.2 */ - public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool + public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool { $items = $this->process($items, $indexKey, $linkKey, $linkValue); $this->purge($items, $indexKey, $linkKey, $linkValue); + if (empty($items)) + { + return true; // nothing to set (already purged) + } + return $this->items->table($this->getTable())->set( $items, $indexKey ); @@ -117,10 +122,19 @@ if ($currentIndexValues !== null) { - // Extract the index values from the items array - $activeIndexValues = array_values(array_map(function($item) use ($indexKey) { - return $item[$indexKey] ?? null; - }, $items)); + // Check if the items array is empty + if (empty($items)) + { + // Set activeIndexValues to an empty array if items is empty + $activeIndexValues = []; + } + else + { + // Extract the index values from the items array + $activeIndexValues = array_values(array_map(function($item) use ($indexKey) { + return $item[$indexKey] ?? null; + }, $items)); + } // Find the index values that are no longer in the items array $inactiveIndexValues = array_diff($currentIndexValues, $activeIndexValues); @@ -180,7 +194,7 @@ /** * Processes an array of arrays based on the specified key. * - * @param array $items Array of arrays to be processed. + * @param mixed $items Array of arrays to be processed. * @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete * @param string $linkKey The link key on which the items where linked in the child table. * @param string $linkValue The value of the link key in child table. @@ -188,8 +202,9 @@ * @return array The processed array of arrays. * @since 3.2.2 */ - private function process(array $items, string $indexKey, string $linkKey, string $linkValue): array + private function process($items, string $indexKey, string $linkKey, string $linkValue): array { + $items = is_array($items) ? $items : []; foreach ($items as &$item) { $value = $item[$indexKey] ?? '';