Skip to content

Commit 9a7b592

Browse files
authored
Merge pull request #11 from francoism90/master
Add findOrFail
2 parents 970c4cb + a998089 commit 9a7b592

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ The package can retrieve the model for a given prefixed id.
1919
```php
2020
// on a specific model
2121
User::findByPrefixedId('user_fj39fj3lsmxlsl'); // returns a User model or `null`
22+
User::findByPrefixedIdOrFail('user_fj39fj3lsmxlsl'); // returns a User model or throws `NoPrefixedModelFound`
2223

2324
// automatically determine the model of a given prefixed id
24-
$user = PrefixedIds::getModel('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;
25+
$user = PrefixedIds::getModelClass('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;
2526
```
2627

2728
## Support us
@@ -84,6 +85,7 @@ Typically, you would put the code above in a service provider.
8485
### Publish the config file
8586

8687
Optionally, You can publish the config file with:
88+
8789
```bash
8890
php artisan vendor:publish --provider="Spatie\PrefixedIds\PrefixedIdsServiceProvider" --tag="prefixed-ids-config"
8991
```
@@ -115,6 +117,7 @@ You can find the model with a given prefix by calling `findByPrefixedId` on it.
115117
```php
116118
YourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel`
117119
YourModel::findByPrefixedId('non-existing-id'); // returns null
120+
YourModel::findByPrefixedIdOrFail('non-existing-id'); // throws `NoPrefixedModelFound`
118121
```
119122

120123
### Finding across models
@@ -124,6 +127,7 @@ You can call `find` on `Spatie\PrefixedIds\PrefixedIds` to automatically get the
124127
```php
125128
$yourModel = Spatie\PrefixedIds\PrefixedIds::find('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel` or `null`
126129
$otherModel = Spatie\PrefixedIds\PrefixedIds::find('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or `null`
130+
$otherModel = Spatie\PrefixedIds\PrefixedIds::findOrFail('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or throws `NoPrefixedModelFound`
127131
```
128132

129133
## Using the prefixed ids in your routes
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Spatie\PrefixedIds\Exceptions;
4+
5+
use Exception;
6+
use Facade\IgnitionContracts\BaseSolution;
7+
use Facade\IgnitionContracts\ProvidesSolution;
8+
use Facade\IgnitionContracts\Solution;
9+
10+
class NoPrefixedModelFound extends Exception implements ProvidesSolution
11+
{
12+
public static function make(string $prefixedId)
13+
{
14+
return new static("Could not find a prefixed model `{$prefixedId}`");
15+
}
16+
17+
public function getSolution(): Solution
18+
{
19+
return BaseSolution::create('Make sure the prefix of the model exists')
20+
->setSolutionDescription("You should make sure your model has a valid `prefixed_id`")
21+
->setDocumentationLinks([
22+
'Documentation' => 'https://github.com/spatie/laravel-prefixed-ids#usage',
23+
]);
24+
}
25+
}

src/Models/Concerns/HasPrefixedId.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Support\Str;
77
use Spatie\PrefixedIds\Exceptions\NoPrefixConfiguredForModel;
8+
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;
89
use Spatie\PrefixedIds\PrefixedIds;
910

1011
trait HasPrefixedId
@@ -32,6 +33,15 @@ public static function findByPrefixedId(string $prefixedId): ?Model
3233
return static::firstWhere($attributeName, $prefixedId);
3334
}
3435

36+
public static function findByPrefixedIdOrFail(string $prefixedId): Model
37+
{
38+
if (is_null($model = static::findByPrefixedId($prefixedId))) {
39+
throw NoPrefixedModelFound::make($prefixedId);
40+
}
41+
42+
return $model;
43+
}
44+
3545
protected function getIdPrefix(): string
3646
{
3747
$prefix = PrefixedIds::getPrefixForModel(static::class);

src/PrefixedIds.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\PrefixedIds;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;
67

78
class PrefixedIds
89
{
@@ -41,6 +42,15 @@ public static function find(string $prefixedId): ?Model
4142
return $modelClass::findByPrefixedId($prefixedId);
4243
}
4344

45+
public static function findOrFail(string $prefixedId): ?Model
46+
{
47+
if (! $modelClass = static::getModelClass($prefixedId)) {
48+
throw NoPrefixedModelFound::make($prefixedId);
49+
}
50+
51+
return $modelClass::findByPrefixedIdOrFail($prefixedId);
52+
}
53+
4454
public static function getModelClass(string $prefixedId): ?string
4555
{
4656
foreach (static::$registeredModels as $prefix => $modelClass) {

tests/PrefixedIdsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\PrefixedIds\Tests;
44

55
use Spatie\PrefixedIds\Exceptions\NoPrefixConfiguredForModel;
6+
use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound;
67
use Spatie\PrefixedIds\PrefixedIds;
78
use Spatie\PrefixedIds\Tests\TestClasses\Models\OtherTestModel;
89
use Spatie\PrefixedIds\Tests\TestClasses\Models\TestModel;
@@ -78,4 +79,44 @@ public function it_can_find_the_right_model_for_the_given_prefixed_id()
7879
$nonExistingModel = PrefixedIds::find('non-existing-id');
7980
$this->assertNull($nonExistingModel);
8081
}
82+
83+
/** @test */
84+
public function a_model_can_find_or_fail_a_record_with_the_given_prefixed_id()
85+
{
86+
$testModel = TestModel::create();
87+
88+
$foundModel = TestModel::findByPrefixedIdOrFail($testModel->prefixed_id);
89+
$this->assertEquals($testModel->id, $foundModel->id);
90+
}
91+
92+
/** @test */
93+
public function it_throws_exception_on_invalid_prefixed_id()
94+
{
95+
$this->expectException(NoPrefixedModelFound::class);
96+
97+
TestModel::findByPrefixedIdOrFail('non_existing');
98+
}
99+
100+
// /** @test */
101+
public function it_can_find_or_fail_the_right_model_for_the_given_prefixed_id()
102+
{
103+
$testModel = TestModel::create();
104+
$otherTestModel = OtherTestModel::create();
105+
106+
$foundModel = PrefixedIds::findOrFail($testModel->prefixed_id);
107+
$this->assertInstanceOf(TestModel::class, $foundModel);
108+
$this->assertEquals($testModel->id, $foundModel->id);
109+
110+
$otherFoundModel = PrefixedIds::findOrFail($otherTestModel->prefixed_id);
111+
$this->assertInstanceOf(OtherTestModel::class, $otherFoundModel);
112+
$this->assertEquals($testModel->id, $otherFoundModel->id);
113+
}
114+
115+
// /** @test */
116+
public function it_throws_exception_on_invalid_given_model_prefixed_id()
117+
{
118+
$this->expectException(NoPrefixedModelFound::class);
119+
120+
PrefixedIds::findOrFail('non-existing-id');
121+
}
81122
}

0 commit comments

Comments
 (0)