1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Model;
/**
* Provide random word from dictionary
*/
class Dictionary
{
/**
* @var string
*/
private $dictionaryFilePath;
/**
* @var \SplFixedArray
*/
private $dictionary;
/**
* @param string $dictionaryFilePath
* @throws \Magento\Setup\Exception
*/
public function __construct($dictionaryFilePath)
{
$this->dictionaryFilePath = $dictionaryFilePath;
}
/**
* Returns random word from dictionary
*
* @return string
*/
public function getRandWord()
{
if ($this->dictionary === null) {
$this->readDictionary();
}
$randIndex = random_int(0, count($this->dictionary) - 1);
return trim($this->dictionary[$randIndex]);
}
/**
* Read dictionary file
*
* @return void
* @throws \Magento\Setup\Exception
*/
private function readDictionary()
{
if (!is_readable($this->dictionaryFilePath)) {
throw new \Magento\Setup\Exception(
sprintf('Description file %s not found or is not readable', $this->dictionaryFilePath)
);
}
$rows = file($this->dictionaryFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($rows === false) {
throw new \Magento\Setup\Exception(
sprintf('Error occurred while reading dictionary file %s', $this->dictionaryFilePath)
);
}
if (empty($rows)) {
throw new \Magento\Setup\Exception(
sprintf('Dictionary file %s is empty', $this->dictionaryFilePath)
);
}
$this->dictionary = \SplFixedArray::fromArray($rows);
}
}