generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #831 from spatie/auto-lazy
Auto lazy
- Loading branch information
Showing
13 changed files
with
568 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
use Spatie\LaravelData\Support\Lazy\ClosureLazy; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] | ||
class AutoClosureLazy extends AutoLazy | ||
{ | ||
public function build(Closure $castValue, mixed $payload, DataProperty $property, mixed $value): ClosureLazy | ||
{ | ||
return Lazy::closure(fn () => $castValue($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
use Spatie\LaravelData\Support\Lazy\InertiaLazy; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] | ||
class AutoInertiaLazy extends AutoLazy | ||
{ | ||
public function build(Closure $castValue, mixed $payload, DataProperty $property, mixed $value): InertiaLazy | ||
{ | ||
return Lazy::inertia(fn () => $castValue($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)] | ||
class AutoLazy | ||
{ | ||
public function build(Closure $castValue, mixed $payload, DataProperty $property, mixed $value): Lazy | ||
{ | ||
return Lazy::create(fn () => $castValue($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
use Spatie\LaravelData\Support\Lazy\ConditionalLazy; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class AutoWhenLoadedLazy extends AutoLazy | ||
{ | ||
public function __construct( | ||
public ?string $relation = null, | ||
) { | ||
} | ||
|
||
public function build(Closure $castValue, mixed $payload, DataProperty $property, mixed $value): ConditionalLazy | ||
{ | ||
$relation = $this->relation ?? $property->name; | ||
|
||
return Lazy::when(fn () => $payload->relationLoaded($relation), fn () => $castValue( | ||
$payload->getRelation($relation) | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.