22
33namespace Farbcode \StatefulResources ;
44
5+ use Farbcode \StatefulResources \Concerns \ResolvesState ;
6+ use Farbcode \StatefulResources \Contracts \ResourceState ;
7+
58/**
69 * ActiveState manages which state is currently active for resources.
710 */
811class ActiveState
912{
13+ use ResolvesState;
14+
1015 private ?string $ sharedState ;
1116
1217 private array $ resourceStates = [];
@@ -19,8 +24,9 @@ public function __construct()
1924 /**
2025 * Set the shared state for all resources.
2126 */
22- public function setShared (string $ state ): void
27+ public function setShared (string | ResourceState $ state ): void
2328 {
29+ $ state = $ this ->resolveState ($ state );
2430 $ this ->sharedState = $ state ;
2531 }
2632
@@ -32,11 +38,21 @@ public function getShared(): string
3238 return $ this ->sharedState ?? app (StateRegistry::class)->getDefaultState ();
3339 }
3440
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+
3550 /**
3651 * Set the state for a specific resource class.
3752 */
38- public function setForResource (string $ resourceClass , string $ state ): void
53+ public function setForResource (string $ resourceClass , string | ResourceState $ state ): void
3954 {
55+ $ state = $ this ->resolveState ($ state );
4056 $ this ->resourceStates [$ resourceClass ] = $ state ;
4157 }
4258
@@ -47,4 +63,64 @@ public function getForResource(string $resourceClass): string
4763 {
4864 return $ this ->resourceStates [$ resourceClass ] ?? app (StateRegistry::class)->getDefaultState ();
4965 }
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+ }
50126}
0 commit comments