From b9f056d68363b1af6f63f643cdb03bc67a8c2201 Mon Sep 17 00:00:00 2001 From: Mehmet Aydin Bahadir Date: Mon, 4 May 2015 17:24:16 +0300 Subject: [PATCH 1/5] min / max for lat long values Sometimes you may want to set lat/lng parameters for a spesific area. Ex: For Istanbul: 'lat' => $faker->latitude(40.958836, 41.136456), 'lng' => $faker->longitude(28.64, 29.98), --- src/Faker/Provider/Address.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Faker/Provider/Address.php b/src/Faker/Provider/Address.php index c11b5b1ed9..fbc0911a6c 100644 --- a/src/Faker/Provider/Address.php +++ b/src/Faker/Provider/Address.php @@ -104,20 +104,24 @@ public static function country() } /** - * @example 77.147489 - * @return float Uses signed degrees format (returns a float number between -90 and 90) + * @example '77.147489' + * @param int $min + * @param int $max + * @return string */ - public static function latitude() + public static function latitude($min = -90, $max = 90) { - return static::randomFloat(6, 0, 180) - 90; + return number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6); } /** - * @example 86.211205 - * @return float Uses signed degrees format (returns a float number between -180 and 180) + * @example '86.211205' + * @param int $min + * @param int $max + * @return string */ - public static function longitude() + public static function longitude($min = -180, $max = 180) { - return static::randomFloat(6, 0, 360) - 180; + return number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6); } } From 321204f2d398ca54d573adfef7036c0fd7469127 Mon Sep 17 00:00:00 2001 From: aydnbhdr Date: Thu, 7 May 2015 09:54:45 +0300 Subject: [PATCH 2/5] min / max for lat long values --- readme.md | 30 +++++++++++++++--------------- src/Faker/Provider/Address.php | 16 ++++++++-------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/readme.md b/readme.md index bb9235165d..d09cd39ea0 100644 --- a/readme.md +++ b/readme.md @@ -106,21 +106,21 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle ### `Faker\Provider\en_US\Address` - cityPrefix // 'Lake' - secondaryAddress // 'Suite 961' - state // 'NewMexico' - stateAbbr // 'OH' - citySuffix // 'borough' - streetSuffix // 'Keys' - buildingNumber // '484' - city // 'West Judge' - streetName // 'Keegan Trail' - streetAddress // '439 Karley Loaf Suite 897' - postcode // '17916' - address // '8888 Cummings Vista Apt. 101, Susanbury, NY 95473' - country // 'Falkland Islands (Malvinas)' - latitude // 77.147489 - longitude // 86.211205 + cityPrefix // 'Lake' + secondaryAddress // 'Suite 961' + state // 'NewMexico' + stateAbbr // 'OH' + citySuffix // 'borough' + streetSuffix // 'Keys' + buildingNumber // '484' + city // 'West Judge' + streetName // 'Keegan Trail' + streetAddress // '439 Karley Loaf Suite 897' + postcode // '17916' + address // '8888 Cummings Vista Apt. 101, Susanbury, NY 95473' + country // 'Falkland Islands (Malvinas)' + latitude($min = -90, $max = 90) // 77.147489 + longitude($min = -180, $max = 180) // 86.211205 ### `Faker\Provider\en_US\PhoneNumber` diff --git a/src/Faker/Provider/Address.php b/src/Faker/Provider/Address.php index fbc0911a6c..de9cd8d0ef 100644 --- a/src/Faker/Provider/Address.php +++ b/src/Faker/Provider/Address.php @@ -105,23 +105,23 @@ public static function country() /** * @example '77.147489' - * @param int $min - * @param int $max - * @return string + * @param float|int $min + * @param float|int $max + * @return float Uses signed degrees format (returns a float number between -90 and 90) */ public static function latitude($min = -90, $max = 90) { - return number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6); + return floatval(number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6)); } /** * @example '86.211205' - * @param int $min - * @param int $max - * @return string + * @param float|int $min + * @param float|int $max + * @return float Uses signed degrees format (returns a float number between -180 and 180) */ public static function longitude($min = -180, $max = 180) { - return number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6); + return floatval(number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6)); } } From f13d6401fc91a925540c80979ecb2e59cdadcbbd Mon Sep 17 00:00:00 2001 From: aydnbhdr Date: Thu, 7 May 2015 10:16:52 +0300 Subject: [PATCH 3/5] removed number_format --- src/Faker/Provider/Address.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Faker/Provider/Address.php b/src/Faker/Provider/Address.php index de9cd8d0ef..9ce7ab4de6 100644 --- a/src/Faker/Provider/Address.php +++ b/src/Faker/Provider/Address.php @@ -111,7 +111,7 @@ public static function country() */ public static function latitude($min = -90, $max = 90) { - return floatval(number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6)); + return mt_rand($min * 1000000, $max * 1000000)/1000000; } /** @@ -122,6 +122,6 @@ public static function latitude($min = -90, $max = 90) */ public static function longitude($min = -180, $max = 180) { - return floatval(number_format(mt_rand($min * 1000000, $max * 1000000)/1000000, 6)); + return mt_rand($min * 1000000, $max * 1000000)/1000000; } } From 4a764d55b0d942659c7aaf3cefad7d5d12bd3cd3 Mon Sep 17 00:00:00 2001 From: aydnbhdr Date: Fri, 22 May 2015 10:17:42 +0300 Subject: [PATCH 4/5] lat, lng converted to randomfloat func --- src/Faker/Provider/Address.php | 4 ++-- src/Faker/Provider/me_ME/Address.php | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Faker/Provider/Address.php b/src/Faker/Provider/Address.php index 9ce7ab4de6..5cc0c78105 100644 --- a/src/Faker/Provider/Address.php +++ b/src/Faker/Provider/Address.php @@ -111,7 +111,7 @@ public static function country() */ public static function latitude($min = -90, $max = 90) { - return mt_rand($min * 1000000, $max * 1000000)/1000000; + return static::randomFloat(6, $min, $max); } /** @@ -122,6 +122,6 @@ public static function latitude($min = -90, $max = 90) */ public static function longitude($min = -180, $max = 180) { - return mt_rand($min * 1000000, $max * 1000000)/1000000; + return static::randomFloat(6, $min, $max); } } diff --git a/src/Faker/Provider/me_ME/Address.php b/src/Faker/Provider/me_ME/Address.php index 5b9caf14f3..82c6a6543c 100644 --- a/src/Faker/Provider/me_ME/Address.php +++ b/src/Faker/Provider/me_ME/Address.php @@ -111,17 +111,23 @@ public function cityName() /** * @example '77.147489' + * @param float|int $min + * @param float|int $max + * @return float Uses signed degrees format (returns a float number between -90 and 90) */ - public static function latitude() + public static function latitude($min = 42.43, $max = 42.45) { - return number_format(mt_rand(42430000, 42450000)/1000000, 6); + return static::randomFloat(6, $min, $max); } /** * @example '86.211205' + * @param float|int $min + * @param float|int $max + * @return float Uses signed degrees format (returns a float number between -180 and 180) */ - public static function longitude() + public static function longitude($min = 19.16, $max = 19.27) { - return number_format(mt_rand(19260000, 19270000)/1000000, 6); + return static::randomFloat(6, $min, $max); } } From c3c586dfdbf32d94a0eac230a4be9837bbaa459e Mon Sep 17 00:00:00 2001 From: Mehmet Aydin Bahadir Date: Tue, 12 Jan 2016 18:22:28 +0200 Subject: [PATCH 5/5] localCoordinates() function added --- src/Faker/Provider/Address.php | 12 ++++++++++++ src/Faker/Provider/me_ME/Address.php | 24 +++++------------------- test/Faker/Provider/AddressTest.php | 12 ++++++++++++ test/test.php | 3 ++- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/Faker/Provider/Address.php b/src/Faker/Provider/Address.php index 5cc0c78105..6300ade106 100644 --- a/src/Faker/Provider/Address.php +++ b/src/Faker/Provider/Address.php @@ -124,4 +124,16 @@ public static function longitude($min = -180, $max = 180) { return static::randomFloat(6, $min, $max); } + + /** + * @example array('77.147489', '86.211205') + * @return array | latitude, longitude + */ + public static function localCoordinates() + { + return array( + 'latitude' => static::latitude(), + 'longitude' => static::longitude() + ); + } } diff --git a/src/Faker/Provider/me_ME/Address.php b/src/Faker/Provider/me_ME/Address.php index 82c6a6543c..422207ea2b 100644 --- a/src/Faker/Provider/me_ME/Address.php +++ b/src/Faker/Provider/me_ME/Address.php @@ -109,25 +109,11 @@ public function cityName() return static::randomElement(static::$cityNames); } - /** - * @example '77.147489' - * @param float|int $min - * @param float|int $max - * @return float Uses signed degrees format (returns a float number between -90 and 90) - */ - public static function latitude($min = 42.43, $max = 42.45) - { - return static::randomFloat(6, $min, $max); - } - - /** - * @example '86.211205' - * @param float|int $min - * @param float|int $max - * @return float Uses signed degrees format (returns a float number between -180 and 180) - */ - public static function longitude($min = 19.16, $max = 19.27) + public static function localCoordinates() { - return static::randomFloat(6, $min, $max); + return array( + 'latitude' => static::latitude(42.43, 42.45), + 'longitude' => static::longitude(19.16, 19.27) + ); } } diff --git a/test/Faker/Provider/AddressTest.php b/test/Faker/Provider/AddressTest.php index cbabce566d..5a5f66d4cc 100644 --- a/test/Faker/Provider/AddressTest.php +++ b/test/Faker/Provider/AddressTest.php @@ -31,4 +31,16 @@ public function testLongitude() $this->assertGreaterThanOrEqual(-180, $longitude); $this->assertLessThanOrEqual(180, $longitude); } + + public function testCoordinate() + { + $coordinate = $this->faker->localCoordinates(); + $this->assertInternalType('array', $coordinate); + $this->assertInternalType('float', $coordinate['latitude']); + $this->assertGreaterThanOrEqual(-90, $coordinate['latitude']); + $this->assertLessThanOrEqual(90, $coordinate['latitude']); + $this->assertInternalType('float', $coordinate['longitude']); + $this->assertGreaterThanOrEqual(-180, $coordinate['longitude']); + $this->assertLessThanOrEqual(180, $coordinate['longitude']); + } } diff --git a/test/test.php b/test/test.php index c238ab7192..9ca2c8ae56 100644 --- a/test/test.php +++ b/test/test.php @@ -1,6 +1,6 @@ seed(5); @@ -18,6 +18,7 @@ city ?> postcode ?> state ?> + localCoordinates['latitude'].','.$faker->localCoordinates['longitude'] ?> boolean(33)): ?>