From bb4dc99a05bd18b52db67a31aa55914da99b3f54 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Thu, 16 Dec 2021 16:06:52 +0100 Subject: [PATCH] disable the behavior were excluded conditional properties still could be included --- docs/as-a-resource/lazy-properties.md | 2 +- src/Data.php | 5 ----- src/Lazy.php | 18 ++++++++++-------- src/Transformers/DataTransformer.php | 6 +++++- tests/DataTest.php | 4 ++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/docs/as-a-resource/lazy-properties.md b/docs/as-a-resource/lazy-properties.md index af6c2240..e18a3bd6 100644 --- a/docs/as-a-resource/lazy-properties.md +++ b/docs/as-a-resource/lazy-properties.md @@ -155,7 +155,7 @@ Sometimes you only want to include a property when a specific condition is true. Lazy::when($this->is_admin, fn() => SongData::collection($album->songs)); ``` -The property will only be included when the `is_admin` property of the data object is true. +The property will only be included when the `is_admin` property of the data object is true. It is not possible to include the property later on with the `include` method when a condition is not accepted. ### Relational Lazy properties diff --git a/src/Data.php b/src/Data.php index 064c5529..41f5886d 100644 --- a/src/Data.php +++ b/src/Data.php @@ -27,11 +27,6 @@ abstract class Data implements Arrayable, Responsable, Jsonable, EloquentCastabl use AppendableData; use ValidateableData; - /** - * - Maybe add support for the dto package casts? - * - Add regex to dataproperty - */ - public static function optional($payload): ?static { return $payload === null diff --git a/src/Lazy.php b/src/Lazy.php index 8e0f1812..46d49585 100644 --- a/src/Lazy.php +++ b/src/Lazy.php @@ -48,17 +48,19 @@ public function condition(Closure $condition): self return $this; } - public function shouldInclude(): bool + public function isConditional(): bool { - if ($this->defaultIncluded) { - return true; - } + return $this->condition !== null; + } - if ($this->condition === null) { - return false; - } + public function getCondition(): Closure + { + return $this->condition; + } - return ($this->condition)(); + public function isDefaultIncluded(): bool + { + return $this->defaultIncluded ?? false; } public function resolve(): mixed diff --git a/src/Transformers/DataTransformer.php b/src/Transformers/DataTransformer.php index 0c5e29f2..1d673a18 100644 --- a/src/Transformers/DataTransformer.php +++ b/src/Transformers/DataTransformer.php @@ -75,6 +75,10 @@ protected function shouldIncludeProperty( return true; } + if ($value->isConditional()) { + return ($value->getCondition())(); + } + if ($this->isPropertyExcluded($name, $excludes, $allowedExcludes)) { return false; } @@ -116,7 +120,7 @@ protected function isPropertyIncluded( return true; } - if ($value->shouldInclude()) { + if ($value->isDefaultIncluded()) { return true; } diff --git a/tests/DataTest.php b/tests/DataTest.php index 4f9bd00f..27623c9a 100644 --- a/tests/DataTest.php +++ b/tests/DataTest.php @@ -219,7 +219,7 @@ public static function create(string $name): static } /** @test */ - public function it_can_have_conditional_lazy_data_manually_loaded() + public function it_cannot_have_conditional_lazy_data_manually_loaded() { $blueprint = new class () extends Data { public function __construct( @@ -237,7 +237,7 @@ public static function create(string $name): static $data = $blueprint::create('Freek'); - $this->assertEquals(['name' => 'Freek'], $data->include('name')->toArray()); + $this->assertEmpty($data->include('name')->toArray()); } /** @test */