Skip to content

Commit 6084474

Browse files
authored
Merge pull request #9 from jameswilddev/fix/#5-toggle-address-validation-via-config
Add strict flag to configuration (fixes #5).
2 parents c00ccb5 + 0c59a08 commit 6084474

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
composer.lock
3-
.idea
3+
.idea
4+
.DS_Store

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Note that by default, you require a Google Maps API key in order to provide
3232
address geocoding and distance calculations. If you do not wish to use geocoding,
3333
this can be disabled in the configuration.
3434

35+
### Strict geocoding
36+
37+
By default, geocoding is configured as "lenient"; if, for example, the name of a real city is given
38+
but the postcode and street address refer to a nonexistent place, it will geocode as the center of
39+
that city.
40+
41+
Set the `geocoding.strict` flag to `true` in the configuration file to instead fail to geocode in
42+
this scenario.
43+
3544
## Usage
3645

3746
Assign the `HasAddresses` trait to the model you wish to have associated addresses.

src/Models/Address.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public function geocode(): void
9292
return;
9393
}
9494

95-
$latLng = GoogleMaps::instance()
96-
->allowPartialMatches()
97-
->geocode($this->human_readable);
95+
$googleMaps = GoogleMaps::instance();
96+
97+
if (!config('addresses.geocoding.strict')) {
98+
$googleMaps = $googleMaps->allowPartialMatches();
99+
}
100+
101+
$latLng = $googleMaps->geocode($this->human_readable);
98102

99103
if ($latLng) {
100104
$this->latitude = $latLng->lat;

src/config/addresses.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'enabled' => true,
1010
'google-maps' => [
1111
'api-key' => env('GEOCODING_GOOGLE_MAPS_API_KEY'),
12-
]
12+
],
13+
'strict' => false,
1314
]
1415
];

0 commit comments

Comments
 (0)