public function typecastSaveRow(Model $m, $row)
{
if (!$row) {
return $row;
}
$result = [];
foreach ($row as $key => $value) {
// Look up field object
$f = $m->hasElement($key);
// Figure out the name of the destination field
$field = $f->actual ?: $key;
// We have no knowledge of the field, it wasn't defined, so
// we will leave it as-is.
if (!$f) {
$result[$field] = $value;
continue;
}
// check null values for mandatory fields
if ($value === null && $f->mandatory) {
throw new Exception(['Mandatory field value cannot be null', 'field' => $key]);
}
// Expression and null cannot be converted.
if ($value instanceof \atk4\dsql\Expression || $value instanceof \atk4\dsql\Expressionable || $value === null) {
$result[$field] = $value;
continue;
}
// typecast if we explicitly want that or there is not serialization enabled
if ($f->typecast || $f->typecast === null && $f->serialize === null) {
$value = $this->typecastSaveField($f, $value);
}
// serialize if we explicitly want that
if ($f->serialize) {
$value = $this->serializeSaveField($f, $value);
}
// store converted value
$result[$field] = $value;
}
return $result;
}