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

Allow to fetch union data properties in unified way #403

Merged
merged 2 commits into from
Apr 7, 2023
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
10 changes: 10 additions & 0 deletions src/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ public function isDefaultIncluded(): bool
{
return $this->defaultIncluded ?? false;
}

public function __get(string $name): mixed
{
return $this->resolve()->$name;
}

public function __call(string $name, array $arguments): mixed
{
return call_user_func_array([$this->resolve(), $name], $arguments);
}
}
19 changes: 19 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Spatie\LaravelData\Tests\Fakes\Transformers\ConfidentialDataTransformer;
use Spatie\LaravelData\Tests\Fakes\Transformers\StringToUpperTransformer;
use Spatie\LaravelData\Tests\Fakes\UlarData;
use Spatie\LaravelData\Tests\Fakes\UnionData;
use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer;
use Spatie\LaravelData\WithData;

Expand Down Expand Up @@ -2278,3 +2279,21 @@ public function __construct(
expect($invaded->_except)->toBeEmpty();
expect($invaded->_wrap)->toBeNull();
});

it('can fetch lazy union data', function () {
$data = UnionData::from(1);

expect($data->id)->toBe(1);
expect($data->simple->string)->toBe('A');
expect($data->dataCollection->toCollection()->pluck('string')->toArray())->toBe(['B', 'C']);
expect($data->fakeModel->string)->toBe('lazy');
});

it('can fetch non-lazy union data', function () {
$data = UnionData::from('A');

expect($data->id)->toBe(1);
expect($data->simple->string)->toBe('A');
expect($data->dataCollection->toCollection()->pluck('string')->toArray())->toBe(['B', 'C']);
expect($data->fakeModel->string)->toBe('non-lazy');
});
45 changes: 45 additions & 0 deletions tests/Fakes/UnionData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Lazy;
use Spatie\LaravelData\Tests\Fakes\Models\FakeModel;

class UnionData extends Data
{
public function __construct(
public int $id,
public SimpleData|Lazy $simple,
#[DataCollectionOf(SimpleData::class)]
public DataCollection|Lazy $dataCollection,
public FakeModel|Lazy $fakeModel,
) {
}

public static function fromInt(int $id): self
{
return new self(
id: $id,
simple: Lazy::create(fn () => SimpleData::from('A')),
dataCollection: Lazy::create(fn () => SimpleData::collection(['B', 'C'])),
fakeModel: Lazy::create(fn () => FakeModel::factory()->create([
'string' => 'lazy',
])),
);
}

public static function fromString(string $name): self
{
return new self(
id: 1,
simple: SimpleData::from($name),
dataCollection: SimpleData::collection(['B', 'C']),
fakeModel: FakeModel::factory()->create([
'string' => 'non-lazy',
]),
);
}
}