Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion config/stateful-resources.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php

return [
//
/*
|--------------------------------------------------------------------------
| Custom States
|--------------------------------------------------------------------------
|
| Below you may register custom resource states that you want to use inside
| your stateful resources. Each state must be a valid enum class that
| implements the ResourceState interface and uses the AsResourceState
| trait.
|
*/
'custom_states' => [
// App\Enums\CustomResourceState::class,
],
];
7 changes: 6 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Farbcode\StatefulResources;

use Farbcode\StatefulResources\Enums\ResourceState;
use Farbcode\StatefulResources\Contracts\ResourceState;
use Illuminate\Support\Facades\Context;

/**
* Builder for creating resource instances with a specific state.
*
* @internal
*/
class Builder
{
private string $resourceClass;
Expand Down
22 changes: 22 additions & 0 deletions src/Concerns/AsResourceState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Farbcode\StatefulResources\Concerns;

trait AsResourceState
{
/**
* Get the string value of the state.
*/
public function value(): string
{
return $this->value;
}

/**
* Get the name of the state.
*/
public function name(): string
{
return $this->name;
}
}
10 changes: 8 additions & 2 deletions src/Concerns/StatefullyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Farbcode\StatefulResources\Concerns;

use Farbcode\StatefulResources\Enums\ResourceState;
use Farbcode\StatefulResources\Contracts\ResourceState;
use Farbcode\StatefulResources\StateRegistry;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
use Illuminate\Http\Resources\MergeValue;
use Illuminate\Http\Resources\MissingValue;
Expand Down Expand Up @@ -155,7 +156,12 @@ public function __call($method, $parameters)
continue;
}

return $this->{$singleStateMethod}(ResourceState::from($state), ...$parameters);
$stateInstance = StateRegistry::tryFrom($state);
if ($stateInstance === null) {
continue;
}

return $this->{$singleStateMethod}($stateInstance, ...$parameters);
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/Contracts/ResourceState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Farbcode\StatefulResources\Contracts;

interface ResourceState
{
/**
* Get the string value of the state.
*/
public function value(): string;

/**
* Get the name of the state.
*/
public function name(): string;

/**
* Create a state instance from a string value.
*/
public static function from(string $value): static;

/**
* Try to create a state instance from a string value.
*/
public static function tryFrom(string $value): ?static;

/**
* Get all available states.
*/
public static function cases(): array;
}
10 changes: 0 additions & 10 deletions src/Enums/ResourceState.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Enums/Variant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Farbcode\StatefulResources\Enums;

use Farbcode\StatefulResources\Concerns\AsResourceState;
use Farbcode\StatefulResources\Contracts\ResourceState;

enum Variant: string implements ResourceState
{
use AsResourceState;

case Minimal = 'minimal';
case Table = 'table';
case Full = 'full';
}
76 changes: 76 additions & 0 deletions src/StateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Farbcode\StatefulResources;

use Farbcode\StatefulResources\Contracts\ResourceState;
use InvalidArgumentException;

/**
* @internal
*/
class StateRegistry
{
private static array $stateClasses = [];

/**
* Register a state enum class.
*/
public static function register(string $stateClass): void
{
if (! is_subclass_of($stateClass, ResourceState::class)) {
throw new InvalidArgumentException("State class {$stateClass} must implement the ResourceState interface.");
}

self::$stateClasses[] = $stateClass;
}

/**
* Try to find a state by value across all registered state classes.
*/
public static function tryFrom(string $value): ?ResourceState
{
foreach (self::$stateClasses as $stateClass) {
$state = $stateClass::tryFrom($value);
if ($state !== null) {
return $state;
}
}

return null;
}

/**
* Find a state by value across all registered state classes.
*/
public static function from(string $value): ResourceState
{
$state = self::tryFrom($value);

if ($state === null) {
throw new InvalidArgumentException("Unknown state: {$value}");
}

return $state;
}

/**
* Get all available states from all registered classes.
*/
public static function all(): array
{
$states = [];
foreach (self::$stateClasses as $stateClass) {
$states = array_merge($states, $stateClass::cases());
}

return $states;
}

/**
* Clear all registered state classes.
*/
public static function clear(): void
{
self::$stateClasses = [];
}
}
7 changes: 4 additions & 3 deletions src/StatefulJsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Farbcode\StatefulResources;

use Farbcode\StatefulResources\Concerns\StatefullyLoadsAttributes;
use Farbcode\StatefulResources\Enums\ResourceState;
use Farbcode\StatefulResources\Contracts\ResourceState;
use Farbcode\StatefulResources\Enums\Variant;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Context;

Expand Down Expand Up @@ -41,7 +42,7 @@ protected function getState(): ResourceState
*/
public function __construct($resource)
{
$this->state = Context::get('resource-state-'.static::class, ResourceState::Full);
$this->state = Context::get('resource-state-'.static::class, Variant::Full);
parent::__construct($resource);
}

Expand All @@ -56,7 +57,7 @@ public function __construct($resource)
*/
public static function __callStatic($method, $parameters)
{
$state = ResourceState::tryFrom($method);
$state = StateRegistry::tryFrom($method);

if ($state === null) {
return parent::__callStatic($method, $parameters);
Expand Down
18 changes: 18 additions & 0 deletions src/StatefulResourcesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Farbcode\StatefulResources;

use Farbcode\StatefulResources\Enums\Variant;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -18,4 +19,21 @@ public function configurePackage(Package $package): void
->name('stateful-resources')
->hasConfigFile();
}

public function bootingPackage(): void
{
$customStates = config()->array('stateful-resources.custom_states');

$this->app->singleton(StateRegistry::class, function () use ($customStates) {
$registry = new StateRegistry;

$registry->register(Variant::class);

foreach ($customStates as $stateClass) {
$registry->register($stateClass);
}

return $registry;
});
}
}
25 changes: 25 additions & 0 deletions tests/Feature/CustomStatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Workbench\App\Enums\CustomResourceStates;
use Workbench\App\Http\Resources\CatResource;
use Workbench\App\Models\Cat;
use Workbench\Database\Factories\CatFactory;

beforeEach(function () {
CatFactory::new()->createOne();
});

it('can use a custom user-defined state', function () {
/** @var TestCase $this */
$cat = Cat::firstOrFail();

$resource = CatResource::state(CustomResourceStates::Custom)->make($cat)->toJson();

expect($resource)->toBeJson();

expect($resource)->json()->toEqual([
'id' => $cat->id,
'name' => $cat->name,
'custom_field' => 'custom_value',
]);
});
75 changes: 75 additions & 0 deletions tests/Feature/DefaultStatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Farbcode\StatefulResources\Enums\Variant;
use Farbcode\StatefulResources\Tests\TestCase;
use Workbench\App\Http\Resources\CatResource;
use Workbench\App\Models\Cat;
use Workbench\Database\Factories\CatFactory;

beforeEach(function () {
CatFactory::new()->createOne();
});

it('can return a stateful resource with the default state', function () {
/** @var TestCase $this */
$cat = Cat::firstOrFail();

$resource = CatResource::make($cat)->toJson();

expect($resource)->toBeJson();

expect($resource)->json()->toEqual([
'id' => $cat->id,
'name' => $cat->name,
'breed' => $cat->breed,
'fluffyness' => $cat->fluffyness,
'color' => $cat->color,
]);

});

it('can return a stateful resource with the correct "full" state', function () {
/** @var TestCase $this */
$cat = Cat::firstOrFail();

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

expect($resource)->toBeJson();

expect($resource)->json()->toEqual([
'id' => $cat->id,
'name' => $cat->name,
'breed' => $cat->breed,
'fluffyness' => $cat->fluffyness,
'color' => $cat->color,
]);
});

it('can use a stateful resource with the "minimal" state', function () {
/** @var TestCase $this */
$cat = Cat::firstOrFail();

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

expect($resource)->toBeJson();

expect($resource)->json()->toEqual([
'id' => $cat->id,
'name' => $cat->name,
]);
});

it('can use a stateful resource with the "table" state', function () {
/** @var TestCase $this */
$cat = Cat::firstOrFail();

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

expect($resource)->toBeJson();

expect($resource)->json()->toEqual([
'id' => $cat->id,
'name' => $cat->name,
'breed' => $cat->breed,
]);
});
Loading
Loading