Skip to content

Commit

Permalink
disable the behavior were excluded conditional properties still could…
Browse files Browse the repository at this point in the history
… be included
  • Loading branch information
rubenvanassche committed Dec 16, 2021
1 parent af0d92d commit bb4dc99
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/as-a-resource/lazy-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions src/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Transformers/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ protected function shouldIncludeProperty(
return true;
}

if ($value->isConditional()) {
return ($value->getCondition())();
}

if ($this->isPropertyExcluded($name, $excludes, $allowedExcludes)) {
return false;
}
Expand Down Expand Up @@ -116,7 +120,7 @@ protected function isPropertyIncluded(
return true;
}

if ($value->shouldInclude()) {
if ($value->isDefaultIncluded()) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 */
Expand Down

0 comments on commit bb4dc99

Please sign in to comment.