forked from Smile-SA/gdpr-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomText.php
67 lines (55 loc) · 1.67 KB
/
RandomText.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
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Generator;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
class RandomText implements ConverterInterface
{
/**
* @var string
*/
protected $characters;
/**
* @var int
*/
private $minLength;
/**
* @var int
*/
private $maxLength;
/**
* @var int
*/
private $charactersCount;
/**
* @param array $parameters
* @throws ValidationException
*/
public function __construct(array $parameters = [])
{
$input = (new ParameterProcessor())
->addParameter('characters', Parameter::TYPE_STRING, true, '0123456789abcdefghijklmnopqrstuvwxyz')
->addParameter('min_length', Parameter::TYPE_INT, true, 3)
->addParameter('max_length', Parameter::TYPE_INT, true, 16)
->process($parameters);
$this->characters = $input->get('characters');
$this->minLength = $input->get('min_length');
$this->maxLength = $input->get('max_length');
$this->charactersCount = strlen($this->characters);
}
/**
* @inheritdoc
*/
public function convert($value, array $context = [])
{
$result = '';
$length = mt_rand($this->minLength, $this->maxLength);
for ($index = 0; $index < $length; $index++) {
$characterIndex = mt_rand(0, $this->charactersCount - 1);
$result .= $this->characters[$characterIndex];
}
return $result;
}
}