Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Dictionary.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Setup\Model;
7 
12 {
16  private $dictionaryFilePath;
17 
21  private $dictionary;
22 
27  public function __construct($dictionaryFilePath)
28  {
29  $this->dictionaryFilePath = $dictionaryFilePath;
30  }
31 
37  public function getRandWord()
38  {
39  if ($this->dictionary === null) {
40  $this->readDictionary();
41  }
42 
43  $randIndex = random_int(0, count($this->dictionary) - 1);
44  return trim($this->dictionary[$randIndex]);
45  }
46 
53  private function readDictionary()
54  {
55  if (!is_readable($this->dictionaryFilePath)) {
56  throw new \Magento\Setup\Exception(
57  sprintf('Description file %s not found or is not readable', $this->dictionaryFilePath)
58  );
59  }
60 
61  $rows = file($this->dictionaryFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
62 
63  if ($rows === false) {
64  throw new \Magento\Setup\Exception(
65  sprintf('Error occurred while reading dictionary file %s', $this->dictionaryFilePath)
66  );
67  }
68 
69  if (empty($rows)) {
70  throw new \Magento\Setup\Exception(
71  sprintf('Dictionary file %s is empty', $this->dictionaryFilePath)
72  );
73  }
74 
75  $this->dictionary = \SplFixedArray::fromArray($rows);
76  }
77 }
__construct($dictionaryFilePath)
Definition: Dictionary.php:27