Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Public Member Functions | Static Public Member Functions | Data Fields
Random Class Reference

Public Member Functions

 getRandomString ($length, $chars=null)
 
 getUniqueHash ($prefix='')
 

Static Public Member Functions

static getRandomNumber ($min=0, $max=null)
 

Data Fields

const CHARS_LOWERS = 'abcdefghijklmnopqrstuvwxyz'
 
const CHARS_UPPERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
const CHARS_DIGITS = '0123456789'
 

Detailed Description

Random data generator

@api

Since
100.0.2

Definition at line 17 of file Random.php.

Member Function Documentation

◆ getRandomNumber()

static getRandomNumber (   $min = 0,
  $max = null 
)
static

Return a random number in the specified range

Parameters
int$min
int$max
Returns
int A random integer value between min (or 0) and max
Exceptions
LocalizedException

Definition at line 62 of file Random.php.

63  {
64  if (null === $max) {
65  $max = mt_getrandmax();
66  }
67 
68  if ($max < $min) {
69  throw new LocalizedException(new Phrase('Invalid range given.'));
70  }
71 
72  return random_int($min, $max);
73  }

◆ getRandomString()

getRandomString (   $length,
  $chars = null 
)

#- Get random string.

Parameters
int$length
null | string$chars
Returns
string
Exceptions
LocalizedException

Definition at line 39 of file Random.php.

40  {
41  $str = '';
42  if (null === $chars) {
43  $chars = self::CHARS_LOWERS.self::CHARS_UPPERS.self::CHARS_DIGITS;
44  }
45 
46  $charsMaxKey = mb_strlen($chars) - 1;
47  for ($i = 0; $i < $length; $i++) {
48  $str .= $chars[self::getRandomNumber(0, $charsMaxKey)];
49  }
50 
51  return $str;
52  }
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
$i
Definition: gallery.phtml:31

◆ getUniqueHash()

getUniqueHash (   $prefix = '')

Generate a hash from unique ID.

Parameters
string$prefix
Returns
string
Exceptions
LocalizedException

Definition at line 82 of file Random.php.

83  {
84  return $prefix . $this->getRandomString(32);
85  }
$prefix
Definition: name.phtml:25
getRandomString($length, $chars=null)
Definition: Random.php:39

Field Documentation

◆ CHARS_DIGITS

const CHARS_DIGITS = '0123456789'

Definition at line 26 of file Random.php.

◆ CHARS_LOWERS

const CHARS_LOWERS = 'abcdefghijklmnopqrstuvwxyz'

#+ Frequently used character classes

Definition at line 22 of file Random.php.

◆ CHARS_UPPERS

const CHARS_UPPERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Definition at line 24 of file Random.php.


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