Skip to content

Commit 9226b0d

Browse files
author
heqiming
committed
configuration with snake case
1 parent 1909472 commit 9226b0d

File tree

6 files changed

+117
-35
lines changed

6 files changed

+117
-35
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ php artisan vendor:publish --provider="Leoboy\Desensitization\Laravel\Desensitiz
8989

9090
content of the configuation file:
9191

92-
- `wildcardChar`, it define the wildcard character used in multi-dimension array searching, default is "*".
93-
- `keyDot`, it define the key separator used in multi-dimension array searching, default is ".".
94-
- `skipTransformationException`, tell the desensitizer to skip the exception thrown by the rule, default is boolean `false`.
92+
- `wildcard_char`, it define the wildcard character used in multi-dimension array searching, default is "*".
93+
- `key_dot`, it define the key separator used in multi-dimension array searching, default is ".".
94+
- `skip_transformation_exception`, tell the desensitizer whether skip the exception thrown by the rule or not, default is boolean `false`.
9595

9696
The desensitizer object is automatically bound in the Laravel container (unless accessed through the `global` method, which returns a local desensitizer object). You can quickly access the desensitizer object through the provided Facade:
9797

src/Config.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Leoboy\Desensitization;
4+
5+
use ArrayAccess;
6+
7+
/**
8+
* configuration for desensitization.
9+
*
10+
* @implements ArrayAccess<string, mixed>
11+
*/
12+
class Config implements ArrayAccess
13+
{
14+
/**
15+
* default config.
16+
*
17+
* @var array<string, mixed>
18+
*/
19+
protected array $config = [
20+
'wildcard_char' => '*',
21+
'key_dot' => '.',
22+
'skip_transformation_exception' => false,
23+
];
24+
25+
public function __construct(array $config)
26+
{
27+
$this->config = array_merge_recursive($this->config, $config);
28+
}
29+
30+
public function set(string $path, mixed $value): static
31+
{
32+
Helper::arraySet($this->config, $path, $value);
33+
34+
return $this;
35+
}
36+
37+
public function get(string $path, mixed $default = null): mixed
38+
{
39+
return Helper::arrayGet($this->config, $path, $default);
40+
}
41+
42+
public function toArray(): array
43+
{
44+
return $this->config;
45+
}
46+
47+
public function getWildcardChar(): string
48+
{
49+
return $this->get('wildcard_char');
50+
}
51+
52+
public function getKeyDot(): string
53+
{
54+
return $this->get('key_dot');
55+
}
56+
57+
public function shouldSkipTransformationException(): bool
58+
{
59+
return boolval($this->get('skip_transformation_exception'));
60+
}
61+
62+
public function offsetExists(mixed $offset): bool
63+
{
64+
return isset($this->config[$offset]);
65+
}
66+
67+
public function offsetGet(mixed $offset): mixed
68+
{
69+
return $this->get($offset);
70+
}
71+
72+
public function offsetSet(mixed $offset, mixed $value): void
73+
{
74+
$this->set($offset, $value);
75+
}
76+
77+
public function offsetUnset(mixed $offset): void
78+
{
79+
unset($this->config[$offset]);
80+
}
81+
}

src/Desensitizer.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,14 @@ class Desensitizer
2222
protected static $instance = null;
2323

2424
/**
25-
* guarded security policy
25+
* guarded security policy.
2626
*/
2727
protected SecurityPolicyContract $policy;
2828

2929
/**
30-
* default configuration for desensitization
31-
*
32-
* @var array<string, mixed>
30+
* configuration for desensitization.
3331
*/
34-
protected array $config = [
35-
'wildcardChar' => '*',
36-
'keyDot' => '.',
37-
'skipTransformationException' => false,
38-
];
32+
protected Config $config;
3933

4034
/**
4135
* @param array<string, mixed> $config
@@ -48,7 +42,7 @@ public function __construct(
4842
$guard = new NoneGuard();
4943
}
5044
$this->via($guard);
51-
$this->config = array_merge($this->config, $config);
45+
$this->config = new Config($config);
5246
}
5347

5448
/**
@@ -92,12 +86,14 @@ public function via(string|GuardContract|RuleContract|SecurityPolicyContract|cal
9286
public function config(?string $key = null, $value = null): mixed
9387
{
9488
if (is_null($key)) {
95-
return $this->config;
89+
return $this->config->toArray();
9690
}
91+
9792
if (is_null($value)) {
98-
return Helper::arrayGet($this->config, $key, null);
93+
return $this->config->get($key, null);
9994
}
100-
Helper::arraySet($this->config, $key, $value);
95+
96+
$this->config->set($key, $value);
10197

10298
return $this;
10399
}
@@ -136,7 +132,7 @@ public function parse(string $definition): RuleContract
136132
*/
137133
public function desensitize(array $data, array $definitions): array
138134
{
139-
$dotArray = Helper::arrayDot($data, $this->config['keyDot']);
135+
$dotArray = Helper::arrayDot($data, $this->config->getKeyDot());
140136
$dotKeys = array_keys($dotArray);
141137
$attributes = [];
142138

@@ -186,7 +182,7 @@ public function invoke(mixed $value, string|RuleContract|callable $type = '')
186182
try {
187183
return $rule->transform($value);
188184
} catch (Throwable $th) {
189-
if ($this->config['skipTransformationException']) {
185+
if ($this->config->shouldSkipTransformationException()) {
190186
return $value;
191187
}
192188
throw new TransformException(sprintf(
@@ -202,8 +198,8 @@ public function invoke(mixed $value, string|RuleContract|callable $type = '')
202198
*/
203199
protected function extractMatchedDataKeys(string $key, array $dotKeys): array
204200
{
205-
$wildcardChar = $this->config['wildcardChar'];
206-
$keyDot = $this->config['keyDot'];
201+
$wildcardChar = $this->config->getWildcardChar();
202+
$keyDot = $this->config->getKeyDot();
207203
if (! str_contains($key, $wildcardChar)) {
208204
$realKeysExisted = count(array_filter(
209205
$dotKeys,
@@ -235,14 +231,19 @@ protected function transform(array $data, array $attributes): array
235231
foreach ($attributes as $attribute) {
236232
$guardedRule = Factory::rule($this->policy->decide($attribute));
237233
foreach ($attribute->getDataKeys() as $key) {
238-
$original = Helper::arrayGet($data, $key, null, $this->config['keyDot']);
234+
$original = Helper::arrayGet(
235+
$data,
236+
$key,
237+
null,
238+
$this->config->getKeyDot()
239+
);
239240
try {
240241
$transformed = match (true) {
241242
($attribute instanceof TransformerContract) => $attribute->transform($original),
242243
default => $guardedRule->transform($original),
243244
};
244245
} catch (Throwable $th) {
245-
if ($this->config['skipTransformationException']) {
246+
if ($this->config->shouldSkipTransformationException()) {
246247
continue;
247248
}
248249
throw new TransformException('Attribute transformation failed, value: '.var_export($attribute, true));
@@ -252,8 +253,8 @@ protected function transform(array $data, array $attributes): array
252253
$key,
253254
$transformed,
254255
true,
255-
$this->config['keyDot'],
256-
$this->config['wildcardChar']
256+
$this->config->getKeyDot(),
257+
$this->config->getWildcardChar()
257258
);
258259
}
259260
}

src/Laravel/config/desensitization.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
/**
55
* define how to match multi-dimensional array keys
66
*/
7-
'wildcardChar' => '*',
7+
'wildcard_char' => '*',
88

99
/**
1010
* divide multi-dimensional array keys
1111
*/
12-
'keyDot' => '.',
12+
'key_dot' => '.',
1313

1414
/**
1515
* continue processing when an exception occurs
1616
*/
17-
'skipTransformationException' => false,
17+
'skip_transformation_exception' => false,
1818
];

tests/DesensitizerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public function testConfig(): void
150150

151151
$this->assertSame(
152152
[
153-
'wildcardChar' => '*',
154-
'keyDot' => '.',
155-
'skipTransformationException' => false,
153+
'wildcard_char' => '*',
154+
'key_dot' => '.',
155+
'skip_transformation_exception' => false,
156156
],
157157
$desensitizer->config()
158158
);
@@ -171,8 +171,8 @@ public function testConfig(): void
171171
],
172172
];
173173

174-
$desensitizer->config('keyDot', '__');
175-
$desensitizer->config('wildcardChar', '-');
174+
$desensitizer->config('key_dot', '__');
175+
$desensitizer->config('wildcard_char', '-');
176176

177177
$desensitized = $desensitizer->desensitize($data, [
178178
'a__b__m-' => (new Mask())->use('*')->padding(1)->repeat(3),

tests/LaravelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ final class LaravelTest extends TestCase
1212

1313
public function testConfig(): void
1414
{
15-
$this->assertSame('*', $this->app['config']->get('desensitization.wildcardChar'));
16-
$this->assertSame('.', $this->app['config']->get('desensitization.keyDot'));
17-
$this->assertSame(false, $this->app['config']->get('desensitization.skipTransformationException'));
15+
$this->assertSame('*', $this->app['config']->get('desensitization.wildcard_char'));
16+
$this->assertSame('.', $this->app['config']->get('desensitization.key_dot'));
17+
$this->assertSame(false, $this->app['config']->get('desensitization.skip_transformation_exception'));
1818
}
1919

2020
public function testServiceProvider(): void

0 commit comments

Comments
 (0)