Skip to content

Commit 46cff4e

Browse files
committed
Add Hidden attribute to support non-serializable properties
Introduce a new `Hidden` attribute to mark properties that should be excluded from serialization. Updated `BaseData` and `PropertyContext` to handle this behavior, added a corresponding unit test, and documented the feature in `PropertyAttributes.md`.
1 parent 5ba9880 commit 46cff4e

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

docs/PropertyAttributes.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Property Attributes
33

44
In order to provide more functionality to your DTOs, you can use the following attributes:
55
- [Computed](#Computed) - To define a property that is computed from other properties.
6+
- [Hidden](#Hidden) - To define a property that should not be serialized.
67
- [Aliases](#Aliases) - To define aliases for a property.
78
- [DefaultsTo](#DefaultsTo) - To define a default value for a property using a fallback resolver.
89
- [CipherTarget](#CipherTarget) - To define a property that should be encrypted/decrypted.
@@ -33,6 +34,29 @@ final readonly class PersonData extends Data
3334

3435
This will make the DTO aware of the `fullName` property, and it will not be serialized or deserialized.
3536

37+
Hidden
38+
-
39+
40+
Sometimes, we may need to specify that a property should not be serialized.
41+
42+
This can be done using the `Hidden` attribute.
43+
44+
```php
45+
use Nuxtifyts\PhpDto\Attributes\Property\Hidden;
46+
use Nuxtifyts\PhpDto\Data;
47+
48+
final readonly class PersonData extends Data
49+
{
50+
public function __construct(
51+
public string $firstName,
52+
#[Hidden]
53+
public string $lastName
54+
) {}
55+
}
56+
```
57+
58+
When serializing the DTO, the `lastName` property will not be included in the output.
59+
3660
Aliases
3761
-
3862

src/Attributes/Property/Hidden.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Attributes\Property;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_PROPERTY)]
8+
class Hidden
9+
{
10+
}

src/Concerns/BaseData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ final public function jsonSerialize(): array
130130

131131
$serializedData = [];
132132
foreach ($context->properties as $propertyContext) {
133-
if ($propertyContext->isComputed) {
133+
if ($propertyContext->isComputed || $propertyContext->isHidden) {
134134
continue;
135135
}
136136

src/Contexts/PropertyContext.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Nuxtifyts\PhpDto\Attributes\Property\CipherTarget;
1010
use Nuxtifyts\PhpDto\Attributes\Property\Computed;
1111
use Nuxtifyts\PhpDto\Attributes\Property\DefaultsTo;
12+
use Nuxtifyts\PhpDto\Attributes\Property\Hidden;
1213
use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner;
1314
use Nuxtifyts\PhpDto\Contexts\Concerns\HasTypes;
1415
use Nuxtifyts\PhpDto\Data;
@@ -51,6 +52,8 @@ class PropertyContext
5152

5253
private(set) bool $isComputed = false;
5354

55+
private(set) bool $isHidden = false;
56+
5457
private(set) ?CipherConfig $cipherConfig = null;
5558

5659
private(set) ?FallbackConfig $fallbackConfig = null;
@@ -104,6 +107,7 @@ private static function getKey(ReflectionProperty $property): string
104107
private function syncPropertyAttributes(): void
105108
{
106109
$this->isComputed = !empty($this->reflection->getAttributes(Computed::class));
110+
$this->isHidden = !empty($this->reflection->getAttributes(Hidden::class));
107111

108112
foreach ($this->reflection->getAttributes(WithRefiner::class) as $withRefinerAttribute) {
109113
/** @var ReflectionAttribute<WithRefiner> $withRefinerAttribute */
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Attributes;
4+
5+
use Nuxtifyts\PhpDto\Attributes\Property\Hidden;
6+
use Nuxtifyts\PhpDto\Data;
7+
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use Throwable;
11+
12+
#[CoversClass(Hidden::class)]
13+
final class HiddenTest extends UnitCase
14+
{
15+
/**
16+
* @throws Throwable
17+
*/
18+
#[Test]
19+
public function test_it_can_hide_a_property(): void
20+
{
21+
$object = new readonly class ('Something that should be hidden') extends Data {
22+
public function __construct(
23+
#[Hidden]
24+
public string $hidden
25+
) {
26+
}
27+
};
28+
29+
self::assertEquals([], $object->toArray());
30+
}
31+
}

0 commit comments

Comments
 (0)