1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Alpha\VisitorTrackingBundle\Entity; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @ORM\Entity |
11
|
|
|
*/ |
12
|
|
|
class Seed |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
* |
17
|
|
|
* @ORM\Id |
18
|
|
|
* @ORM\Column(type="integer") |
19
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
20
|
|
|
*/ |
21
|
|
|
protected $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Lifetime |
25
|
|
|
* |
26
|
|
|
* @ORM\ManyToOne(targetEntity="Lifetime", inversedBy="seeds") |
27
|
|
|
*/ |
28
|
|
|
protected $lifetime; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
* |
33
|
|
|
* @ORM\Column(type="string") |
34
|
|
|
*/ |
35
|
|
|
protected $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
* |
40
|
|
|
* @ORM\Column(type="string") |
41
|
|
|
*/ |
42
|
|
|
protected $value; |
43
|
|
|
|
44
|
|
|
public function __construct(string $name, int $numberOfValues, ?array $weights = null) |
45
|
|
|
{ |
46
|
|
|
$this->name = $name; |
47
|
|
|
$this->setValue($numberOfValues, $weights); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
|
|
public function getId() |
54
|
|
|
{ |
55
|
|
|
return $this->id; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Lifetime |
60
|
|
|
*/ |
61
|
|
|
public function getLifetime() |
62
|
|
|
{ |
63
|
|
|
return $this->lifetime; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Lifetime $lifetime |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setLifetime(Lifetime $lifetime) |
72
|
|
|
{ |
73
|
|
|
$this->lifetime = $lifetime; |
74
|
|
|
|
75
|
|
|
if (!$this->lifetime->getSeeds()->contains($this)) { |
76
|
|
|
$this->lifetime->addSeed($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getValue() |
94
|
|
|
{ |
95
|
|
|
return $this->value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function setValue(int $numberOfValues, ?array $weights = null): void |
99
|
|
|
{ |
100
|
|
|
if ($weights === null) { |
101
|
|
|
$weights = \array_fill(1, $numberOfValues, 1); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (\count($weights) !== $numberOfValues) { |
105
|
|
|
throw new \RuntimeException('Number of seed values must equal the count of the weights array'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// This does not need to be cryptographically secure, mt_rand() is roughly 2x faster than random_int(). |
109
|
|
|
/** @noinspection RandomApiMigrationInspection */ |
110
|
|
|
$random = \mt_rand(1, (int) \array_sum($weights)); |
111
|
|
|
$total = 0; |
112
|
|
|
|
113
|
|
|
foreach ($weights as $seed => $weight) { |
114
|
|
|
$total += $weight; |
115
|
|
|
if ($random <= $total) { |
116
|
|
|
if (!\is_string($seed)) { |
117
|
|
|
$seed = (string) $seed; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->value = $seed; |
121
|
|
|
|
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|