14
14
15
15
class SnapshotJsonObjectDriver implements Driver
16
16
{
17
- public function __construct (private array $ ignoredProperties = [])
17
+ public function __construct (
18
+ private array $ ignoredProperties = [],
19
+ private array $ redactedProperties = []
20
+ )
18
21
{
19
22
}
20
23
@@ -39,7 +42,8 @@ public function serialize($data): string
39
42
AbstractNormalizer::IGNORED_ATTRIBUTES => $ this ->ignoredProperties ,
40
43
]);
41
44
42
- $ this ->unsetIgnoredKeys ($ normalizedData , $ this ->ignoredProperties );
45
+ $ this ->ignoreKeys ($ normalizedData , $ this ->ignoredProperties );
46
+ $ this ->redactKeys ($ normalizedData , $ this ->redactedProperties );
43
47
44
48
return $ serializer ->serialize ($ normalizedData , JsonEncoder::FORMAT , [
45
49
'json_encode_options ' => JSON_PRETTY_PRINT
@@ -56,14 +60,27 @@ public function match($expected, $actual)
56
60
Assert::assertEquals ($ expected , $ this ->serialize ($ actual ));
57
61
}
58
62
59
- private function unsetIgnoredKeys (array &$ values , array $ ignoredKeys ): void
63
+ private function ignoreKeys (array &$ values , array $ ignoredKeys ): void
60
64
{
61
65
foreach ($ values as $ key => $ value ) {
62
66
if (in_array ($ key , $ ignoredKeys )) {
63
67
unset($ values [$ key ]);
64
68
}
65
69
else if (is_array ($ value )) {
66
- $ this ->unsetIgnoredKeys ($ value , $ ignoredKeys );
70
+ $ this ->ignoreKeys ($ value , $ ignoredKeys );
71
+ $ values [$ key ] = $ value ;
72
+ }
73
+ }
74
+ }
75
+
76
+ private function redactKeys (array &$ values , array $ redactedKeys ): void
77
+ {
78
+ foreach ($ values as $ key => $ value ) {
79
+ if (in_array ($ key , $ redactedKeys )) {
80
+ $ values [$ key ] = 'REDACTED ' ;
81
+ }
82
+ else if (is_array ($ value )) {
83
+ $ this ->redactKeys ($ value , $ redactedKeys );
67
84
$ values [$ key ] = $ value ;
68
85
}
69
86
}
0 commit comments