1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Contract\Core; |
4: | |
5: | use Closure; |
6: | use DateTimeInterface; |
7: | |
8: | /** |
9: | * @api |
10: | * |
11: | * @template TEntity of object |
12: | */ |
13: | interface SerializeRulesInterface extends Immutable |
14: | { |
15: | /** |
16: | * Get the entity to which the rules apply |
17: | * |
18: | * @return class-string<TEntity> |
19: | */ |
20: | public function getEntity(): string; |
21: | |
22: | /** |
23: | * Merge with another instance, giving precedence to its values if there are |
24: | * any conflicts |
25: | * |
26: | * An exception is thrown if `$rules` does not apply to the same entity or |
27: | * one of its subclasses. |
28: | * |
29: | * @template T of TEntity |
30: | * |
31: | * @param static<T> $rules |
32: | * @return static<T> |
33: | */ |
34: | public function merge(SerializeRulesInterface $rules): SerializeRulesInterface; |
35: | |
36: | /** |
37: | * Get keys to remove from a serialized class at a given path |
38: | * |
39: | * If `$baseClass` is given, rules applied to `$class` and its parents up to |
40: | * but not including `$baseClass` are considered, otherwise the only |
41: | * class-based rules considered are those applied to `$class`. |
42: | * |
43: | * @template T0 of object |
44: | * @template T1 of T0 |
45: | * |
46: | * @param class-string<T1>|null $class |
47: | * @param class-string<T0>|null $baseClass |
48: | * @param string[] $path |
49: | * @return array<string,string> Keys and values are the same. |
50: | */ |
51: | public function getRemovableKeys(?string $class, ?string $baseClass, array $path): array; |
52: | |
53: | /** |
54: | * Get keys to replace in a serialized class at a given path |
55: | * |
56: | * If `$baseClass` is given, rules applied to `$class` and its parents up to |
57: | * but not including `$baseClass` are considered, otherwise the only |
58: | * class-based rules considered are those applied to `$class`. |
59: | * |
60: | * @template T0 of object |
61: | * @template T1 of T0 |
62: | * |
63: | * @param class-string<T1>|null $class |
64: | * @param class-string<T0>|null $baseClass |
65: | * @param string[] $path |
66: | * @return array<string,array{int|string|null,(Closure(mixed $value): mixed)|null}> Each |
67: | * key is mapped to an array with two values, one of which may be `null`: |
68: | * - a new key for the value |
69: | * - a closure to return a new value for the key |
70: | */ |
71: | public function getReplaceableKeys(?string $class, ?string $baseClass, array $path): array; |
72: | |
73: | /** |
74: | * Get a date formatter to serialize date and time values |
75: | */ |
76: | public function getDateFormatter(): ?DateFormatterInterface; |
77: | |
78: | /** |
79: | * Get an instance that uses the given date formatter to serialize date and |
80: | * time values |
81: | * |
82: | * {@see DateTimeInterface} instances are serialized as ISO-8601 strings if |
83: | * no date formatter is provided. |
84: | * |
85: | * @return static |
86: | */ |
87: | public function withDateFormatter(?DateFormatterInterface $formatter); |
88: | |
89: | /** |
90: | * Check if undeclared property values are serialized |
91: | */ |
92: | public function getIncludeMeta(): bool; |
93: | |
94: | /** |
95: | * Get an instance that serializes undeclared property values |
96: | * |
97: | * @return static |
98: | */ |
99: | public function withIncludeMeta(?bool $include = true); |
100: | |
101: | /** |
102: | * Check if serialized entities are sorted by key |
103: | */ |
104: | public function getSortByKey(): bool; |
105: | |
106: | /** |
107: | * Get an instance that sorts serialized entities by key |
108: | * |
109: | * @return static |
110: | */ |
111: | public function withSortByKey(?bool $sort = true); |
112: | |
113: | /** |
114: | * Get the maximum depth of nested values |
115: | */ |
116: | public function getMaxDepth(): int; |
117: | |
118: | /** |
119: | * Get an instance that limits the depth of nested values |
120: | * |
121: | * @return static |
122: | */ |
123: | public function withMaxDepth(?int $depth); |
124: | |
125: | /** |
126: | * Check if recursion is detected when serializing nested entities |
127: | */ |
128: | public function getDetectRecursion(): bool; |
129: | |
130: | /** |
131: | * Get an instance that detects recursion when serializing nested entities |
132: | * |
133: | * If it would be impossible for a circular reference to arise during |
134: | * serialization, disable recursion detection to improve performance and |
135: | * reduce memory consumption. |
136: | * |
137: | * @return static |
138: | */ |
139: | public function withDetectRecursion(?bool $detect = true); |
140: | |
141: | /** |
142: | * Check if path-based rules are applied to nested instances of the entity |
143: | */ |
144: | public function getRecurseRules(): bool; |
145: | |
146: | /** |
147: | * Get an instance that applies path-based rules to nested instances of the |
148: | * entity |
149: | * |
150: | * @return static |
151: | */ |
152: | public function withRecurseRules(?bool $recurse = true); |
153: | } |
154: |