Skip to content

Commit

Permalink
fix filled & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Mar 17, 2023
1 parent 2c5f145 commit 415eff5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenSoutheners\LaravelDto;

use Exception;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -100,8 +101,12 @@ public function filled(string $property): bool

$reflection = new \ReflectionClass($this);

$reflectionProperty = $reflection->getProperty($classProperty);

$reflectionProperty = match (true) {
$reflection->hasProperty($property) => $reflection->getProperty($property),
$reflection->hasProperty($classProperty) => $reflection->getProperty($classProperty),
default => throw new Exception("Properties '{$property}' or '{$classProperty}' doesn't exists on class instance."),
};

$defaultValue = $reflectionProperty->getDefaultValue();
$propertyValue = $reflectionProperty->getValue($this);

Expand All @@ -111,11 +116,19 @@ public function filled(string $property): bool
return function_exists('filled') && filled($propertyValue);
}

/**
* Not filled when DTO property's default value is set to null while none is passed through
*/
if (! $propertyValue && $reflectionPropertyType->allowsNull() && $defaultValue === null) {
return false;
}

if ($reflectionProperty->getValue($this) === $defaultValue) {
/**
* Not filled when property isn't promoted and does have a default value matching value sent
*
* @see problem with promoted properties and hasDefaultValue/getDefaultValue https://bugs.php.net/bug.php?id=81386
*/
if (! $reflectionProperty->isPromoted() && $reflectionProperty->hasDefaultValue() && $propertyValue === $defaultValue) {
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Fixtures/CreatePostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

class CreatePostData extends DataTransferObject
{
public mixed $authorEmail = '[email protected]';

public function __construct(
public string $title,
public array|null $tags,
public PostStatus $postStatus,
public Post|null $post = null,
public array|string|null $country = null
public array|string|null $country = null,
public $description = '',
$authorEmail = null
) {
//
$this->authorEmail = $authorEmail;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ public function testDataTransferObjectFromArrayDelimitedLists()
$this->assertIsString($data->country);
}

public function testDataTransferObjectFilled()
public function testDataTransferObjectFilledViaClassProperties()
{
$data = CreatePostData::fromArray([
'title' => 'Hello world',
'tags' => '',
'post_status' => PostStatus::Published->value,
'author_email' => '[email protected]',
]);

$this->assertTrue($data->filled('tags'));
$this->assertTrue($data->filled('postStatus'));
$this->assertFalse($data->filled('post'));
$this->assertFalse($data->filled('description'));
$this->assertFalse($data->filled('author_email'));
}

public function testDataTransferObjectWithDefaults()
Expand Down

0 comments on commit 415eff5

Please sign in to comment.