Skip to content

Commit 11c38d3

Browse files
authored
Merge pull request #12 from nuxtifyts/feature/library-config
Introducing Data Configuration
2 parents d0c4ede + 6e524ab commit 11c38d3

31 files changed

+861
-272
lines changed

clover.xml

Lines changed: 287 additions & 200 deletions
Large diffs are not rendered by default.

docs/CloneableData.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ $todoWithDueDate = $todo->with(
5353
);
5454
```
5555

56-
> We are using the `empty` method
56+
> **Note:** We are using the `empty` method
5757
> from [Empty Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/EmptyData.md)
5858
> here
5959
60-
> `emptyTodo`, `todo` and `todoWithDueDate` are all different instances.
60+
> **Important:** `emptyTodo`, `todo` and `todoWithDueDate` are all different instances.
6161
6262
Computed properties
6363
-

docs/DataConfiguration.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Data Configuration
2+
=
3+
4+
The library uses a configuration based approach to define, load and save data.
5+
It uses a `DataConfiguration` object to define many things:
6+
7+
```php
8+
use Nuxtifyts\PhpDto\Configuration\DataConfiguration;
9+
10+
$config = DataConfiguration::getInstance();
11+
```
12+
13+
The function's signature is:
14+
15+
| Argument | Type | Description |
16+
|-------------|---------------|-------------------------------------------------------------------------------------------------|
17+
| config | array \| null | The configuration array to load, by default it's `null`, which means switch to default configs. |
18+
| forceCreate | bool | If `true`, it will create a new instance of `DataConfiguration` even if it's already created. |
19+
20+
If nothing is passed, it will be the equivalent of:
21+
22+
```php
23+
DataConfiguration::getInstance([
24+
'normalizers' => [
25+
'baseNormalizers' => [
26+
JsonStringNormalizer::class,
27+
StdClassNormalizer::class,
28+
ArrayAccessNormalizer::class,
29+
ArrayNormalizer::class,
30+
],
31+
],
32+
33+
'serializers' => [
34+
'baseSerializers' => [
35+
ArraySerializer::class,
36+
DataSerializer::class,
37+
DateTimeSerializer::class,
38+
BackedEnumSerializer::class,
39+
ScalarTypeSerializer::class,
40+
]
41+
]
42+
])
43+
```

docs/DefaultValues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class UserConfigDataFallbackResolver implements FallbackResolver
7070
}
7171
```
7272

73-
>! When using `DefaultsTo` attribute, priority is given to the attribute instead of the parameter's default value.
73+
> When using `DefaultsTo` attribute, priority is given to the attribute instead of the parameter's default value.
7474
7575
If ever needed to create a new instance of a DTO with complex default value,
7676
using the constructor is no longer possible, instead, you can make use of the

docs/EmptyData.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ By calling the `empty()` method, we can create a new instance of the `TodoData`
3636
$emptyTodo = TodoData::empty();
3737
```
3838

39-
> This is really useful with [Cloneable Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/CloneableData.md)
39+
> **Note:** This is really useful with [Cloneable Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/CloneableData.md)
4040
4141
The `$emptyTodo` variable will contain the following data:
4242

docs/Normalizers.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ data into (preferable) an `array`/`ArrayAccess`.
77
By default, there are 4 normalizers:
88

99
- **JsonStringNormalizer** will cast json string.
10-
- **StrClassNormalizer** will cast stdObject.
10+
- **StdClassNormalizer** will cast stdObject.
1111
- **ArrayAccessNormalizer** will cast ArrayAccess.
1212
- **ArrayNormalizer** will cast array.
1313

14+
> **Note:** In order to adjust there default normalizers, for example, you don't need
15+
> to use the `StdClassNormalizer` and you want to simply remove it, you need
16+
> to [configure](https://github.com/nuxtifyts/php-dto/blob/main/docs/DataConfiguration.md)
17+
> the `DataConfiguration` object.
18+
1419
Custom normalizers:
1520
=
1621

docs/Quickstart.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,4 @@ can be found here:
8181
- [Data Refiners](https://github.com/nuxtifyts/php-dto/blob/main/docs/DataRefiners.md)
8282
- [Empty Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/EmptyData.md)
8383
- [Cloneable Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/CloneableData.md)
84+
- [Data Configuration](https://github.com/nuxtifyts/php-dto/blob/main/docs/DataConfiguration.md)

src/Concerns/BaseData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Nuxtifyts\PhpDto\Exceptions\DataCreationException;
77
use Nuxtifyts\PhpDto\Exceptions\DeserializeException;
88
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
9+
use Nuxtifyts\PhpDto\Normalizers\Concerns\HasNormalizers;
910
use Nuxtifyts\PhpDto\Pipelines\DeserializePipeline\DeserializePipeline;
1011
use Nuxtifyts\PhpDto\Pipelines\DeserializePipeline\DeserializePipelinePassable;
11-
use Nuxtifyts\PhpDto\Support\Traits\HasNormalizers;
1212
use ReflectionClass;
1313
use Throwable;
1414

src/Concerns/CloneableData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Nuxtifyts\PhpDto\Contexts\ClassContext;
66
use Nuxtifyts\PhpDto\Exceptions\DataCreationException;
7-
use Nuxtifyts\PhpDto\Support\Traits\HasNormalizers;
7+
use Nuxtifyts\PhpDto\Normalizers\Concerns\HasNormalizers;
88
use ReflectionClass;
99
use Throwable;
1010

@@ -36,7 +36,7 @@ public function with(mixed ...$args): static
3636
? $this->cloneInstanceWithConstructorCall($context, $value)
3737
: $this->cloneInstanceWithoutConstructorCall($context, $value);
3838
} catch (Throwable $t) {
39-
throw DataCreationException::unableToCloneInstanceWithNewData($t);
39+
throw DataCreationException::unableToCloneInstanceWithNewData(static::class, $t);
4040
}
4141
}
4242

src/Configuration/Configuration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Configuration;
4+
5+
use Nuxtifyts\PhpDto\Exceptions\DataConfigurationException;
6+
7+
interface Configuration
8+
{
9+
/**
10+
* @param ?array<string, mixed> $config
11+
*
12+
* @throws DataConfigurationException
13+
*/
14+
public static function getInstance(
15+
?array $config = null,
16+
bool $forceCreate = false
17+
): self;
18+
}

0 commit comments

Comments
 (0)