Skip to content

Commit d8c4bbe

Browse files
committed
return null if value is empty
1 parent 8feacf0 commit d8c4bbe

File tree

9 files changed

+47
-37
lines changed

9 files changed

+47
-37
lines changed

Service/Anonymize/Anonymizer/AlphaLower.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(
3737
* @inheritdoc
3838
* @throws LocalizedException
3939
*/
40-
public function anonymize($value): string
40+
public function anonymize($value): ?string
4141
{
42-
return $this->mathRandom->getRandomString($this->length, Random::CHARS_LOWERS);
42+
return $value ? $this->mathRandom->getRandomString($this->length, Random::CHARS_LOWERS) : null;
4343
}
4444
}

Service/Anonymize/Anonymizer/AlphaNum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(
3737
* @inheritdoc
3838
* @throws LocalizedException
3939
*/
40-
public function anonymize($value): string
40+
public function anonymize($value): ?string
4141
{
42-
return $this->mathRandom->getRandomString($this->length);
42+
return $value ? $this->mathRandom->getRandomString($this->length) : null;
4343
}
4444
}

Service/Anonymize/Anonymizer/AlphaUpper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(
3737
* @inheritdoc
3838
* @throws LocalizedException
3939
*/
40-
public function anonymize($value): string
40+
public function anonymize($value): ?string
4141
{
42-
return $this->mathRandom->getRandomString($this->length, Random::CHARS_UPPERS);
42+
return $value ? $this->mathRandom->getRandomString($this->length, Random::CHARS_UPPERS) : null;
4343
}
4444
}

Service/Anonymize/Anonymizer/Anonymous.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public function __construct(
3333
* @inheritdoc
3434
* @throws LocalizedException
3535
*/
36-
public function anonymize($value): string
36+
public function anonymize($value): ?string
3737
{
38-
$phrase = new Phrase(
39-
self::PHRASE,
40-
[
41-
$this->mathRandom->getRandomString(self::PREFIX_LENGTH),
42-
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH),
43-
]
44-
);
45-
46-
return $phrase->render();
38+
return $value
39+
? (new Phrase(
40+
self::PHRASE,
41+
[
42+
$this->mathRandom->getRandomString(self::PREFIX_LENGTH),
43+
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH),
44+
]
45+
))->render()
46+
: null;
4747
}
4848
}

Service/Anonymize/Anonymizer/Date.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Opengento\Gdpr\Service\Anonymize\Anonymizer;
99

10-
use Exception;
10+
use DateTime;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Math\Random;
12-
use Magento\Framework\Stdlib\DateTime;
13+
use Magento\Framework\Stdlib\DateTime as StdlibDateTime;
1314
use Opengento\Gdpr\Service\Anonymize\AnonymizerInterface;
1415

1516
final class Date implements AnonymizerInterface
@@ -19,13 +20,22 @@ final class Date implements AnonymizerInterface
1920

2021
/**
2122
* @inheritdoc
22-
* @throws Exception
23+
* @throws LocalizedException
2324
*/
24-
public function anonymize($value): string
25+
public function anonymize($value): ?string
2526
{
26-
$dateTime = new \DateTime();
27+
return $value ? $this->randomDateTime()->format(StdlibDateTime::DATETIME_PHP_FORMAT) : null;
28+
}
29+
30+
/**
31+
* @return DateTime
32+
* @throws LocalizedException
33+
*/
34+
private function randomDateTime(): DateTime
35+
{
36+
$dateTime = new DateTime();
2737
$dateTime->setTimestamp(Random::getRandomNumber(self::MIN_TIMESTAMP, self::MAX_TIMESTAMP));
2838

29-
return $dateTime->format(DateTime::DATE_PHP_FORMAT);
39+
return $dateTime;
3040
}
3141
}

Service/Anonymize/Anonymizer/Email.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public function __construct(
3333
* @inheritdoc
3434
* @throws LocalizedException
3535
*/
36-
public function anonymize($value): string
36+
public function anonymize($value): ?string
3737
{
38-
$phrase = new Phrase(
39-
self::PHRASE,
40-
[
41-
$this->mathRandom->getRandomString(self::PREFIX_LENGTH, Random::CHARS_LOWERS),
42-
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH, Random::CHARS_LOWERS),
43-
]
44-
);
45-
46-
return $phrase->render();
38+
return $value
39+
? (new Phrase(
40+
self::PHRASE,
41+
[
42+
$this->mathRandom->getRandomString(self::PREFIX_LENGTH, Random::CHARS_LOWERS),
43+
$this->mathRandom->getRandomString(self::SUFFIX_LENGTH, Random::CHARS_LOWERS),
44+
]
45+
))->render()
46+
: null;
4747
}
4848
}

Service/Anonymize/Anonymizer/Entity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(
4848
* @inheritdoc
4949
* @throws Exception
5050
*/
51-
public function anonymize($entity)
51+
public function anonymize($entity): object
5252
{
5353
if (!is_object($entity)) {
5454
throw new InvalidArgumentException(

Service/Anonymize/Anonymizer/Number.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(
3737
* @inheritdoc
3838
* @throws LocalizedException
3939
*/
40-
public function anonymize($value): int
40+
public function anonymize($value): ?int
4141
{
42-
return Random::getRandomNumber($this->min, $this->max);
42+
return $value ? Random::getRandomNumber($this->min, $this->max) : null;
4343
}
4444
}

Service/Anonymize/Anonymizer/Phone.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ final class Phone implements AnonymizerInterface
1313
{
1414
private const PHONE_NUMBER = '9999999999';
1515

16-
public function anonymize($value): string
16+
public function anonymize($value): ?string
1717
{
18-
return self::PHONE_NUMBER;
18+
return $value ? self::PHONE_NUMBER : null;
1919
}
2020
}

0 commit comments

Comments
 (0)