1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 06:14:04 +00:00

allow defining a mix of attribute names and default values

This commit is contained in:
Abrar Ahmad 2022-09-28 13:38:01 +05:00
parent 5b34b0283a
commit e1229ef446
2 changed files with 72 additions and 6 deletions

View file

@ -129,12 +129,32 @@ class UpdateSyncedResource extends QueueableListener
}
if (Arr::isAssoc($model->getSyncedCreationAttributes())) {
// Developer provided the default values
// Developer provided the default values (key => value) or mix of default values and attribute names (values only)
// We will merge the default values with sync attributes
return array_merge($model->getSyncedCreationAttributes(), $model->only($model->getSyncedAttributeNames()));
[$attributes, $defaultValues] = $this->getAttributeNamesAndDefaultValues($model);
return array_merge($defaultValues, $model->only(array_merge($model->getSyncedAttributeNames(), $attributes)));
}
// Developer provided the attribute names, so we'd use them to pick model attributes
return $model->only($model->getSyncedCreationAttributes());
}
/**
* Split the attribute names (sequential index items) and default values (key => values).
*/
protected function getAttributeNamesAndDefaultValues(Syncable $model): array
{
$syncedCreationAttributes = $model->getSyncedCreationAttributes();
$attributes = Arr::where($syncedCreationAttributes, function ($value, $key) {
return is_numeric($key);
});
$defaultValues = Arr::where($syncedCreationAttributes, function ($value, $key) {
return is_string($key);
});
return [$attributes, $defaultValues];
}
}