Skip to content

Commit 7678ce5

Browse files
committed
Re-add withoutAnonymization
1 parent be57b83 commit 7678ce5

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/Anonymized.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function attributesToArray(): array
2525
{
2626
$attributes = parent::attributesToArray();
2727

28-
if (static::getAnonymizeManager()->isEnabled()) {
28+
if ($this->anonymizeEnabled && static::getAnonymizeManager()->isEnabled()) {
2929
$attributes = $this->addAnonymizedAttributesToArray($attributes);
3030
}
3131

@@ -39,7 +39,7 @@ public function attributesToArray(): array
3939
*/
4040
public function getAttributeValue($key): mixed
4141
{
42-
if (! static::getAnonymizeManager()->isEnabled()) {
42+
if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
4343
return parent::getAttributeValue($key);
4444
}
4545

src/AnonymizedResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function resolve($request = null): array
1515
{
1616
$attributes = parent::resolve($request);
1717

18-
if (! static::getAnonymizeManager()->isEnabled()) {
18+
if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
1919
return $attributes;
2020
}
2121

src/AnonymizesAttributes.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
trait AnonymizesAttributes
88
{
9+
/**
10+
* Whether anonymization is enabled for the current instance.
11+
*/
12+
protected bool $anonymizeEnabled = true;
13+
914
/**
1015
* The anonymized attributes for the current instance and seed.
1116
*/
@@ -16,6 +21,27 @@ trait AnonymizesAttributes
1621
*/
1722
protected string $anonymizedAttributeCacheSeed;
1823

24+
/**
25+
* Execute a callback without anonymization.
26+
*
27+
* @template TReturn
28+
*
29+
* @param callable($this): TReturn $callback
30+
* @return TReturn
31+
*/
32+
public function withoutAnonymization(callable $callback): mixed
33+
{
34+
$previous = $this->anonymizeEnabled;
35+
36+
$this->anonymizeEnabled = false;
37+
38+
try {
39+
return $callback($this);
40+
} finally {
41+
$this->anonymizeEnabled = $previous;
42+
}
43+
}
44+
1945
/**
2046
* Get the seed for the anonymizable instance.
2147
*/
@@ -48,17 +74,19 @@ protected function addAnonymizedAttributesToArray(array $attributes): array
4874
*/
4975
protected function getCachedAnonymizedAttributes(): array
5076
{
51-
$seed = $this->getAnonymizableSeed();
77+
return $this->withoutAnonymization(function (): array {
78+
$seed = $this->getAnonymizableSeed();
5279

53-
if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) {
54-
$this->anonymizedAttributeCache = $this->getAnonymizedAttributes(
55-
static::getAnonymizeManager()->faker($seed)
56-
);
80+
if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) {
81+
$this->anonymizedAttributeCache = $this->getAnonymizedAttributes(
82+
static::getAnonymizeManager()->faker($seed)
83+
);
5784

58-
$this->anonymizedAttributeCacheSeed = $seed;
59-
}
85+
$this->anonymizedAttributeCacheSeed = $seed;
86+
}
6087

61-
return $this->anonymizedAttributeCache;
88+
return $this->anonymizedAttributeCache;
89+
});
6290
}
6391

6492
/**

0 commit comments

Comments
 (0)