ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilRandom Class Reference

Wrapper for generation of random numbers, strings, bytes. More...

+ Collaboration diagram for ilRandom:

Public Member Functions

 __construct ()
 ilRandom constructor. More...
 
 int (int $min=null, int $max=null)
 

Private Member Functions

 logIfPossible (callable $c)
 

Detailed Description

Wrapper for generation of random numbers, strings, bytes.

Definition at line 7 of file class.ilRandom.php.

Constructor & Destructor Documentation

◆ __construct()

ilRandom::__construct ( )

ilRandom constructor.

Definition at line 13 of file class.ilRandom.php.

14  {
15  }

Member Function Documentation

◆ int()

ilRandom::int ( int  $min = null,
int  $max = null 
)
Parameters
int$min
int$max
Returns
int
Exceptions
Throwable

Definition at line 35 of file class.ilRandom.php.

References Vendor\Package\$e, ilLogLevel\ERROR, ilLogger\error(), logIfPossible(), and ilLogger\logStack().

35  :int
36  {
37  if (is_null($min)) {
38  $min = 0;
39  }
40  if (is_null($max)) {
41  $max = mt_getrandmax();
42  }
43 
44  try {
45  return random_int($min, $max);
46  } catch (Exception $e) {
47  $this->logIfPossible(static function (ilLogger $logger) {
48  $logger->logStack(\ilLogLevel::ERROR);
49  $logger->error('No suitable random number generator found.');
50  });
51  throw $e;
52  } catch (Throwable $e) {
53  $this->logIfPossible(static function (ilLogger $logger) {
54  $logger->logStack(\ilLogLevel::ERROR);
55  $logger->error('max should be greater than min.');
56  });
57  throw $e;
58  }
59  }
logStack($a_level=null, $a_message='')
log stack trace
error($a_message)
logIfPossible(callable $c)
Component logger with individual log levels by component id.
+ Here is the call graph for this function:

◆ logIfPossible()

ilRandom::logIfPossible ( callable  $c)
private
Parameters
callable$c

Definition at line 20 of file class.ilRandom.php.

References $c, and $DIC.

Referenced by int().

21  {
22  global $DIC;
23 
24  if (isset($DIC['ilLoggerFactory'])) {
25  $c($DIC->logger()->rnd());
26  }
27  }
$c
Definition: cli.php:37
global $DIC
Definition: goto.php:24
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: