Skip to content

Commit 35fed56

Browse files
committed
Added documentation for property attributes
1 parent 4457a96 commit 35fed56

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

docs/PropertyAttributes.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Property Attributes
2+
=
3+
4+
In order to provide more functionality to your DTOs, you can use the following attributes:
5+
- [Computed](#Computed) - To define a property that is computed from other properties.
6+
7+
Computed
8+
-
9+
10+
Sometimes, we may need to specify that a property is computed, meaning that it is derived from other properties.
11+
This can be done using the `Computed` attribute.
12+
13+
```php
14+
use Nuxtifyts\PhpDto\Data;
15+
use Nuxtifyts\PhpDto\Attributes\PropertyAttributes\Computed;
16+
17+
final readonly class Person extends Data
18+
{
19+
#[Computed]
20+
public string $fullName;
21+
22+
public function __construct(
23+
public string $firstName,
24+
public string $lastName
25+
) {
26+
$this->fullName = "$this->firstName $this->lastName";
27+
}
28+
}
29+
```
30+
31+
This will make the DTO aware of the `fullName` property, and it will not be serialized or deserialized.

docs/Quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ can be found here:
7878

7979
- [Supported Types](https://github.com/nuxtifyts/php-dto/blob/main/docs/SupportedTypes.md)
8080
- [Normalizers](https://github.com/nuxtifyts/php-dto/blob/main/docs/Normalizers.md)
81-
- [Property Attributes (TBD)](#)
81+
- [Property Attributes](https://github.com/nuxtifyts/php-dto/blob/main/docs/PropertyAttributes.md)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Documentation;
4+
5+
use Nuxtifyts\PhpDto\Attributes\PropertyAttributes\Computed;
6+
use Nuxtifyts\PhpDto\Data;
7+
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\Attributes\UsesClass;
10+
use Throwable;
11+
12+
#[UsesClass(Computed::class)]
13+
#[UsesClass(Data::class)]
14+
final class PropertyAttributesExampleTest extends UnitCase
15+
{
16+
/**
17+
* @throws Throwable
18+
*/
19+
#[Test]
20+
public function will_be_able_to_handle_data_classes_with_computed_properties(): void
21+
{
22+
$person = new readonly class ('John', 'Doe') extends Data {
23+
#[Computed]
24+
public string $fullName;
25+
26+
public function __construct(
27+
public string $firstName,
28+
public string $lastName
29+
) {
30+
$this->fullName = $this->firstName . ' ' . $this->lastName;
31+
}
32+
};
33+
34+
self::assertEquals(
35+
$serializedData = [
36+
'firstName' => 'John',
37+
'lastName' => 'Doe'
38+
],
39+
$person->toArray(),
40+
);
41+
42+
$personFrom = $person::from($serializedData);
43+
44+
self::assertEquals('John Doe', $personFrom->fullName);
45+
}
46+
}

0 commit comments

Comments
 (0)