Skip to content

Commit 68c362b

Browse files
refactor: rename Variant enum to State
1 parent d6c9da6 commit 68c362b

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

config/stateful-resources.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Farbcode\StatefulResources\Enums\Variant;
3+
use Farbcode\StatefulResources\Enums\State;
44

55
return [
66
/*
@@ -14,7 +14,7 @@
1414
|
1515
*/
1616
'states' => [
17-
...Variant::cases(),
17+
...State::cases(),
1818
//
1919
],
2020

@@ -27,5 +27,5 @@
2727
| If not set, the first state in the states array will be used.
2828
|
2929
*/
30-
'default_state' => Variant::Full,
30+
'default_state' => State::Full,
3131
];

docs/pages/basic-usage.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class UserResource extends StatefulJsonResource
3636

3737
## Built-in States
3838

39-
The package comes with three built-in states defined in the `Variant` enum:
39+
The package comes with three built-in states defined in the `State` enum:
4040

4141
- **`Full`** - For all available attributes
4242
- **`Table`** - For attributes suitable for table/listing views
@@ -53,7 +53,7 @@ Inside your stateful resource, you can use conditional methods to control which
5353

5454
namespace App\Http\Resources;
5555

56-
use Farbcode\StatefulResources\Enums\Variant;
56+
use Farbcode\StatefulResources\Enums\State;
5757
use Farbcode\StatefulResources\StatefulJsonResource;
5858
use Illuminate\Http\Request;
5959

@@ -64,14 +64,14 @@ class UserResource extends StatefulJsonResource
6464
return [
6565
'id' => $this->id,
6666
'name' => $this->name,
67-
'email' => $this->whenState(Variant::Full, $this->email),
68-
'profile' => $this->whenStateIn([Variant::Full], [
67+
'email' => $this->whenState(State::Full, $this->email),
68+
'profile' => $this->whenStateIn([State::Full], [
6969
'bio' => $this->bio,
7070
'avatar' => $this->avatar,
7171
'created_at' => $this->created_at,
7272
]),
73-
'role' => $this->whenStateIn([Variant::Full, Variant::Table], $this->role),
74-
'last_login' => $this->unlessState(Variant::Minimal, $this->last_login_at),
73+
'role' => $this->whenStateIn([State::Full, State::Table], $this->role),
74+
'last_login' => $this->unlessState(State::Minimal, $this->last_login_at),
7575
];
7676
}
7777
}
@@ -93,24 +93,24 @@ The package provides several methods to conditionally include attributes:
9393
Include a value only when the current state matches the specified state:
9494

9595
```php
96-
'email' => $this->whenState(Variant::Full, $this->email),
97-
'admin_notes' => $this->whenState(Variant::Full, $this->admin_notes, 'N/A'),
96+
'email' => $this->whenState(State::Full, $this->email),
97+
'admin_notes' => $this->whenState(State::Full, $this->admin_notes, 'N/A'),
9898
```
9999

100100
### `unlessState`
101101

102102
Include a value unless the current state matches the specified state:
103103

104104
```php
105-
'public_info' => $this->unlessState(Variant::Minimal, $this->public_information),
105+
'public_info' => $this->unlessState(State::Minimal, $this->public_information),
106106
```
107107

108108
### `whenStateIn`
109109

110110
Include a value when the current state is one of the specified states:
111111

112112
```php
113-
'detailed_info' => $this->whenStateIn([Variant::Full, Variant::Table], [
113+
'detailed_info' => $this->whenStateIn([State::Full, State::Table], [
114114
'department' => $this->department,
115115
'position' => $this->position,
116116
]),
@@ -121,7 +121,7 @@ Include a value when the current state is one of the specified states:
121121
Include a value unless the current state is one of the specified states:
122122

123123
```php
124-
'sensitive_data' => $this->unlessStateIn([Variant::Minimal, Variant::Table], $this->sensitive_info),
124+
'sensitive_data' => $this->unlessStateIn([State::Minimal, State::Table], $this->sensitive_info),
125125
```
126126

127127
### Magic Conditionals
@@ -140,15 +140,15 @@ You can also use magic methods with for cleaner syntax:
140140
Use the static `state()` method to create a resource with a specific state:
141141

142142
```php
143-
$user = UserResource::state(Variant::Minimal)->make($user);
143+
$user = UserResource::state(State::Minimal)->make($user);
144144
```
145145

146146
### Using Magic Methods
147147

148148
You can also use magic methods for a more fluent syntax:
149149

150150
```php
151-
// This is equivalent to the explicit state() call
151+
// This is equivalent to the explicit state(State::Minimal) call
152152
$user = UserResource::minimal()->make($user);
153153
```
154154

docs/pages/extending-states.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Extending States
22

3-
You may find yourself being too limited with the three Variant states included in the package's `Variant` enum.
3+
You may find yourself being too limited with the three State states included in the package's `State` enum.
44
This package allows you to register custom states that you can then use in your resources.
55

66
## Registering Custom States
@@ -9,10 +9,11 @@ Before using a custom state, register it in the package's `stateful-resources.st
99

1010
```php
1111
<?php
12+
use Farbcode\StatefulResources\Enums\State;
1213

1314
return [
1415
'states' => [
15-
...Variant::cases(), // The built-in states
16+
...State::cases(), // The built-in states
1617
'custom', // Your custom state as a string
1718
...CustomResourceState::cases(), // Or as cases of a custom enum
1819
],

src/Enums/Variant.php renamed to src/Enums/State.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Farbcode\StatefulResources\Contracts\ResourceState;
66

7-
enum Variant: string implements ResourceState
7+
enum State: string implements ResourceState
88
{
99
case Full = 'full';
1010
case Table = 'table';

tests/Feature/DefaultStatesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Farbcode\StatefulResources\Enums\Variant;
3+
use Farbcode\StatefulResources\Enums\State;
44
use Farbcode\StatefulResources\Tests\TestCase;
55
use Workbench\App\Http\Resources\CatResource;
66
use Workbench\App\Models\Cat;
@@ -32,7 +32,7 @@
3232
/** @var TestCase $this */
3333
$cat = Cat::firstOrFail();
3434

35-
$resource = CatResource::state(Variant::Full)->make($cat)->toJson();
35+
$resource = CatResource::state(State::Full)->make($cat)->toJson();
3636

3737
expect($resource)->toBeJson();
3838

@@ -49,7 +49,7 @@
4949
/** @var TestCase $this */
5050
$cat = Cat::firstOrFail();
5151

52-
$resource = CatResource::state(Variant::Minimal)->make($cat)->toJson();
52+
$resource = CatResource::state(State::Minimal)->make($cat)->toJson();
5353

5454
expect($resource)->toBeJson();
5555

@@ -63,7 +63,7 @@
6363
/** @var TestCase $this */
6464
$cat = Cat::firstOrFail();
6565

66-
$resource = CatResource::state(Variant::Table)->make($cat)->toJson();
66+
$resource = CatResource::state(State::Table)->make($cat)->toJson();
6767

6868
expect($resource)->toBeJson();
6969

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Farbcode\StatefulResources\Tests;
44

5-
use Farbcode\StatefulResources\Enums\Variant;
5+
use Farbcode\StatefulResources\Enums\State;
66
use Farbcode\StatefulResources\StatefulResourcesServiceProvider;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
88
use Orchestra\Testbench\Concerns\WithWorkbench;
@@ -29,7 +29,7 @@ protected function getPackageProviders($app)
2929
protected function getEnvironmentSetUp($app)
3030
{
3131
$app['config']->set('stateful-resources.states', [
32-
...Variant::cases(),
32+
...State::cases(),
3333
'custom',
3434
]);
3535
}

workbench/app/Http/Resources/CatResource.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Workbench\App\Http\Resources;
44

5-
use Farbcode\StatefulResources\Enums\Variant;
5+
use Farbcode\StatefulResources\Enums\State;
66
use Farbcode\StatefulResources\StatefulJsonResource;
77
use Illuminate\Http\Request;
88

@@ -18,9 +18,9 @@ public function toArray(Request $request): array
1818
return [
1919
'id' => $this->id,
2020
'name' => $this->name,
21-
'breed' => $this->whenStateIn([Variant::Full, Variant::Table], $this->breed),
22-
'fluffyness' => $this->whenStateIn([Variant::Full], $this->fluffyness),
23-
'color' => $this->whenStateIn([Variant::Full], $this->color),
21+
'breed' => $this->whenStateIn([State::Full, State::Table], $this->breed),
22+
'fluffyness' => $this->whenStateIn([State::Full], $this->fluffyness),
23+
'color' => $this->whenStateIn([State::Full], $this->color),
2424
'custom_field' => $this->whenState('custom', 'custom_value'),
2525
];
2626
}

0 commit comments

Comments
 (0)