Skip to content

Commit 6afad4f

Browse files
committed
Adjusted documentation
1 parent 2e50371 commit 6afad4f

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ final readonly class UserData extends Data
2727
}
2828
```
2929

30+
You can then create an instance of the class from a mixed value. The DTO will then attempt to hydrate the object with the given data.
31+
32+
```php
33+
$data = [
34+
'firstName' => 'John',
35+
'lastName' => 'Doe',
36+
'fullName' => 'John Doe'
37+
];
38+
39+
$user = UserData::from($data);
40+
```
41+
42+
DTOs can also be serialized to an array:
43+
44+
```php
45+
46+
$user = new UserData('John', 'Doe');
47+
48+
$userData = $user->toArray();
49+
50+
// Or you can use the json_encode function, as the DTO implements the JsonSerializable interface
51+
52+
$userData = json_encode($user);
53+
54+
```
55+
3056
### Note
3157

3258
This package was inspired from the [spatie/data-transfer-object](https://github.com/spatie/laravel-data) package.
@@ -41,5 +67,3 @@ and if you are using Laravel, I highly recommend using the original package. ins
4167

4268
- PHP 8.4 or higher
4369
- That's it!
44-
45-

src/Concerns/BaseData.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ final public function jsonSerialize(): array
112112
throw new SerializeException($e->getMessage(), $e->getCode(), $e);
113113
}
114114
}
115+
116+
/**
117+
* @return array<string, mixed>
118+
*
119+
* @throws SerializeException
120+
*/
121+
final public function toArray(): array
122+
{
123+
return $this->jsonSerialize();
124+
}
115125
}

tests/Dummies/ArrayOfMixedAttributesData.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
final readonly class ArrayOfMixedAttributesData extends Data
1212
{
13+
/**
14+
* @param ?array<array-key, int|YesNoBackedEnum> $arrayOfIntegersOrBackedEnums
15+
*/
1316
public function __construct(
1417
#[ArrayOfScalarTypes(Type::INT)]
1518
#[ArrayOfBackedEnums(YesNoBackedEnum::class)]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Dummies\DocsDummies;
4+
5+
use Nuxtifyts\PhpDto\Data;
6+
7+
final readonly class UserDetailsData extends Data
8+
{
9+
public string $fullName;
10+
11+
public function __construct(
12+
public string $firstName,
13+
public string $lastName
14+
) {
15+
$this->fullName = $this->firstName . ' ' . $this->lastName;
16+
}
17+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Documentation;
4+
5+
use Nuxtifyts\PhpDto\Data;
6+
use Nuxtifyts\PhpDto\Tests\Dummies\DocsDummies\UserDetailsData;
7+
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\Attributes\UsesClass;
11+
use Throwable;
12+
13+
#[UsesClass(UserDetailsData::class)]
14+
final class ReadmeExampleTest extends UnitCase
15+
{
16+
/**
17+
* @param class-string<Data> $dtoClass
18+
* @param array<string, mixed> $data
19+
* @param array<string, mixed> $expectedDtoProperties
20+
* @param array<string, mixed> $expectedSerializedProperties
21+
*
22+
* @throws Throwable
23+
*/
24+
#[Test]
25+
#[DataProvider('data_provider')]
26+
public function will_perform_serialize_and_deserialize_on_data_transfer_objects_from_docs(
27+
string $dtoClass,
28+
array $data,
29+
array $expectedDtoProperties,
30+
array $expectedSerializedProperties,
31+
): void {
32+
$dtoObject = $dtoClass::from($data);
33+
34+
foreach ($expectedDtoProperties as $property => $value) {
35+
self::assertEquals($value, $dtoObject->{$property});
36+
}
37+
38+
self::assertEquals(
39+
$expectedSerializedProperties,
40+
$dtoObject->jsonSerialize()
41+
);
42+
}
43+
44+
/**
45+
* @return array<string, array{
46+
* dtoClass: class-string<Data>,
47+
* data: array<string, mixed>,
48+
* expectedDtoProperties: array<string, mixed>,
49+
* expectedSerializedProperties: array<string, mixed>
50+
* }>
51+
*/
52+
public static function basic_examples_data_provider(): array
53+
{
54+
// @phpstan-ignore-next-line
55+
return [
56+
'First basic example' => [
57+
'dtoClass' => UserDetailsData::class,
58+
'data' => $data = [
59+
'firstName' => 'John',
60+
'lastName' => 'Doe',
61+
'fullName' => 'John Doe'
62+
],
63+
'expectedDtoProperties' => [
64+
'firstName' => 'John',
65+
'lastName' => 'Doe',
66+
'fullName' => 'John Doe'
67+
],
68+
'expectedSerializedProperties' => $data
69+
]
70+
];
71+
}
72+
}

tests/Unit/Serializers/ArraySerializerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,25 @@ public static function will_perform_data_serialization_on_array_types_data_provi
157157
],
158158
'propertyName' => 'arrayOfIntegersOrBackedEnums',
159159
'expectedDeserializedValue' => [YesNoBackedEnum::YES, YesNoBackedEnum::NO]
160+
],
161+
'Associated array of data' => [
162+
'object' => new ArrayOfAttributesData(
163+
arrayOfPersonData: [
164+
'john-doe' => new PersonData('John', 'Doe'),
165+
'jane-doe' => new PersonData('Jane', 'Doe'),
166+
]
167+
),
168+
'expectedSerializedValue' => [
169+
'arrayOfPersonData' => [
170+
'john-doe' => ['firstName' => 'John', 'lastName' => 'Doe', 'fullName' => 'John Doe'],
171+
'jane-doe' => ['firstName' => 'Jane', 'lastName' => 'Doe', 'fullName' => 'Jane Doe'],
172+
]
173+
],
174+
'propertyName' => 'arrayOfPersonData',
175+
'expectedDeserializedValue' => [
176+
'john-doe' => new PersonData('John', 'Doe'),
177+
'jane-doe' => new PersonData('Jane', 'Doe'),
178+
]
160179
]
161180
];
162181
}

0 commit comments

Comments
 (0)