Skip to content

Commit

Permalink
Fix #774
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jun 13, 2024
1 parent c49ce35 commit 3d70e8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Normalizers/Normalized/NormalizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ public function getProperty(string $name, DataProperty $dataProperty): mixed
{
$propertyName = $this->model::$snakeAttributes ? Str::snake($name) : $name;

return $this->properties[$propertyName] ?? $this->fetchNewProperty($propertyName, $dataProperty);
$value = array_key_exists($propertyName, $this->properties)
? $this->properties[$propertyName]
: $this->fetchNewProperty($propertyName, $dataProperty);

if ($value === null && ! $dataProperty->type->isNullable) {
return UnknownProperty::create();
}

return $value;
}

protected function initialize(Model $model): void
Expand Down
9 changes: 9 additions & 0 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
'boolean' => true,
'date' => CarbonImmutable::create(2020, 05, 16, 12, 00, 00),
'nullable_date' => null,
'nullable_optional_date' => null,
]);

$dataClass = new class () extends Data {
Expand All @@ -245,6 +246,10 @@
public Carbon $date;

public ?Carbon $nullable_date;

public Optional|Carbon $optional_date;

public Optional|null|Carbon $nullable_optional_date;
};

$data = $dataClass::from(DummyModel::findOrFail($model->id));
Expand All @@ -253,9 +258,13 @@
->string->toEqual('test')
->boolean->toBeTrue()
->nullable_date->toBeNull()
->optional_date->toBeInstanceOf(Optional::class)
->nullable_optional_date->toBeNull()
->and(CarbonImmutable::create(2020, 05, 16, 12, 00, 00)->eq($data->date))->toBeTrue();
});



it('can create a data object from a stdClass object', function () {
$object = (object) [
'string' => 'test',
Expand Down
4 changes: 4 additions & 0 deletions tests/Fakes/Models/DummyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class DummyModel extends Model
protected $casts = [
'date' => 'datetime',
'nullable_date' => 'datetime',
'optional_date' => 'datetime',
'nullable_optional_date' => 'datetime',
'boolean' => 'boolean',
];

Expand All @@ -22,6 +24,8 @@ public static function migrate()
$blueprint->string('string');
$blueprint->dateTime('date');
$blueprint->dateTime('nullable_date')->nullable();
$blueprint->dateTime('optional_date')->nullable();
$blueprint->dateTime('nullable_optional_date')->nullable();
$blueprint->boolean('boolean');

$blueprint->timestamps();
Expand Down

0 comments on commit 3d70e8a

Please sign in to comment.