|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Geocoder\Provider\Faker; |
| 6 | + |
| 7 | +use Faker\Factory; |
| 8 | +use Geocoder\Collection; |
| 9 | +use Geocoder\Model\AddressBuilder; |
| 10 | +use Geocoder\Model\AddressCollection; |
| 11 | +use Geocoder\Provider\Provider; |
| 12 | +use Geocoder\Query\GeocodeQuery; |
| 13 | +use Geocoder\Query\Query; |
| 14 | +use Geocoder\Query\ReverseQuery; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Romain Monteil <[email protected]> |
| 18 | + */ |
| 19 | +final class Faker implements Provider |
| 20 | +{ |
| 21 | + public const PROVIDER_NAME = 'faker'; |
| 22 | + |
| 23 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 24 | + { |
| 25 | + return $this->generateFakeLocations($query); |
| 26 | + } |
| 27 | + |
| 28 | + public function reverseQuery(ReverseQuery $query): Collection |
| 29 | + { |
| 30 | + return $this->generateFakeLocations($query); |
| 31 | + } |
| 32 | + |
| 33 | + public function getName(): string |
| 34 | + { |
| 35 | + return self::PROVIDER_NAME; |
| 36 | + } |
| 37 | + |
| 38 | + private function generateFakeLocations(Query $query): Collection |
| 39 | + { |
| 40 | + $faker = Factory::create($query->getLocale() ?? Factory::DEFAULT_LOCALE); |
| 41 | + |
| 42 | + $results = []; |
| 43 | + |
| 44 | + $i = 0; |
| 45 | + while ($i < $query->getLimit()) { |
| 46 | + $builder = new AddressBuilder($this->getName()); |
| 47 | + $builder |
| 48 | + ->setCoordinates($faker->latitude(), $faker->longitude()) |
| 49 | + ->setStreetNumber($faker->buildingNumber()) |
| 50 | + ->setStreetName($faker->streetName()) |
| 51 | + ->setPostalCode($faker->postcode()) |
| 52 | + ->setLocality($faker->city()) |
| 53 | + ->setCountry($faker->country()) |
| 54 | + ->setCountryCode($faker->countryCode()) |
| 55 | + ->setTimezone($faker->timezone()) |
| 56 | + ; |
| 57 | + |
| 58 | + $results[] = $builder->build(); |
| 59 | + ++$i; |
| 60 | + } |
| 61 | + |
| 62 | + return new AddressCollection($results); |
| 63 | + } |
| 64 | +} |
0 commit comments