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