File tree Expand file tree Collapse file tree 5 files changed +70
-1
lines changed
Expand file tree Collapse file tree 5 files changed +70
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ Property Attributes
33
44In 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
3435This 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+
3660Aliases
3761-
3862
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Nuxtifyts \PhpDto \Attributes \Property ;
4+
5+ use Attribute ;
6+
7+ #[Attribute(Attribute::TARGET_PROPERTY )]
8+ class Hidden
9+ {
10+ }
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 99use Nuxtifyts \PhpDto \Attributes \Property \CipherTarget ;
1010use Nuxtifyts \PhpDto \Attributes \Property \Computed ;
1111use Nuxtifyts \PhpDto \Attributes \Property \DefaultsTo ;
12+ use Nuxtifyts \PhpDto \Attributes \Property \Hidden ;
1213use Nuxtifyts \PhpDto \Attributes \Property \WithRefiner ;
1314use Nuxtifyts \PhpDto \Contexts \Concerns \HasTypes ;
1415use 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 */
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments