-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.php
More file actions
107 lines (97 loc) · 3.15 KB
/
Copy pathConfiguration.php
File metadata and controls
107 lines (97 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace TinyBlocks\Mapper;
/**
* Per-call serialization options: key preservation, omitted fields, and null omission.
*/
final readonly class Configuration
{
private function __construct(
private array $omittedFields,
private KeyPreservation $keyPreservation,
private NullPreservation $nullPreservation
) {
}
/**
* Creates the default configuration: keys preserved, nothing omitted, null-valued properties kept.
*
* @return Configuration The default instance.
*/
public static function default(): Configuration
{
return new Configuration(
omittedFields: [],
keyPreservation: KeyPreservation::PRESERVE,
nullPreservation: NullPreservation::KEEP
);
}
/**
* Tells whether the given field is omitted from serialization.
*
* @param string $field The candidate property name.
* @return bool True when the field is omitted, false otherwise.
*/
public function omits(string $field): bool
{
return in_array($field, $this->omittedFields, true);
}
/**
* Returns a copy that omits the given properties from serialization.
*
* @param string ...$fields The property names to exclude from the output.
* @return Configuration The new instance.
*/
public function omitting(string ...$fields): Configuration
{
return new Configuration(
omittedFields: [...$this->omittedFields, ...$fields],
keyPreservation: $this->keyPreservation,
nullPreservation: $this->nullPreservation
);
}
/**
* Tells whether properties whose value is null are omitted from serialization.
*
* @return bool True when null-valued properties are omitted, false otherwise.
*/
public function omitsNulls(): bool
{
return $this->nullPreservation->shouldOmitNulls();
}
/**
* Returns the given iterable with keys arranged per the configured key preservation.
*
* @param array<int|string, mixed> $values The serialized iterable.
* @return array<int|string, mixed> The values keyed per the configured preservation.
*/
public function arrangeKeys(array $values): array
{
return $this->keyPreservation->shouldPreserveKeys() ? $values : array_values($values);
}
/**
* Returns a copy that omits properties whose value is null from serialization.
*
* @return Configuration The new instance.
*/
public function omittingNulls(): Configuration
{
return new Configuration(
omittedFields: $this->omittedFields,
keyPreservation: $this->keyPreservation,
nullPreservation: NullPreservation::OMIT
);
}
/**
* Returns a copy that discards array keys of iterable content.
*
* @return Configuration The new instance.
*/
public function discardingKeys(): Configuration
{
return new Configuration(
omittedFields: $this->omittedFields,
keyPreservation: KeyPreservation::DISCARD,
nullPreservation: $this->nullPreservation
);
}
}