Skip to content

Commit 0461267

Browse files
committed
Add option to use Faker library for FakeIpPlugin
1 parent 918599e commit 0461267

File tree

6 files changed

+27
-3
lines changed

6 files changed

+27
-3
lines changed

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
5353

5454
if ($config['fake_ip']['enabled']) {
5555
$definition = $container->getDefinition(FakeIpPlugin::class);
56-
$definition->replaceArgument(1, $config['fake_ip']['ip']);
56+
$definition->replaceArgument(1, $config['fake_ip']['ip'], $config['use_faker']);
5757
} else {
5858
$container->removeDefinition(FakeIpPlugin::class);
5959
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function getConfigTreeBuilder()
8787
->canBeEnabled()
8888
->children()
8989
->scalarNode('ip')->defaultNull()->end()
90+
->booleanNode('user_faker')->defaultFalse()->end()
9091
->end()
9192
->end();
9293

Plugin/FakeIpPlugin.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Bazinga\GeocoderBundle\Plugin;
1414

15+
use Faker\Factory;
1516
use Geocoder\Plugin\Plugin;
1617
use Geocoder\Query\GeocodeQuery;
1718
use Geocoder\Query\Query;
@@ -33,10 +34,16 @@ class FakeIpPlugin implements Plugin
3334
*/
3435
private $replacement;
3536

36-
public function __construct(string $needle, string $replacement)
37+
/**
38+
* @var bool
39+
*/
40+
private $useFaker;
41+
42+
public function __construct(string $needle, string $replacement = null, bool $useFaker = false)
3743
{
3844
$this->needle = $needle;
3945
$this->replacement = $replacement;
46+
$this->useFaker = $useFaker;
4047
}
4148

4249
/**
@@ -48,7 +55,14 @@ public function handleQuery(Query $query, callable $next, callable $first)
4855
return $next($query);
4956
}
5057

51-
$text = str_replace($this->needle, $this->replacement, $query->getText(), $count);
58+
$replacement = $this->replacement;
59+
60+
if ($this->useFaker) {
61+
$faker = Factory::create();
62+
$replacement = $faker->ipv4;
63+
}
64+
65+
$text = str_replace($this->needle, $replacement, $query->getText(), $count);
5266
if ($count > 0) {
5367
$query = $query->withText($text);
5468
}

Resources/doc/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ bazinga_geocoder:
207207

208208
If set, the parameter will replace all instances of "127.0.0.1" in your queries and replace them with the given one.
209209

210+
You can also let [Faker](https://github.com/fzaninotto/Faker) generates fake ip for you.
211+
212+
```yaml
213+
bazinga_geocoder:
214+
fake_ip:
215+
user_faker: true # default false
216+
```
210217

211218
### Cache Results
212219

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function testGetConfigTreeBuilder()
3535
$this->assertTrue($config['profiling']['enabled']);
3636
$this->assertTrue($config['fake_ip']['enabled']);
3737
$this->assertEquals('33.33.33.11', $config['fake_ip']['ip']);
38+
$this->assertFalse($config['fake_ip']['use_faker']);
3839
}
3940

4041
public function testGetConfigTreeBuilderNoDebug()

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"require-dev": {
2323
"doctrine/cache": "~1.3",
2424
"doctrine/orm": "~2.3",
25+
"fzaninotto/faker": "^1.8",
2526
"geocoder-php/algolia-places-provider": "^0.1.1",
2627
"geocoder-php/arcgis-online-provider": "^4.0",
2728
"geocoder-php/bing-maps-provider": "^4.0",

0 commit comments

Comments
 (0)