Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
DataGenerator Class Reference

Public Member Functions

 __construct ($dictionaryFile)
 
 generate ($minAmountOfWords, $maxAmountOfWords, $key=null)
 

Protected Member Functions

 readData ()
 

Detailed Description

Definition at line 12 of file DataGenerator.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $dictionaryFile)

DataGenerator constructor.

Parameters
string$dictionaryFile

Definition at line 40 of file DataGenerator.php.

41  {
42  $this->dictionaryFile = $dictionaryFile;
43  $this->readData();
44  $this->generatedValues = [];
45  }

Member Function Documentation

◆ generate()

generate (   $minAmountOfWords,
  $maxAmountOfWords,
  $key = null 
)

Generate string of random word data.

Parameters
int$minAmountOfWords
int$maxAmountOfWords
string | null$key
Returns
string

Definition at line 68 of file DataGenerator.php.

69  {
70  $numberOfWords = random_int($minAmountOfWords, $maxAmountOfWords);
71  $result = '';
72 
73  if ($key === null || !array_key_exists($key, $this->generatedValues)) {
74  for ($i = 0; $i < $numberOfWords; $i++) {
75  $result .= ' ' . $this->dictionaryData[random_int(0, count($this->dictionaryData) - 1)];
76  }
77  $result = trim($result);
78 
79  if ($key !== null) {
80  $this->generatedValues[$key] = $result;
81  }
82  } else {
83  $result = $this->generatedValues[$key];
84  }
85  return $result;
86  }
$i
Definition: gallery.phtml:31

◆ readData()

readData ( )
protected

Read data from file.

Returns
void

Definition at line 52 of file DataGenerator.php.

53  {
54  $f = fopen($this->dictionaryFile, 'r');
55  while (!feof($f) && is_array($line = fgetcsv($f))) {
56  $this->dictionaryData[] = $line[0];
57  }
58  }

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