Skip to content

Commit

Permalink
Merge pull request #505 from dbpolito/hidden
Browse files Browse the repository at this point in the history
Adding Hidden Attribute
  • Loading branch information
rubenvanassche authored Aug 9, 2023
2 parents d6ad2e4 + 69bb94a commit 31e9fae
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Attributes/Hidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\LaravelData\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Hidden
{
}
7 changes: 7 additions & 0 deletions src/Support/DataProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ReflectionProperty;
use Spatie\LaravelData\Attributes\Computed;
use Spatie\LaravelData\Attributes\GetsCast;
use Spatie\LaravelData\Attributes\Hidden;
use Spatie\LaravelData\Attributes\WithoutValidation;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Casts\Cast;
Expand All @@ -25,6 +26,7 @@ public function __construct(
public readonly DataType $type,
public readonly bool $validate,
public readonly bool $computed,
public readonly bool $hidden,
public readonly bool $isPromoted,
public readonly bool $isReadonly,
public readonly bool $hasDefaultValue,
Expand Down Expand Up @@ -66,6 +68,10 @@ public static function create(
fn (object $attribute) => $attribute instanceof Computed
);

$hidden = $attributes->contains(
fn (object $attribute) => $attribute instanceof Hidden
);

return new self(
name: $property->name,
className: $property->class,
Expand All @@ -74,6 +80,7 @@ className: $property->class,
fn (object $attribute) => $attribute instanceof WithoutValidation
) && ! $computed,
computed: $computed,
hidden: $hidden,
isPromoted: $property->isPromoted(),
isReadonly: $property->isReadOnly(),
hasDefaultValue: $property->isPromoted() ? $hasDefaultValue : $property->hasDefaultValue(),
Expand Down
4 changes: 4 additions & 0 deletions src/Transformers/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ protected function resolvePayload(TransformableData $data): array
$payload = [];

foreach ($dataClass->properties as $property) {
if ($property->hidden) {
continue;
}

$name = $property->name;

if (! $this->shouldIncludeProperty($name, $data->{$name}, $trees)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Inertia\LazyProp;
use Spatie\LaravelData\Attributes\Computed;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Hidden;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Attributes\WithCast;
Expand Down Expand Up @@ -2386,6 +2387,27 @@ public function __construct(
->toThrow(CannotSetComputedValue::class);
});

it('can have a hidden value', function () {
$dataObject = new class ('', '') extends Data {
public function __construct(
public string $show,
#[Hidden]
public string $hidden,
) {
}
};

expect($dataObject::from(['show' => 'Yes', 'hidden' => 'No']))
->show->toBe('Yes')
->hidden->toBe('No');

expect($dataObject::validateAndCreate(['show' => 'Yes', 'hidden' => 'No']))
->show->toBe('Yes')
->hidden->toBe('No');

expect($dataObject::from(['show' => 'Yes', 'hidden' => 'No'])->toArray())->toBe(['show' => 'Yes']);
});

it('throws a readable exception message when the constructor fails', function (
array $data,
string $message,
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/DataPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Spatie\LaravelData\Attributes\Computed;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Hidden;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Attributes\WithCast;
Expand Down Expand Up @@ -152,6 +153,21 @@ public function __construct(
)->toBeTrue();
});

it('can check if a property is hidden', function () {
expect(
resolveHelper(new class () {
public string $property;
})->hidden
)->toBeFalse();

expect(
resolveHelper(new class () {
#[Hidden]
public string $property;
})->hidden
)->toBeTrue();
});

it('wont throw an error if non existing attribute is used on a data class property', function () {
expect(NonExistingPropertyAttributeData::from(['property' => 'hello'])->property)->toEqual('hello')
->and(PhpStormAttributeData::from(['property' => 'hello'])->property)->toEqual('hello')
Expand Down

0 comments on commit 31e9fae

Please sign in to comment.