-
Notifications
You must be signed in to change notification settings - Fork 48
/
RandomizeEmail.php
58 lines (45 loc) · 1.32 KB
/
RandomizeEmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Randomizer;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
class RandomizeEmail extends RandomizeText
{
/**
* @var string[]
*/
private array $domains;
private int $domainsCount;
/**
* @inheritdoc
*/
public function setParameters(array $parameters): void
{
parent::setParameters($parameters);
$input = (new ParameterProcessor())
->addParameter('domains', Parameter::TYPE_ARRAY, true, ['example.com', 'example.net', 'example.org'])
->process($parameters);
$this->domains = $input->get('domains');
$this->domainsCount = count($this->domains);
}
/**
* @inheritdoc
*/
public function convert(mixed $value, array $context = []): string
{
$value = (string) $value;
if ($value === '') {
return $value;
}
// Replace the username
$parts = explode('@', $value);
$value = parent::convert($parts[0]);
if (!isset($parts[1])) {
return $value;
}
// Replace the email domain
$index = mt_rand(0, $this->domainsCount - 1);
$value .= '@' . $this->domains[$index];
return $value;
}
}