-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathFakeIpPlugin.php
68 lines (55 loc) · 1.7 KB
/
FakeIpPlugin.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Bazinga\GeocoderBundle\Plugin;
use Faker\Generator;
use Faker\Provider\Internet;
use Geocoder\Plugin\Plugin;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Http\Promise\Promise;
/**
* Replace local IP with something better.
*
* @author Tobias Nyholm <[email protected]>
*/
class FakeIpPlugin implements Plugin
{
private ?string $needle;
private ?string $replacement;
private ?Generator $faker = null;
public function __construct(?string $needle, ?string $replacement = null, bool $useFaker = false)
{
$this->needle = $needle;
$this->replacement = $replacement;
if ($useFaker) {
$this->faker = new Generator();
$this->faker->addProvider(new Internet($this->faker));
}
}
public function handleQuery(Query $query, callable $next, callable $first): Promise
{
if (!$query instanceof GeocodeQuery) {
return $next($query);
}
$replacement = $this->replacement;
if (null !== $this->faker) {
$replacement = $this->faker->ipv4();
}
if (null !== $this->needle && '' !== $this->needle) {
$text = str_replace($this->needle, $replacement, $query->getText(), $count);
if ($count > 0) {
$query = $query->withText($text);
}
} else {
$query = $query->withText($replacement);
}
return $next($query);
}
}