forked from Smile-SA/gdpr-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomizeText.php
69 lines (56 loc) · 1.68 KB
/
RandomizeText.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
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Randomizer;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
class RandomizeText implements ConverterInterface
{
/**
* @var int
*/
private $minLength;
/**
* @var string
*/
private $replacements;
/**
* @var int
*/
private $replacementsCount;
/**
* @param array $parameters
* @throws ValidationException
*/
public function __construct(array $parameters = [])
{
$input = (new ParameterProcessor())
->addParameter('replacements', Parameter::TYPE_STRING, true, '0123456789abcdefghijklmnopqrstuvwxyz')
->addParameter('min_length', Parameter::TYPE_INT, true, 3)
->process($parameters);
$this->minLength = $input->get('min_length');
$this->replacements = $input->get('replacements');
$this->replacementsCount = strlen($this->replacements);
}
/**
* @inheritdoc
*/
public function convert($value, array $context = [])
{
$value = (string) $value;
if ($value === '') {
return $value;
}
$result = '';
$length = strlen($value);
if ($length < $this->minLength) {
$length = $this->minLength;
}
for ($index = 0; $index < $length; $index++) {
$replacementIndex = mt_rand(0, $this->replacementsCount - 1);
$result .= $this->replacements[$replacementIndex];
}
return $result;
}
}