Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataGenerator.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Setup\Model;
11 
13 {
19  private $dictionaryFile;
20 
26  private $dictionaryData;
27 
33  private $generatedValues;
34 
40  public function __construct($dictionaryFile)
41  {
42  $this->dictionaryFile = $dictionaryFile;
43  $this->readData();
44  $this->generatedValues = [];
45  }
46 
52  protected function readData()
53  {
54  $f = fopen($this->dictionaryFile, 'r');
55  while (!feof($f) && is_array($line = fgetcsv($f))) {
56  $this->dictionaryData[] = $line[0];
57  }
58  }
59 
68  public function generate($minAmountOfWords, $maxAmountOfWords, $key = null)
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  }
87 }
generate($minAmountOfWords, $maxAmountOfWords, $key=null)
$i
Definition: gallery.phtml:31