|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Edyan\Neuralyzer\Faker\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Faker\Generator; |
|
8
|
|
|
use Faker\Provider\Base; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class UniqueWord. |
|
12
|
|
|
*/ |
|
13
|
|
|
class UniqueWord extends Base |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Language to generate words from. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $language; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private static $dictionary = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* UniqueWord constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Generator $generator |
|
29
|
|
|
* @param string $language |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Generator $generator, string $language = 'en_US') |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($generator); |
|
34
|
|
|
$this->language = $language; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* If not already done, load the file in memory as an array, then shuffle it |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function uniqueWord(): string |
|
43
|
|
|
{ |
|
44
|
|
|
if (empty(self::$dictionary)) { |
|
45
|
|
|
$this->loadFullDictionary(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->extractARowFromDictionnary(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Load the current language dictionary with a lot of words |
|
53
|
|
|
* to always be able to get a unique value. |
|
54
|
|
|
*/ |
|
55
|
|
|
private function loadFullDictionary(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$file = __DIR__ . '/../Dictionary/' . $this->language; |
|
58
|
|
|
if (!file_exists($file)) { |
|
59
|
|
|
$file = __DIR__ . '/../Dictionary/en_US'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
self::$dictionary = array_filter(explode(PHP_EOL, file_get_contents($file))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function extractARowFromDictionnary(): string |
|
66
|
|
|
{ |
|
67
|
|
|
if (empty(self::$dictionary)) { |
|
68
|
|
|
throw new \RuntimeException("Couldn't generate more unique words ... consider using another method"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$key = array_rand(self::$dictionary); |
|
72
|
|
|
$row = self::$dictionary[$key]; |
|
73
|
|
|
unset(self::$dictionary[$key]); |
|
74
|
|
|
|
|
75
|
|
|
return $row; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: