atk4\data\Persistence::typecastSaveRow PHP Method

typecastSaveRow() public method

In: [ 'name'=>' John Smith', 'age'=>30, 'password'=>'abc', 'is_married'=>true, ] Out: [ 'first_name'=>'John Smith', 'age'=>30, 'is_married'=>1 ]
public typecastSaveRow ( Model $m, array $row ) : array
$m Model
$row array
return array
    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;
    }