Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Lottery.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use RuntimeException;
6
7 class Lottery
8 {
9 /**
10 * The number of expected wins.
11 *
12 * @var int|float
13 */
14 protected $chances;
15
16 /**
17 * The number of potential opportunities to win.
18 *
19 * @var int|null
20 */
21 protected $outOf;
22
23 /**
24 * The winning callback.
25 *
26 * @var null|callable
27 */
28 protected $winner;
29
30 /**
31 * The losing callback.
32 *
33 * @var null|callable
34 */
35 protected $loser;
36
37 /**
38 * The factory that should be used to generate results.
39 *
40 * @var callable|null
41 */
42 protected static $resultFactory;
43
44 /**
45 * Create a new Lottery instance.
46 *
47 * @param int|float $chances
48 * @param int<1, max>|null $outOf
49 */
50 public function __construct($chances, $outOf = null)
51 {
52 if ($outOf === null && is_float($chances) && $chances > 1) {
53 throw new RuntimeException('Float must not be greater than 1.');
54 }
55
56 if ($outOf !== null && $outOf < 1) {
57 throw new RuntimeException('Lottery "out of" value must be greater than or equal to 1.');
58 }
59
60 $this->chances = $chances;
61
62 $this->outOf = $outOf;
63 }
64
65 /**
66 * Create a new Lottery instance.
67 *
68 * @param int|float $chances
69 * @param int|null $outOf
70 * @return static
71 */
72 public static function odds($chances, $outOf = null)
73 {
74 return new static($chances, $outOf);
75 }
76
77 /**
78 * Set the winner callback.
79 *
80 * @param callable $callback
81 * @return $this
82 */
83 public function winner($callback)
84 {
85 $this->winner = $callback;
86
87 return $this;
88 }
89
90 /**
91 * Set the loser callback.
92 *
93 * @param callable $callback
94 * @return $this
95 */
96 public function loser($callback)
97 {
98 $this->loser = $callback;
99
100 return $this;
101 }
102
103 /**
104 * Run the lottery.
105 *
106 * @param mixed ...$args
107 * @return mixed
108 */
109 public function __invoke(...$args)
110 {
111 return $this->runCallback(...$args);
112 }
113
114 /**
115 * Run the lottery.
116 *
117 * @param null|int $times
118 * @return mixed
119 */
120 public function choose($times = null)
121 {
122 if ($times === null) {
123 return $this->runCallback();
124 }
125
126 $results = [];
127
128 for ($i = 0; $i < $times; $i++) {
129 $results[] = $this->runCallback();
130 }
131
132 return $results;
133 }
134
135 /**
136 * Run the winner or loser callback, randomly.
137 *
138 * @param mixed ...$args
139 * @return callable
140 */
141 protected function runCallback(...$args)
142 {
143 return $this->wins()
144 ? ($this->winner ?? fn () => true)(...$args)
145 : ($this->loser ?? fn () => false)(...$args);
146 }
147
148 /**
149 * Determine if the lottery "wins" or "loses".
150 *
151 * @return bool
152 */
153 protected function wins()
154 {
155 return static::resultFactory()($this->chances, $this->outOf);
156 }
157
158 /**
159 * The factory that determines the lottery result.
160 *
161 * @return callable
162 */
163 protected static function resultFactory()
164 {
165 return static::$resultFactory ?? fn ($chances, $outOf) => $outOf === null
166 ? random_int(0, PHP_INT_MAX) / PHP_INT_MAX <= $chances
167 : random_int(1, $outOf) <= $chances;
168 }
169
170 /**
171 * Force the lottery to always result in a win.
172 *
173 * @param callable|null $callback
174 * @return void
175 */
176 public static function alwaysWin($callback = null)
177 {
178 self::setResultFactory(fn () => true);
179
180 if ($callback === null) {
181 return;
182 }
183
184 $callback();
185
186 static::determineResultNormally();
187 }
188
189 /**
190 * Force the lottery to always result in a lose.
191 *
192 * @param callable|null $callback
193 * @return void
194 */
195 public static function alwaysLose($callback = null)
196 {
197 self::setResultFactory(fn () => false);
198
199 if ($callback === null) {
200 return;
201 }
202
203 $callback();
204
205 static::determineResultNormally();
206 }
207
208 /**
209 * Set the sequence that will be used to determine lottery results.
210 *
211 * @param array $sequence
212 * @param callable|null $whenMissing
213 * @return void
214 */
215 public static function fix($sequence, $whenMissing = null)
216 {
217 static::forceResultWithSequence($sequence, $whenMissing);
218 }
219
220 /**
221 * Set the sequence that will be used to determine lottery results.
222 *
223 * @param array $sequence
224 * @param callable|null $whenMissing
225 * @return void
226 */
227 public static function forceResultWithSequence($sequence, $whenMissing = null)
228 {
229 $next = 0;
230
231 $whenMissing ??= function ($chances, $outOf) use (&$next) {
232 $factoryCache = static::$resultFactory;
233
234 static::$resultFactory = null;
235
236 $result = static::resultFactory()($chances, $outOf);
237
238 static::$resultFactory = $factoryCache;
239
240 $next++;
241
242 return $result;
243 };
244
245 static::setResultFactory(function ($chances, $outOf) use (&$next, $sequence, $whenMissing) {
246 if (array_key_exists($next, $sequence)) {
247 return $sequence[$next++];
248 }
249
250 return $whenMissing($chances, $outOf);
251 });
252 }
253
254 /**
255 * Indicate that the lottery results should be determined normally.
256 *
257 * @return void
258 */
259 public static function determineResultsNormally()
260 {
261 static::determineResultNormally();
262 }
263
264 /**
265 * Indicate that the lottery results should be determined normally.
266 *
267 * @return void
268 */
269 public static function determineResultNormally()
270 {
271 static::$resultFactory = null;
272 }
273
274 /**
275 * Set the factory that should be used to determine the lottery results.
276 *
277 * @param callable $factory
278 * @return void
279 */
280 public static function setResultFactory($factory)
281 {
282 self::$resultFactory = $factory;
283 }
284 }