2
2
3
3
namespace Farbcode \StatefulResources ;
4
4
5
+ use Farbcode \StatefulResources \Concerns \ResolvesState ;
6
+ use Farbcode \StatefulResources \Contracts \ResourceState ;
7
+
5
8
/**
6
9
* ActiveState manages which state is currently active for resources.
7
10
*/
8
11
class ActiveState
9
12
{
13
+ use ResolvesState;
14
+
10
15
private ?string $ sharedState ;
11
16
12
17
private array $ resourceStates = [];
@@ -19,8 +24,9 @@ public function __construct()
19
24
/**
20
25
* Set the shared state for all resources.
21
26
*/
22
- public function setShared (string $ state ): void
27
+ public function setShared (string | ResourceState $ state ): void
23
28
{
29
+ $ state = $ this ->resolveState ($ state );
24
30
$ this ->sharedState = $ state ;
25
31
}
26
32
@@ -32,11 +38,21 @@ public function getShared(): string
32
38
return $ this ->sharedState ?? app (StateRegistry::class)->getDefaultState ();
33
39
}
34
40
41
+ /**
42
+ * Check if a specific resource class has a state set.
43
+ */
44
+ public function matchesShared (string |ResourceState $ state ): bool
45
+ {
46
+ $ state = $ this ->resolveState ($ state );
47
+ return $ this ->getShared () === $ state ;
48
+ }
49
+
35
50
/**
36
51
* Set the state for a specific resource class.
37
52
*/
38
- public function setForResource (string $ resourceClass , string $ state ): void
53
+ public function setForResource (string $ resourceClass , string | ResourceState $ state ): void
39
54
{
55
+ $ state = $ this ->resolveState ($ state );
40
56
$ this ->resourceStates [$ resourceClass ] = $ state ;
41
57
}
42
58
@@ -47,4 +63,64 @@ public function getForResource(string $resourceClass): string
47
63
{
48
64
return $ this ->resourceStates [$ resourceClass ] ?? app (StateRegistry::class)->getDefaultState ();
49
65
}
66
+
67
+ /**
68
+ * Check if the current state matches the resource's state.
69
+ */
70
+ public function matchesResource (string $ resourceClass , string |ResourceState $ state ): bool
71
+ {
72
+ $ state = $ this ->resolveState ($ state );
73
+ return $ this ->getForResource ($ resourceClass ) === $ state ;
74
+ }
75
+
76
+ /**
77
+ * Get the current state for a resource.
78
+ * If no resource class is provided, returns the shared state.
79
+ *
80
+ * @param string|null $resourceClass
81
+ */
82
+ public function get ($ resourceClass = null ): string
83
+ {
84
+ if ($ resourceClass === null ) {
85
+ return $ this ->getShared ();
86
+ }
87
+
88
+ return $ this ->getForResource ($ resourceClass );
89
+ }
90
+
91
+ /**
92
+ * Set the current state for a resource.
93
+ * If no resource class is provided, sets the shared state.
94
+ *
95
+ * @param string|ResourceState $state
96
+ * @param string|null $resourceClass
97
+ */
98
+ public function set ($ state , $ resourceClass = null ): void
99
+ {
100
+ $ state = $ this ->resolveState ($ state );
101
+
102
+ if ($ resourceClass === null ) {
103
+ $ this ->setShared ($ state );
104
+ } else {
105
+ $ this ->setForResource ($ resourceClass , $ state );
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Check if the current state matches the given state for a resource.
111
+ * If no resource class is provided, checks against the shared state.
112
+ *
113
+ * @param string|ResourceState $state
114
+ * @param string|null $resourceClass
115
+ */
116
+ public function matches ($ state , $ resourceClass = null ): bool
117
+ {
118
+ $ state = $ this ->resolveState ($ state );
119
+
120
+ if ($ resourceClass === null ) {
121
+ return $ this ->matchesShared ($ state );
122
+ }
123
+
124
+ return $ this ->matchesResource ($ resourceClass , $ state );
125
+ }
50
126
}
0 commit comments