Skip to content

Commit

Permalink
always normalize in typecastSaveField
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Nov 5, 2021
1 parent df2fbe1 commit 0bd0c07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ public function typecastSaveRow(Model $model, array $row): array
foreach ($row as $fieldName => $value) {
$field = $model->getField($fieldName);

// TODO move to value (instead of row) typecast
// TODO should we drop field normalization completely?
$value = $field->normalize($value);

$result[$field->getPersistenceName()] = $this->typecastSaveField($field, $value);
}

Expand Down Expand Up @@ -212,8 +208,13 @@ public function typecastLoadRow(Model $model, array $row): array
*
* @return scalar|Persistence\Sql\Expressionable|null
*/
public function typecastSaveField(Field $field, $value)
final public function typecastSaveField(Field $field, $value)
{
$prevFrame = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1] ?? [];
if (($prevFrame['object'] ?? null) !== $field || ($prevFrame['function'] ?? null) !== 'normalize') {
$value = $field->normalize($value);
}

if ($value === null) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function testEnum4(): void
$m = new Model();
$m->addField('foo', ['enum' => [1, 'bar'], 'default' => 1]);
$m = $m->createEntity();
$m->set('foo', null);
$m->setNull('foo');

$this->assertNull($m->get('foo'));
}
Expand Down

0 comments on commit 0bd0c07

Please sign in to comment.