Skip to content

Commit cff9ce0

Browse files
committed
Add result filters to Exporter_ToYamlArray.
1 parent d90febf commit cff9ce0

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Exporter/Exporter_ToYamlArray.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ class Exporter_ToYamlArray implements ExporterInterface {
6363
*/
6464
private array $defaultObjectFactories = [];
6565

66+
/**
67+
* Filters to apply to the complete result tree.
68+
*
69+
* @var list<callable(array, mixed): array>
70+
*/
71+
private array $filters = [];
72+
6673
/**
6774
* @template T of object
6875
*
@@ -130,12 +137,29 @@ public function withDefaultObjectFactory(string $class, \Closure $factory): stat
130137
return $clone;
131138
}
132139

140+
/**
141+
* @param callable(array, mixed): array $filter
142+
* Callback to apply to the complete result tree.
143+
* The parameters are the result tree and the original value.
144+
*
145+
* @return static
146+
*/
147+
public function withResultFilter(callable $filter): static {
148+
$clone = clone $this;
149+
$clone->filters[] = $filter;
150+
return $clone;
151+
}
152+
133153
/**
134154
* {@inheritdoc}
135155
*/
136156
public function export(mixed $value, int $depth = 2): mixed {
137157
// Don't pollute the main object cache in the main instance.
138-
return (clone $this)->doExport($value, $depth);
158+
$result = (clone $this)->doExport($value, $depth);
159+
foreach ($this->filters as $filter) {
160+
$result = $filter($result, $value);
161+
}
162+
return $result;
139163
}
140164

141165
/**

0 commit comments

Comments
 (0)