Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/property-level-missing_as_null #25

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/DataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public static function from(iterable|object|null $context = []): self
$self->{$methods[$property_name]}($context[$context_key] ?? null, $context, $Attribute, $ReflectionProperty);
continue;
}

/** When a property name does not match a key name */
if (!array_key_exists($context_key, $context)) {
if (isset($Describe->default)) {
Expand All @@ -250,6 +249,10 @@ public static function from(iterable|object|null $context = []): self
if (isset($Describe->required) && $Describe->required) {
throw new PropertyRequiredException("Property: $property_name is required");
}
if (isset($Describe->missing_as_null) && $Describe?->missing_as_null) {
$self->{$property_name} = null;
continue;
}
if (isset($ClassDescribe->missing_as_null) && $ClassDescribe?->missing_as_null) {
$self->{$property_name} = null;
continue;
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Metadata/MissingAsNull/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Tests\Unit\Metadata\MissingAsNull;

use Zerotoprod\DataModel\DataModel;
use Zerotoprod\DataModel\Describe;

#[Describe(['missing_as_null' => true])]
class User
{
use \Zerotoprod\DataModel\DataModel;
use DataModel;

public ?string $name;

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Metadata/MissingAsNullProperty/MissingAsNullTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Metadata\MissingAsNullProperty;

use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class MissingAsNullTest extends TestCase
{
#[Test] public function from(): void
{
$User = User::from();

$this->assertNull($User->name);
$this->assertEquals(2, $User->age);
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Metadata/MissingAsNullProperty/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Metadata\MissingAsNullProperty;

use Zerotoprod\DataModel\Describe;

//#[Describe(['missing_as_null' => true])]
class User
{
use \Zerotoprod\DataModel\DataModel;

#[Describe(['missing_as_null' => true])]
public ?string $name;

#[Describe(['default' => 2])]
public ?int $age;
}