Skip to content

Commit 15216c6

Browse files
authored
Merge pull request #274 from aSeriousDeveloper/states-contract
Contract / Interface for Models that use `HasStates` trait.
2 parents 315e237 + 5c4d47b commit 15216c6

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

docs/working-with-states/01-configuring-states.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ weight: 1
55

66
This package provides a `HasStates` trait which you can use in whatever model you want state support in. Within your codebase, each state is represented by a class, and will be serialised to the database by this package behind the scenes.
77

8-
This way you don't have to worry about whether a state is in its textual form or not: you're always working with state objects.
9-
108
```php
119
use Spatie\ModelStates\HasStates;
1210

@@ -158,3 +156,21 @@ abstract class PaymentState extends State
158156
```
159157

160158
Next up, we'll take a moment to discuss how state classes are serialized to the database.
159+
160+
## Improved typehinting
161+
162+
Optionally, for improved type-hinting, the package also provides a `HasStatesContract` interface.
163+
164+
This way you don't have to worry about whether a state is in its textual form or not: you're always working with state objects.
165+
166+
```php
167+
use Spatie\ModelStates\HasStates;
168+
use Spatie\ModelStates\HasStatesContract;
169+
170+
class Payment extends Model implements HasStatesContract
171+
{
172+
use HasStates;
173+
174+
// …
175+
}
176+
```

src/HasStatesContract.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Spatie\ModelStates;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Support\Collection;
7+
8+
interface HasStatesContract
9+
{
10+
public static function bootHasStates(): void;
11+
12+
public function initializeHasStates(): void;
13+
14+
public static function getStates(): Collection;
15+
16+
public static function getDefaultStates(): Collection;
17+
18+
public static function getDefaultStateFor(string $fieldName): ?string;
19+
20+
public static function getStatesFor(string $fieldName): Collection;
21+
22+
public function scopeWhereState(Builder $builder, string $column, $states): Builder;
23+
24+
public function scopeWhereNotState(Builder $builder, string $column, $states): Builder;
25+
26+
public function scopeOrWhereState(Builder $builder, string $column, $states): Builder;
27+
28+
public function scopeOrWhereNotState(Builder $builder, string $column, $states): Builder;
29+
}

0 commit comments

Comments
 (0)