Skip to content

Commit 8eca350

Browse files
committed
Add 'orFail' variants to query methods
1 parent afab613 commit 8eca350

8 files changed

+78
-1
lines changed

src/Entity/Country.php

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CommerceGuys\Addressing\Country\Country as BaseCountry;
88
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
99
use Galahad\LaravelAddressing\Collection\AdministrativeAreaCollection;
10+
use Galahad\LaravelAddressing\Exceptions\AdministrativeAreaNotFoundException;
1011

1112
/**
1213
* @property-read $country_code
@@ -85,6 +86,15 @@ public function administrativeArea(string $code): ?AdministrativeArea
8586
?? $this->administrativeAreas()->first(fn(AdministrativeArea $subdivision) => 0 === strcasecmp($subdivision->getCode(), $code));
8687
}
8788

89+
public function administrativeAreaOrFail(string $code): AdministrativeArea
90+
{
91+
if ($administrative_area = $this->administrativeArea($code)) {
92+
return $administrative_area;
93+
}
94+
95+
throw new AdministrativeAreaNotFoundException($this->getCountryCode(), $code);
96+
}
97+
8898
public function administrativeAreaByName(string $name): ?AdministrativeArea
8999
{
90100
return $this->administrativeAreas()

src/Entity/DecoratesEntity.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
use BadMethodCallException;
66
use Exception;
77
use Illuminate\Support\Str;
8+
use Illuminate\Support\Traits\ForwardsCalls;
9+
use Illuminate\Support\Traits\Macroable;
810

911
trait DecoratesEntity
1012
{
13+
use ForwardsCalls;
14+
use Macroable {
15+
__call as macroCall;
16+
}
17+
1118
public function __call($name, $arguments)
1219
{
13-
return $this->decoratedEntity()->$name(...$arguments);
20+
if (static::hasMacro($name)) {
21+
return $this->macroCall($name, $arguments);
22+
}
23+
24+
return $this->forwardCallTo($this->decoratedEntity(), $name, $arguments);
1425
}
1526

1627
public function __get($name)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Galahad\LaravelAddressing\Exceptions;
4+
5+
use Illuminate\Database\RecordsNotFoundException;
6+
7+
class AdministrativeAreaNotFoundException extends RecordsNotFoundException
8+
{
9+
public function __construct(string $country_code, string $administrative_area_code)
10+
{
11+
parent::__construct("Unable to find administrative area '{$administrative_area_code}' in country '{$country_code}'.");
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Galahad\LaravelAddressing\Exceptions;
4+
5+
use Illuminate\Database\RecordsNotFoundException;
6+
7+
class CountryNotFoundException extends RecordsNotFoundException
8+
{
9+
public function __construct(string $country_code)
10+
{
11+
parent::__construct("Unable to find country '{$country_code}'.");
12+
}
13+
}

src/LaravelAddressing.php

+13
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
99
use Galahad\LaravelAddressing\Collection\CountryCollection;
1010
use Galahad\LaravelAddressing\Entity\Country;
11+
use Galahad\LaravelAddressing\Exceptions\CountryNotFoundException;
12+
use Illuminate\Support\Traits\Macroable;
1113

1214
class LaravelAddressing
1315
{
16+
use Macroable;
17+
1418
protected string $locale;
1519

1620
protected string $fallback_locale;
@@ -68,6 +72,15 @@ public function country(string $country_code, ?string $locale = null): ?Country
6872

6973
return $this->countries->get($country_code, null);
7074
}
75+
76+
public function countryOrFail(string $country_code, ?string $locale = null): Country
77+
{
78+
if ($country = $this->country($country_code, $locale)) {
79+
return $country;
80+
}
81+
82+
throw new CountryNotFoundException($country_code);
83+
}
7184

7285
/**
7386
* Get all countries as a collection.

src/Support/Facades/Addressing.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @method static Country|null country($country_code, $locale = null)
12+
* @method static Country countryOrFail($country_code, $locale = null)
1213
* @method static CountryCollection countries($locale = null)
1314
* @method static Country|null countryByName($name)
1415
* @method static Country|null findCountry($input)

tests/CountryEntityTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Galahad\LaravelAddressing\Entity\AdministrativeArea;
66
use Galahad\LaravelAddressing\Entity\Country;
77
use Galahad\LaravelAddressing\Entity\Subdivision;
8+
use Galahad\LaravelAddressing\Exceptions\AdministrativeAreaNotFoundException;
89
use Galahad\LaravelAddressing\Support\Facades\Addressing;
910

1011
class CountryEntityTest extends TestCase
@@ -88,6 +89,13 @@ public function test_it_returns_null_on_invalid_administrative_areas(): void
8889
{
8990
$this->assertNull($this->country->administrativeArea('XX'));
9091
}
92+
93+
public function test_it_triggers_an_exception_when_using_or_fail_method(): void
94+
{
95+
$this->expectException(AdministrativeAreaNotFoundException::class);
96+
97+
$this->country->administrativeAreaOrFail('XX');
98+
}
9199

92100
public function test_a_administrative_area_can_be_loaded_by_name(): void
93101
{

tests/LaravelAddressingTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Galahad\LaravelAddressing\Tests;
44

55
use Galahad\LaravelAddressing\Entity\Country;
6+
use Galahad\LaravelAddressing\Exceptions\CountryNotFoundException;
67
use Galahad\LaravelAddressing\LaravelAddressing;
78

89
class LaravelAddressingTest extends TestCase
@@ -41,6 +42,13 @@ public function test_it_returns_null_when_an_unknown_country_code_is_provided():
4142
{
4243
$this->assertNull($this->addressing->country('XX'));
4344
}
45+
46+
public function test_it_triggers_an_exception_when_using_or_fail_method(): void
47+
{
48+
$this->expectException(CountryNotFoundException::class);
49+
50+
$this->addressing->countryOrFail('XX');
51+
}
4452

4553
public function test_a_country_can_be_loaded_by_its_name(): void
4654
{

0 commit comments

Comments
 (0)