Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #570 from actuallymab/master
Browse files Browse the repository at this point in the history
min / max for lat long values
  • Loading branch information
fzaninotto committed Feb 23, 2016
2 parents a4037c4 + c3c586d commit 0b81c70
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
30 changes: 15 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,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`

Expand Down
28 changes: 22 additions & 6 deletions src/Faker/Provider/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,36 @@ public static function country()
}

/**
* @example 77.147489
* @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 = -90, $max = 90)
{
return static::randomFloat(6, 0, 180) - 90;
return static::randomFloat(6, $min, $max);
}

/**
* @example 86.211205
* @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 = -180, $max = 180)
{
return static::randomFloat(6, 0, 360) - 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()
);
}
}
18 changes: 5 additions & 13 deletions src/Faker/Provider/me_ME/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,11 @@ public function cityName()
return static::randomElement(static::$cityNames);
}

/**
* @example '77.147489'
*/
public static function latitude()
{
return number_format(mt_rand(42430000, 42450000)/1000000, 6);
}

/**
* @example '86.211205'
*/
public static function longitude()
public static function localCoordinates()
{
return number_format(mt_rand(19260000, 19270000)/1000000, 6);
return array(
'latitude' => static::latitude(42.43, 42.45),
'longitude' => static::longitude(19.16, 19.27)
);
}
}
12 changes: 12 additions & 0 deletions test/Faker/Provider/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
3 changes: 2 additions & 1 deletion test/test.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
require __DIR__ .'/../vendor/autoload.php';

date_default_timezone_set('GMT');
$faker = Faker\Factory::create();
$faker->seed(5);

Expand All @@ -18,6 +18,7 @@
<city><?php echo $faker->city ?></city>
<postcode><?php echo $faker->postcode ?></postcode>
<state><?php echo $faker->state ?></state>
<coordinate><?php echo $faker->localCoordinates['latitude'].','.$faker->localCoordinates['longitude'] ?></coordinate>
</address>
<company name="<?php echo $faker->company ?>" catchPhrase="<?php echo $faker->catchPhrase ?>">
<?php if ($faker->boolean(33)): ?>
Expand Down

0 comments on commit 0b81c70

Please sign in to comment.