Update 2024-07-11 23:10:31

This commit is contained in:
Robot 2024-07-11 22:13:51 +02:00
parent 67e3a5823d
commit 0160c64a63
Signed by untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
6 changed files with 61 additions and 31 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
);
@ -141,11 +146,20 @@ final class Subform implements SubformInterface
$currentIndexValues = $this->items->table($this->getTable())->values([$linkValue], $linkKey, $indexKey);
if ($currentIndexValues !== null)
{
// 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] ?? '';

View File

@ -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
);
@ -116,11 +121,20 @@
$currentIndexValues = $this->items->table($this->getTable())->values([$linkValue], $linkKey, $indexKey);
if ($currentIndexValues !== null)
{
// 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] ?? '';