Skip to content

Commit d6b0491

Browse files
committed
Added documentation
1 parent 00768c5 commit d6b0491

19 files changed

+836
-28
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ and if you are using Laravel, I highly recommend using the original package. ins
6767

6868
- PHP 8.4 or higher
6969
- That's it!
70+
71+
### Installation

src/Concerns/BaseData.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,4 @@ 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-
}
125115
}

src/Data.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,24 @@
44

55
use Nuxtifyts\PhpDto\Contracts\BaseData as BaseDataContract;
66
use Nuxtifyts\PhpDto\Concerns\BaseData;
7+
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
78

89
abstract readonly class Data implements BaseDataContract
910
{
1011
use BaseData;
12+
13+
/**
14+
* @return array<string, mixed>
15+
*
16+
* @throws SerializeException
17+
*/
18+
final public function toArray(): array
19+
{
20+
return $this->jsonSerialize();
21+
}
22+
23+
final public function toJson(): false|string
24+
{
25+
return json_encode($this);
26+
}
1127
}

src/Docs/Introduction.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Introduction
2+
=
3+
4+
This package enabled the creation of data objects which can be used in various ways.
5+
Using modern PHP syntax, it allows you to hydrate data from arrays, objects, and other data sources.
6+
As well as carrying out the data, type validation and serialize the data for any purpose.
7+
8+
To create a `data` class, you will need to declare a `readonly` class that extends `Data` class.
9+
Then you can define the properties of the class and their types.
10+
11+
```php
12+
use Nuxtifyts\PhpDto\Data;
13+
14+
final readonly class UserData extends Data
15+
{
16+
public string $fullName;
17+
18+
public function __construct(
19+
public string $firstName,
20+
public stirng $lastName
21+
) {
22+
$this->fullName = "$this->firstName $this->lastName";
23+
}
24+
}
25+
```
26+
27+
By extending from `Data` you enable the ability to serialize/deserialize a data object.
28+
29+
- Hydrating a data from a `mixed` value using normalizers
30+
- Serializing a data from a data object to an array or JSON.
31+
32+
Checkout the [Quickstart](#) guide to get started with the package.

src/Docs/Normalizers.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Normalizers
2+
=
3+
4+
When trying to hydrate a data object using the `from` function, the package will use normalizers to convert the passed
5+
data into (preferable) an `array`/`ArrayAccess`.
6+
7+
By default, there are 4 normalizers:
8+
9+
- **JsonStringNormalizer** will cast json string.
10+
- **StrClassNormalizer** will cast stdObject.
11+
- **ArrayAccessNormalizer** will cast ArrayAccess.
12+
- **ArrayNormalizer** will cast array.
13+
14+
Custom normalizers:
15+
=
16+
17+
Custom normalizers can be added if needed: To do this, you will need to extend the `Normalizer` class
18+
and implement the `normalize` method.
19+
20+
For example: we have a custom class called: `Goal`:
21+
22+
```php
23+
class Goal
24+
{
25+
public function __construct(
26+
public string $summary,
27+
public string $description,
28+
public DateTimeImmutable $dueDate
29+
) {
30+
}
31+
}
32+
```
33+
34+
And we have our `Todo` data class:
35+
36+
```php
37+
use Nuxtifyts\PhpDto\Data;
38+
39+
final readonly class Todo extends Data
40+
{
41+
public function __construct(
42+
public string $title,
43+
public string $content,
44+
public Status $status,
45+
public ?DateTimeImmutable $dueDate
46+
) {
47+
}
48+
}
49+
```
50+
51+
And we want to hydrate instances of this class to using a normalizer. first, we need to create
52+
a normalizer class, we'll call it `GoalTodoNormalizer`:
53+
54+
```php
55+
use Nuxtifyts\PhpDto\Normalizers\Normalizer;
56+
57+
final readonly class GoalTodoNormalizer extends Normalizer
58+
{
59+
public function normalize() : array|false{
60+
if (!$this->value instanceof Goal) {
61+
return false;
62+
}
63+
64+
return [
65+
'title' => $this->value->summary,
66+
'content' => $this->value->description,
67+
'status' => 'ready',
68+
'dueDate' => $this->value->dueDate->format(DateTimeInterface::ATOM)
69+
];
70+
}
71+
}
72+
```
73+
74+
Next step is to add this new normalizer to the todo class:
75+
76+
```php
77+
use Nuxtifyts\PhpDto\Data;
78+
79+
final readonly class Todo extends Data
80+
{
81+
// ...
82+
83+
protected static function normalizers() : array{
84+
return GoalTodoNormalizer::class;
85+
}
86+
}
87+
```
88+
89+
This will add the `GoalTodoNormalizer` on top of the default normalizers. Now it'll be possible to hydrate
90+
a `Todo` instance from a `Goal` instance.
91+
92+
```php
93+
$goal = new Goal(
94+
title: 'Learn PHP DTO',
95+
description: 'Learn how to use PHP DTO',
96+
dueDate: new DateTimeImmutable()
97+
);
98+
99+
$todo = Todo::from($goal);
100+
```

src/Docs/Quickstart.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Quickstart
2+
=
3+
4+
In this quickstart guide, you will learn the most basic usage of this package.
5+
6+
First, you should [install](#) the package using composer:
7+
8+
We will create a Todo application, where we need to manage a list of TODOs. so we can start
9+
with creating a `Todo` data class.
10+
11+
```php
12+
use Nuxtifyts\PhpDto\Data;
13+
14+
final readonly class Todo extends Data
15+
{
16+
public function __construct(
17+
public string $title,
18+
public string $content,
19+
public Status $status,
20+
public ?DateTimeImmutable $dueDate
21+
) {}
22+
}
23+
```
24+
25+
Extending from `Data` class is all you'll need to start using data transfer objects.
26+
You can define the properties of the class and their types.
27+
28+
In the above example, the `Status` a native `BackedEnum` class:
29+
30+
```php
31+
enum Status: string
32+
{
33+
case BACKLOG = 'backlog';
34+
case READY = 'ready';
35+
case IN_PROGRESS = 'in_progress';
36+
case DONE = 'done';
37+
}
38+
```
39+
40+
We can now create a new instance of the `Todo`:
41+
42+
```php
43+
$todo = new Todo(
44+
title: 'Learn PHP DTO',
45+
content: 'Learn how to use PHP DTO',
46+
status: Status::READY,
47+
dueDate: new DateTimeImmutable()
48+
)
49+
```
50+
51+
The package allows you to hydrate these data objects from other types, for example an array:
52+
53+
```php
54+
$todo = Todo::from([
55+
'title' => 'Learn PHP DTO',
56+
'content' => 'Learn how to use PHP DTO',
57+
'status' => 'ready',
58+
'dueDate' => '2025-01-01T00:00:00+00:00'
59+
]);
60+
```
61+
62+
You can also serialize the data object to an array, or a json string:
63+
64+
```php
65+
// Serialize to an array
66+
$todo->toArray();
67+
68+
// Serialize to a json string
69+
json_encode($todo); // or
70+
$todo->toJson();
71+
```
72+
73+
Conclusion
74+
-
75+
76+
This is the most basic usage of this package, more details on how to use the package
77+
can be found here:
78+
79+
- [Supported Types](#)
80+
- [Normalizers](#)
81+
- [Property Attributes (TBD)](#)

0 commit comments

Comments
 (0)