-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathRandomDate.php
65 lines (54 loc) · 1.83 KB
/
RandomDate.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
<?php
declare(strict_types=1);
namespace Smile\GdprDump\Converter\Generator;
use DateTime;
use Smile\GdprDump\Converter\ConverterInterface;
use Smile\GdprDump\Converter\Parameters\Parameter;
use Smile\GdprDump\Converter\Parameters\ParameterProcessor;
use Smile\GdprDump\Converter\Parameters\ValidationException;
class RandomDate implements ConverterInterface
{
protected string $defaultFormat = 'Y-m-d';
protected DateTime $date;
private string $format;
private int $minYear;
private int $maxYear;
public function __construct()
{
$this->date = new DateTime();
}
public function setParameters(array $parameters): void
{
$input = (new ParameterProcessor())
->addParameter('format', Parameter::TYPE_STRING, true, $this->defaultFormat)
->addParameter('min_year', Parameter::TYPE_INT, false, 1900)
->addParameter('max_year', Parameter::TYPE_INT)
->process($parameters);
$this->format = $input->get('format');
$this->minYear = $input->get('min_year') ?? (int) $this->date->format('Y');
$this->maxYear = $input->get('max_year') ?? (int) $this->date->format('Y');
if ($this->minYear > $this->maxYear) {
throw new ValidationException('The parameter "min_year" must be lower than the parameter "max_year".');
}
}
/**
* @inheritdoc
*/
public function convert(mixed $value, array $context = []): string
{
$this->randomizeDate();
return $this->date->format($this->format);
}
/**
* Randomize the date.
*/
protected function randomizeDate(): void
{
// Randomize the year, month and day
$this->date->setDate(
mt_rand($this->minYear, $this->maxYear),
mt_rand(1, 12),
mt_rand(1, 31)
);
}
}