Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Hidden Attribute #505

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading