Skip to content

Commit 9f3414a

Browse files
committed
Add static constructor method
1 parent c66168c commit 9f3414a

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ But what if you are sending a custom key in a POST request and you want to valid
5959

6060
Let's say you have a model with an ID of `1`, but `getRouteKey()` returns the encoded value of `1234`.
6161

62-
In your validation rules, new up `\CodeZero\RouteKeyExists\RouteKeyExists` and pass the model's class name to the constructor:
62+
In your validation rules, pass your model's class name to `\CodeZero\RouteKeyExists\RouteKeyExists`:
6363

6464
```php
6565
request()->validate([
66-
'model_id' => new RouteKeyExists(Model::class),
66+
'model_id' => RouteKeyExists::model(Model::class),
6767
]);
6868
```
6969

@@ -73,7 +73,7 @@ Possibly, you will need the actual ID to work with when validation passes. Tack
7373

7474
```php
7575
request()->validate([
76-
'model_id' => (new RouteKeyExists(Model::class))->replace(),
76+
'model_id' => RouteKeyExists::model(Model::class)->replace(),
7777
]);
7878

7979
$id = request('model_id');
@@ -83,7 +83,7 @@ Or maybe you want to keep the encoded ID in the request, but add the actual ID a
8383

8484
```php
8585
request()->validate([
86-
'model_id' => (new RouteKeyExists(Model::class))->add('actual_id'),
86+
'model_id' => RouteKeyExists::model(Model::class)->add('actual_id'),
8787
]);
8888

8989
$id = request('actual_id');

src/RouteKeyExists.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ class RouteKeyExists implements Rule
3636
*/
3737
protected $addAttribute = null;
3838

39+
/**
40+
* Create a new rule instance.
41+
*
42+
* @param string $model
43+
*
44+
* @return static
45+
*/
46+
public static function model($model)
47+
{
48+
return new static($model);
49+
}
50+
3951
/**
4052
* Create a new rule instance.
4153
*

tests/RouteKeyExistsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp()
2424
public function it_resolves_a_model_with_custom_route_key()
2525
{
2626
$this->createRoute(
27-
new RouteKeyExists(Model::class)
27+
RouteKeyExists::model(Model::class)
2828
);
2929

3030
$this->validate('foo-9999')
@@ -42,7 +42,7 @@ public function it_resolves_a_model_with_custom_route_key()
4242
public function it_replaces_the_route_key_in_the_request_with_the_original_route_key()
4343
{
4444
$this->createRoute(
45-
(new RouteKeyExists(Model::class))->replace()
45+
RouteKeyExists::model(Model::class)->replace()
4646
);
4747

4848
$response = $this->validate("foo-{$this->model->id}")
@@ -57,7 +57,7 @@ public function it_replaces_the_route_key_in_the_request_with_the_original_route
5757
public function it_adds_the_original_route_key_to_the_request()
5858
{
5959
$this->createRoute(
60-
(new RouteKeyExists(Model::class))->add('database_key')
60+
RouteKeyExists::model(Model::class)->add('database_key')
6161
);
6262

6363
$response = $this->validate("foo-{$this->model->id}")

0 commit comments

Comments
 (0)