Skip to content

Commit 410093d

Browse files
authored
Merge pull request #119 from alhelwany/main
implement enum based statuses
2 parents 4f2ed7e + b0a7945 commit 410093d

12 files changed

+302
-190
lines changed

README.md

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ This package provides a `HasStatuses` trait that, once installed on a model, all
1111

1212
```php
1313
// set a status
14-
$model->setStatus('pending', 'needs verification');
14+
$model->setStatus(ModelStatus::Pending, 'needs verification');
1515

1616
// set another status
17-
$model->setStatus('accepted');
17+
$model->setStatus(ModelStatus::Approved);
1818

1919
// specify a reason
20-
$model->setStatus('rejected', 'My rejection reason');
20+
$model->setStatus(ModelStatus::Rejected, 'My rejection reason');
2121

22-
// get the current status
22+
// get the current status model
2323
$model->status(); // returns an instance of \Spatie\ModelStatus\Status
2424

25+
//get the current status
26+
$model->status; //returns an instance of the status enum
27+
2528
// get the previous status
26-
$latestPendingStatus = $model->latestStatus('pending');
29+
$latestPendingStatus = $model->latestStatus(ModelStatus::Pending);
2730

2831
$latestPendingStatus->reason; // returns 'needs verification'
2932
```
@@ -84,14 +87,31 @@ return [
8487

8588
## Usage
8689

87-
Add the `HasStatuses` trait to a model you like to use statuses on.
90+
Create The Enum that holds all of your available statuses for the model. \
91+
The enum must be string backed
92+
93+
```php
94+
enum ModelStatusEnum: string
95+
{
96+
case Pending = "pending";
97+
case Approved = "approved";
98+
case Rejected = "rejected";
99+
}
100+
```
101+
102+
Add the `HasStatuses` trait to a model you like to use statuses on and implement the abstract function getStatusEnumClass. Inside, you must return the class name of the enum you desire to use with this model.
88103

89104
```php
90105
use Spatie\ModelStatus\HasStatuses;
91106

92107
class YourEloquentModel extends Model
93108
{
94109
use HasStatuses;
110+
111+
public static function getStatusEnumClass(): string
112+
{
113+
return ModelStatusEnum::class;
114+
}
95115
}
96116
```
97117

@@ -100,45 +120,41 @@ class YourEloquentModel extends Model
100120
You can set a new status like this:
101121

102122
```php
103-
$model->setStatus('status-name');
123+
$model->setStatus(ModelStatusEnum::Pending);
104124
```
105125

106126
A reason for the status change can be passed as a second argument.
107127

108128
```php
109-
$model->setStatus('status-name', 'optional reason');
129+
$model->setStatus(ModelStatusEnum::Approved, 'optional reason');
110130
```
111131

112132
### Retrieving statuses
113133

114134
You can get the current status of model:
115135

116136
```php
117-
$model->status; // returns a string with the name of the latest status
137+
$model->status; // returns an enum instance of the latest status assigned to the model
118138

119139
$model->status(); // returns the latest instance of `Spatie\ModelStatus\Status`
120140

121141
$model->latestStatus(); // equivalent to `$model->status()`
122142
```
123143

124-
You can also get latest status of a given name:
144+
You can also get latest status of a given value:
125145

126146
```php
127-
$model->latestStatus('pending'); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending`
147+
$model->latestStatus(ModelStatusEnum::Approved); // returns an instance of `Spatie\ModelStatus\Status` that has the value `approved`
128148
```
129-
Get all available status names for the model.
130149

131-
```php
132-
$statusNames = $model->getStatusNames(); // returns a collection of all available status names.
133-
```
134150

135-
The following examples will return statusses of type `status 1` or `status 2`, whichever is latest.
151+
The following examples will return statusses of type Approved or Rejected, whichever is latest.
136152

137153
```php
138-
$lastStatus = $model->latestStatus(['status 1', 'status 2']);
154+
$lastStatus = $model->latestStatus([ModelStatusEnum::Approved, ModelStatusEnum::Rejected]);
139155

140156
// or alternatively...
141-
$lastStatus = $model->latestStatus('status 1', 'status 2');
157+
$lastStatus = $model->latestStatus(ModelStatusEnum::Approved, ModelStatusEnum::Rejected);
142158
```
143159

144160
All associated statuses of a model can be retrieved like this:
@@ -149,45 +165,45 @@ $allStatuses = $model->statuses;
149165
This will check if the model has status:
150166

151167
```php
152-
$model->setStatus('status1');
168+
$model->setStatus(ModelStatusEnum::Approved);
153169

154-
$isStatusExist = $model->hasStatus('status1'); // return true
155-
$isStatusExist = $model->hasStatus('status2'); // return false
170+
$isStatusExist = $model->hasStatus(ModelStatusEnum::Approved); // return true
171+
$isStatusExist = $model->hasStatus(ModelStatusEnum::Rejected); // return false
156172
```
157173
### Retrieving models with a given latest state
158174

159-
The `currentStatus` scope will return models that have a status with the given name.
175+
The `currentStatus` scope will return models that have a status with the given value.
160176

161177
```php
162-
$allPendingModels = Model::currentStatus('pending');
178+
$allPendingModels = Model::currentStatus(ModelStatusEnum::Approved);
163179

164180
//or array of statuses
165-
$allPendingModels = Model::currentStatus(['pending', 'initiated']);
166-
$allPendingModels = Model::currentStatus('pending', 'initiated');
181+
$allPendingModels = Model::currentStatus([ModelStatusEnum::Approved, ModelStatusEnum::Pending]);
182+
$allPendingModels = Model::currentStatus(ModelStatusEnum::Approved, ModelStatusEnum::Pending);
167183
```
168184

169-
### Retrieving models without a given state
185+
### Retrieving models without a given status
170186

171187
The `otherCurrentStatus` scope will return all models that do not have a status with the given name, including any model that does not have any statuses associated with them.
172188

173189
```php
174-
$allNonPendingModels = Model::otherCurrentStatus('pending');
190+
$allNonPendingModels = Model::otherCurrentStatus(ModelStatusEnum::Pending);
175191
```
176192

177193
You can also provide an array of status names to exclude from the query.
178194
```php
179-
$allNonInitiatedOrPendingModels = Model::otherCurrentStatus(['initiated', 'pending']);
195+
$allNonPendingOrRejectedModels = Model::otherCurrentStatus([ModelStatusEnum::Pending, ModelStatusEnum::Rejected]);
180196

181197
// or alternatively...
182-
$allNonInitiatedOrPendingModels = Model::otherCurrentStatus('initiated', 'pending');
198+
$allNonPendingOrRejectedModels = Model::otherCurrentStatus(ModelStatusEnum::Pending, ModelStatusEnum::Rejected);
183199
```
184200

185201
### Validating a status before setting it
186202

187203
You can add custom validation when setting a status by overriding the `isValidStatus` method:
188204

189205
```php
190-
public function isValidStatus(string $name, ?string $reason = null): bool
206+
public function isValidStatus($statusEnum, ?string $reason = null): bool
191207
{
192208
...
193209

@@ -204,15 +220,15 @@ If `isValidStatus` returns `false` a `Spatie\ModelStatus\Exceptions\InvalidStatu
204220
You may bypass validation with the `forceSetStatus` method:
205221

206222
```php
207-
$model->forceSetStatus('invalid-status-name');
223+
$model->forceSetStatus(ModelStatusEnum::Pending);
208224
```
209225

210226
### Check if status has been assigned
211227

212228
You can check if a specific status has been set on the model at any time by using the `hasEverHadStatus` method:
213229

214230
```php
215-
$model->hasEverHadStatus('status 1');
231+
$model->hasEverHadStatus(ModelStatusEnum::Approved);
216232
```
217233

218234
### Delete status from model
@@ -222,13 +238,13 @@ You can delete any given status that has been set on the model at any time by us
222238
Delete single status from model:
223239

224240
```php
225-
$model->deleteStatus('status 1');
241+
$model->deleteStatus(ModelStatusEnum::Rejected);
226242
```
227243

228244
Delete multiple statuses from model at once:
229245

230246
```php
231-
$model->deleteStatus(['status 1', 'status 2']);
247+
$model->deleteStatus([ModelStatusEnum::Pending, ModelStatusEnum::Rejected]);
232248
```
233249

234250
### Events

src/Exceptions/InvalidEnumClass.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\ModelStatus\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidEnumClass extends Exception
8+
{
9+
public static function create(string $statusEnum): self
10+
{
11+
return new self("The status is not of type $statusEnum");
12+
}
13+
}

src/Exceptions/InvalidEnumType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\ModelStatus\Exceptions;
4+
5+
use Exception;
6+
7+
class InvalidEnumType extends Exception
8+
{
9+
public static function create(string $statusEnumClass): self
10+
{
11+
return new self("Enum `{$statusEnumClass}` must be a string backed Enum");
12+
}
13+
}

0 commit comments

Comments
 (0)