|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Emmanuel Dyan |
|
8
|
|
|
* @copyright 2018 Emmanuel Dyan |
|
9
|
|
|
* |
|
10
|
|
|
* @package edyan/neuralyzer |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU General Public License v2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* @link https://github.com/edyan/neuralyzer |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Edyan\Neuralyzer\Faker\Provider; |
|
18
|
|
|
|
|
19
|
|
|
use Faker\Generator; |
|
20
|
|
|
use Faker\Provider\Base; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class UniqueWord. |
|
24
|
|
|
*/ |
|
25
|
|
|
class UniqueWord extends Base |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Language to generate words from. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $language; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
private static $dictionary = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* UniqueWord constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Generator $generator |
|
41
|
|
|
* @param string $language |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(Generator $generator, string $language = 'en_US') |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($generator); |
|
46
|
|
|
$this->language = $language; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* If not already done, load the file in memory as an array, then shuffle it |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function uniqueWord(): string |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty(self::$dictionary)) { |
|
57
|
|
|
$this->loadFullDictionary(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->extractARowFromDictionary(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Load the current language dictionary with a lot of words |
|
65
|
|
|
* to always be able to get a unique value. |
|
66
|
|
|
*/ |
|
67
|
|
|
private function loadFullDictionary(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$file = __DIR__ . '/../Dictionary/' . $this->language; |
|
70
|
|
|
if (!file_exists($file)) { |
|
71
|
|
|
$file = __DIR__ . '/../Dictionary/en_US'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
self::$dictionary = array_filter(explode(PHP_EOL, file_get_contents($file))); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
* @throws \RuntimeException |
|
80
|
|
|
*/ |
|
81
|
|
|
private function extractARowFromDictionary(): string |
|
82
|
|
|
{ |
|
83
|
|
|
if (empty(self::$dictionary)) { |
|
84
|
|
|
throw new \RuntimeException("Couldn't generate more unique words ... consider using another method"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$key = array_rand(self::$dictionary); |
|
88
|
|
|
$row = self::$dictionary[$key]; |
|
89
|
|
|
unset(self::$dictionary[$key]); |
|
90
|
|
|
|
|
91
|
|
|
return $row; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
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: