Skip to content

Commit

Permalink
Merge pull request #403 from Jampire/resolve_lazy_properties
Browse files Browse the repository at this point in the history
Allow to fetch union data properties in unified way
  • Loading branch information
rubenvanassche authored Apr 7, 2023
2 parents b429257 + 6a1e5f6 commit 557d4bf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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',
]),
);
}
}

0 comments on commit 557d4bf

Please sign in to comment.